我有一个动态创建的tablelayout,每行动态创建了imageview。
这些图片视图是可点击的,并会显示“选择照片意图”
选择/拍摄照片后,图像将加载到图像视图中。
我的问题是,即使我选择了第一张图像,图像也始终会在最新的图像视图中加载。
以下代码:
public class ImageGalleryDemoActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
private Uri outputFileUri;
private ImageView imageView;
private int size = 2;
ImageView imageview_attachrequirement;
TableLayout tablelayout_coverages;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tablelayout_coverages = (TableLayout) findViewById(
R.id.tablelayout_coverages);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
imageView = (ImageView) findViewById(R.id.imgView);
setCoverageTableLayout();
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
openImageIntent();
}
});
}
private void openImageIntent() {
final File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "MyDir" + File.separator);
root.mkdirs();
final String fname = "img_" + System.currentTimeMillis();
final File sdImageMainDirectory = new File(root, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(
captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName,
res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
final Intent chooserIntent = Intent.createChooser(galleryIntent,
"Select Source");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
cameraIntents.toArray(new Parcelable[] {}));
startActivityForResult(chooserIntent, RESULT_LOAD_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == RESULT_LOAD_IMAGE) {
final boolean isCamera;
if (data == null) {
isCamera = true;
} else {
final String action = data.getAction();
if (action == null) {
isCamera = false;
} else {
isCamera = action
.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri selectedImageUri;
if (isCamera) {
selectedImageUri = outputFileUri;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(
selectedImageUri.getPath(), options);
imageview_attachrequirement.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 50, 50, false));
} else {
selectedImageUri = data == null ? null : data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(
selectedImageUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
imageview_attachrequirement.setImageBitmap(Bitmap.createScaledBitmap(BitmapFactory
.decodeFile(picturePath), 50, 50, false));
}
}
}
}
private void setCoverageTableLayout() {
for (int i = 0; i < size; i++) {
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
imageview_attachrequirement = new ImageView(this);
imageview_attachrequirement.setId(i+1);
imageview_attachrequirement.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
imageview_attachrequirement.setPadding(5, 5, 5, 5);
imageview_attachrequirement.setImageResource(R.drawable.icon);
final int index = i;
imageview_attachrequirement.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"Clicked Button Index :" + index,
Toast.LENGTH_LONG).show();
openImageIntent();
}
});
row.addView(imageview_attachrequirement);
TextView textview_coveragedescription = new TextView(this);
textview_coveragedescription.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
textview_coveragedescription.setPadding(5, 5, 5, 5);
textview_coveragedescription.setText(String.valueOf(i));
row.addView(textview_coveragedescription);
TextView textview_coverageamount = new TextView(this);
textview_coverageamount.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
textview_coverageamount.setPadding(5, 5, 5, 5);
textview_coverageamount.setText(String.valueOf(i));
row.addView(textview_coverageamount);
tablelayout_coverages.addView(row);
}
}
}
有没有办法可以在意图之后将图像加载到各自的imageview中?
谢谢!