我有自定义照片库,我需要获取所选图像并在列表视图中显示其文件名
这是我的自定义图库:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class CustomPhotoGallery extends Activity {
private GridView grdImages;
private Button btnSelect;
private ImageAdapter imageAdapter;
private String[] arrPath;
private boolean[] thumbnailsselection;
private int ids[];
private int count;
String filename;
/**
* Overrides methods
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_gallery);
grdImages= (GridView) findViewById(R.id.grdImages);
btnSelect= (Button) findViewById(R.id.btnSelect);
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
@SuppressWarnings("deprecation")
Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.arrPath = new String[this.count];
ids = new int[count];
this.thumbnailsselection = new boolean[this.count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
ids[i] = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = imagecursor.getString(dataColumnIndex);
}
imageAdapter = new ImageAdapter();
grdImages.setAdapter(imageAdapter);
imagecursor.close();
btnSelect.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final int len = thumbnailsselection.length;
int cnt = 0;
String selectImages = "";
for (int i = 0; i < len; i++) {
if (thumbnailsselection[i]) {
cnt++;
selectImages = selectImages + arrPath[i] + "|";
}
}
if (cnt == 0) {
Toast.makeText(getApplicationContext(), "Please select at least one image", Toast.LENGTH_LONG).show();
} else {
Log.d("SelectedImages", selectImages);
Intent i = new Intent();
i.putExtra("data", selectImages);
setResult(Activity.RESULT_OK, i);
finish();
}
}
});
}
@Override
public void onBackPressed() {
setResult(Activity.RESULT_CANCELED);
super.onBackPressed();
}
/**
* Class method
*/
/**
* This method used to set bitmap.
* @param iv represented ImageView
* @param id represented id
*/
private void setBitmap(final ImageView iv, final int id) {
new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... params) {
return MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
iv.setImageBitmap(result);
}
}.execute();
}
/**
* List adapter
* @author tasol
*/
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.custom_gallery_item, null);
holder.imgThumb = (ImageView) convertView.findViewById(R.id.imgThumb);
holder.chkImage = (CheckBox) convertView.findViewById(R.id.chkImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.chkImage.setId(position);
holder.imgThumb.setId(position);
holder.chkImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
int id = cb.getId();
if (thumbnailsselection[id]) {
cb.setChecked(false);
thumbnailsselection[id] = false;
} else {
cb.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
holder.imgThumb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int id = holder.chkImage.getId();
if (thumbnailsselection[id]) {
holder.chkImage.setChecked(false);
thumbnailsselection[id] = false;
} else {
holder.chkImage.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
try {
setBitmap(holder.imgThumb, ids[position]);
} catch (Throwable e) {
}
holder.chkImage.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
}
/**
* Inner class
* @author tasol
*/
class ViewHolder {
ImageView imgThumb;
CheckBox chkImage;
int id;
}
}
现在基于选择的结果进入主要活动,我需要在列表视图中显示所有选定的图像。
这就是我要做的事情
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if(requestCode == PICK_IMAGE_MULTIPLE){
imagesPathList = new ArrayList<String>();
String[] imagesPath = data.getStringExtra("data").split("\\|");
try{
lnrImages.removeAllViews();
}catch (Throwable e){
e.printStackTrace();
}
for (int i=0;i<imagesPath.length;i++){
//Toast.makeText(MainActivity.this,String.valueOf(imagesPath.length),Toast.LENGTH_LONG).show();
imagesPathList.add(imagesPath[i]);
//Toast.makeText(MainActivity.this,imagesPathList.toString(),Toast.LENGTH_LONG).show();
yourbitmap = BitmapFactory.decodeFile(imagesPath[i]);
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(yourbitmap);
imageView.setAdjustViewBounds(true);
//lnrImages.addView(imageView);
Uri uri = (Uri.fromFile(new File(imagesPath[i])));
filename = uri.getLastPathSegment();
//Toast.makeText(MainActivity.this,filename,Toast.LENGTH_LONG).show();
Uri selectedImage = data.getData();
encodedImage= bitmapToBase64(yourbitmap);
//Toast.makeText(MainActivity.this,encodedImage,Toast.LENGTH_LONG).show();
myStringList.add(filename);
myStringArray = myStringList.toArray();
lv.setAdapter(null);
Toast.makeText(MainActivity.this,(String)myStringArray[i],Toast.LENGTH_LONG).show();
arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
myStringList );
lv.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
}
}
}
现在我的问题是:
1)如果我选择一张图片,它会向我显示带有文件名的吐司,但不会将其添加到listview
2)如果我选择两个图像,则将其添加到列表视图中
3)如果我选择两个以上它附加到旧列表(即......将新选择的图像添加到先前选择的图像),尽管我已将adapter
设置为'NULL'
4)由于crashing
,选择大量图片需要花费时间将其添加到应用的列表视图memory
。
那么有人能建议我处理这个问题的最佳方法吗?
提前致谢!
答案 0 :(得分:0)
您的代码中存在多个问题。与在Java
中一样,所有内容都是引用,因此您不应该反复创建适配器,而应该只将新列表设置为适配器,然后调用notifyDataSetChanged
。例如,您的ImageAdapter
应该是这样的
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<String> imagesList = new ArrayList<>();
public void updateImagesList(ArrayList<Strinf> newImages) {
if(newImages != null && newImages.size() > 0) {
this.imagesList.clear();
this.imagesList.addAll(newImages);
}
}
}
然后你应该创建我们将用于未来列表更新的全局Adapter
变量
并且在onActivityResult
中,您应该在循环中填充字符串列表,然后在循环之后,您应该将适配器列表更新为
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == PICK_IMAGE_MULTIPLE) {
for (int i = 0; i < imagesPath.length; i++) {
//Toast.makeText(MainActivity.this,String.valueOf(imagesPath.length),Toast.LENGTH_LONG).show();
imagesPathList.add(imagesPath[i]);
//Toast.makeText(MainActivity.this,imagesPathList.toString(),Toast.LENGTH_LONG).show();
yourbitmap = BitmapFactory.decodeFile(imagesPath[i]);
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(yourbitmap);
imageView.setAdjustViewBounds(true);
//lnrImages.addView(imageView);
Uri uri = (Uri.fromFile(new File(imagesPath[i])));
filename = uri.getLastPathSegment();
//Toast.makeText(MainActivity.this,filename,Toast.LENGTH_LONG).show();
Uri selectedImage = data.getData();
encodedImage = bitmapToBase64(yourbitmap);
//Toast.makeText(MainActivity.this,encodedImage,Toast.LENGTH_LONG).show();
myStringList.add(filename);
Toast.makeText(MainActivity.this, (String) myStringArray[i], Toast.LENGTH_LONG).show();
}
arrayAdapter.updateImages(myStringList);
arrayAdapter.notifyDataSetChanged();
}
}
}
除上述两项更改外,还有 性能影响 ,因为您正在对Ui线程执行I / O操作,因为您正在解码位图文件主线程这不是一个好方法,因为它会导致 OutOfMemoryError 以及它会降低您的应用程序。您应该使用通用图像加载器https://github.com/nostra13/Android-Universal-Image-Loader
进行位图操作或显示希望这有帮助。