gpt4 book ai didi

android - 图像从 sd 卡到 galleryview

转载 作者:太空狗 更新时间:2023-10-29 16:21:13 26 4
gpt4 key购买 nike

在我的应用程序中,我想从 SD 卡获取图像到特定位置,我需要在图库中显示,

我使用的代码是附加的

private Gallery gallery;
private ImageView imgView;
int position;
private byte[] data = { };

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
try{

String filepath = "/sdcard/data/Crak/";
File imagefile = new File(filepath +"abcd.jpg" );
FileInputStream fis = new FileInputStream(imagefile);
Bitmap bi = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
System.out.println("cnvrtn");
}
catch(Exception e) {
e.printStackTrace();

}

final Bitmap bitmapimage = BitmapFactory.decodeByteArray(data, 0, data.length);
position = 0;
imgView = (ImageView) findViewById(R.id.ImageView01);
imgView.setImageBitmap(bitmapimage);

gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));

gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
imgView.setImageBitmap(bitmapimage);
DisplayImage.this.position = position;
}
});
System.out.println("Enter the activity//////////");

imgView.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("deprecation")


@Override
public void onClick(View paramView) {
// TODO Auto-generated method stub

}
});

}

public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;

@SuppressWarnings("null")
public AddImgAdp(Context c) {
cont = c;

TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(
R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}

public int getCount() {
return data.length;
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {


ImageView imgView = new ImageView(cont);
final Bitmap bitmapimage = BitmapFactory.decodeByteArray(data, 0, data.length);
imgView.setImageBitmap(bitmapimage);;
imgView.setLayoutParams(new Gallery.LayoutParams(100, 100));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);

return imgView;

}
}

问题是

1)如何获取SD卡中特定文件夹中的所有图像
2) GalleryView 不定或长个没完没了

最佳答案

这是从特定路径获取图像的更新代码...

package com.example.gall;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.example.gall.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

public class gall extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this, ReadSDCard()));

g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View v, int position, long id) {
}
});
}

private List<String> ReadSDCard()
{
List<String> tFileList = new ArrayList<String>();

//It have to be matched with the directory in SDCard
File f = new File("/android/sdcard2/");

File[] files=f.listFiles();

for(int i=0; i<files.length; i++)
{
File file = files[i];
/*It's assumed that all file in the path are in supported type*/
tFileList.add(file.getPath());
}
return tFileList;
}

public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private List<String> FileList;

public ImageAdapter(Context c, List<String> fList) {
mContext = c;
FileList = fList;
TypedArray a = obtainStyledAttributes(R.styleable.gall);
mGalleryItemBackground = a.getResourceId(
R.styleable.gall_android_galleryItemBackground,0);
a.recycle();
}

public int getCount() {
return FileList.size();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView,
ViewGroup parent) {
ImageView i = new ImageView(mContext);

Bitmap bm = BitmapFactory.decodeFile(
FileList.get(position).toString());
i.setImageBitmap(bm);

i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);

return i;
}
}

public TypedArray obtainStyledAttributes(int theme) {
// TODO Auto-generated method stub
return null;
}
}

关于android - 图像从 sd 卡到 galleryview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14230128/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com