我创建了一个自定义图库,用于在android中选择多张照片。我正在使用AsynTask来设置位图。当我滚动屏幕时,图像将替换为下一个图像,并将设置替换为之前的位置。
答案 0 :(得分:0)
public class CustomGallery extends Activity实现了View.OnClickListener {
/ ** *由Ramesh于04年1月4日创建。 * /
private ImageAdapter imageAdapter;
private String[] arrPath;
private boolean[] thumbnailsSelection;
private int ids[];
private int count;
String imgDecode;
ArrayList<String> imageIdList = new ArrayList<String>();
final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_gallery);
System.out.println("MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE-->" + MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
if (Integer.valueOf(Build.VERSION.SDK_INT)>=23) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
Log.i("Permission Log", " Permission Granted----> if cond");
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
} else {
// No explanation needed, we can request the permission.
Log.i("Permission Log", " Permission Granted----> else cond");
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
else{
final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID};
Log.i( MediaStore.Images.Media.DATA, "Media Store Images.Media.Data");
Log.i(MediaStore.Images.Media._ID, "Media Store Images.Media_id" );
final String orderBy = MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC" ;
Cursor imageCursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null,
orderBy );
final int image_column_index = imageCursor.getColumnIndex(MediaStore.Images.Media._ID);
imgDecode = imageCursor.getColumnName(image_column_index);
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);
}
GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);
imageCursor.close();
final Button selectBtn = (Button) findViewById(R.id.selectBtn);
selectBtn.setOnClickListener(this);
Button cancelBtn = (Button) findViewById(R.id.cancelBtn);
cancelBtn.setOnClickListener(this);
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
setResult(Activity.RESULT_CANCELED);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
Log.i(" Permission Log", " Permission Granted");
final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID};
Log.i( MediaStore.Images.Media.DATA, "Media Store Images.Media.Data");
Log.i(MediaStore.Images.Media._ID, "Media Store Images.Media_id" );
final String orderBy = MediaStore.Images.Media._ID;
Cursor imageCursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
//final int image_column_index = imageCursor.getColumnIndex(MediaStore.Images.Media._ID);
final int image_column_index = imageCursor.getColumnIndex(MediaStore.Images.Media._ID );
imgDecode = imageCursor.getColumnName(image_column_index);
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);
}
GridView image_grid = (GridView) findViewById(R.id.PhoneImageGrid);
imageAdapter = new ImageAdapter();
image_grid.setAdapter(imageAdapter);
imageCursor.close();
final Button selectBtn = (Button) findViewById(R.id.selectBtn);
selectBtn.setOnClickListener(this);
Button cancelBtn = (Button) findViewById(R.id.cancelBtn);
cancelBtn.setOnClickListener(this);
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Log.i("Permission Log", " else cond");
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
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.MINI_KIND, null);
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
iv.setImageBitmap(result);
}
}.execute();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.selectBtn:
final int len = thumbnailsSelection.length;
String selectImages = "";
int cnt = 0;
for (int i = 0; i < len; i++) {
if (thumbnailsSelection[i]) {
cnt++;
Log.i("Custom Gallery", "Inside for loop----->");
selectImages = selectImages + arrPath[i];
System.out.println("Array Path--" + arrPath[i]);
imageIdList.add(arrPath[i]);
}
}
if (cnt == 0) {
Toast.makeText(getApplicationContext(), "Please select at least one image", Toast.LENGTH_LONG).show();
} else if (cnt > 5) {
Toast.makeText(getApplicationContext(), "You can select only 5 images", Toast.LENGTH_LONG).show();
} else {
// System.out.println("My ListItem -->>" + imageIdList);
/* Bundle bundel = new Bundle();
bundel.putStringArrayList("key", imageIdList);
Intent intent = new Intent(getApplicationContext(), AddVehicle.class);
intent.putExtras(bundel);
startActivity(intent);
finish();*/
Intent returnIntent = new Intent();
returnIntent.putStringArrayListExtra("image-list", imageIdList);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
break;
case R.id.cancelBtn:
onBackPressed();
break;
}
}
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.gallery_item, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
convertView.setTag(holder);
/* } else {
holder = (ViewHolder) convertView.getTag();
} */
holder.checkbox.setId(position);
holder.imageview.setId(position);
holder.checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
int id = cb.getId();
if (thumbnailsSelection[id]) {
cb.setChecked(false);
System.out.println("if thumbnail selection");
thumbnailsSelection[id] = false;
} else {
System.out.println("else thumbnail selection");
cb.setChecked(true);
thumbnailsSelection[id] = true;
}
}
});
holder.imageview.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int id = holder.checkbox.getId();
if (thumbnailsSelection[id]) {
Log.i("Custom Gallery", "if image click----->");
holder.checkbox.setChecked(false);
thumbnailsSelection[id] = false;
} else {
Log.i("Custom Gallery", "if image not clicked----->");
holder.checkbox.setChecked(true);
thumbnailsSelection[id] = true;
}
}
});
try {
setBitmap(holder.imageview, ids[position]);
} catch (Throwable e) {
Log.i(e.getMessage(), "Exception:");
}
holder.checkbox.setChecked(thumbnailsSelection[position]);
holder.id = position;
return convertView;
}
}
class ViewHolder {
ImageView imageview;
CheckBox checkbox;
int id;
}
}