我在Android中将图片从一个活动传递到另一个活动时遇到了问题。
我可以使用以下代码将图像从第1个活动传递到第2个活动,没有任何问题:
public void onEditPhotosClicked(View view) {
Intent intent = new Intent(this, EditPhotos.class);
// Pass the imgDecodableString as an intent to the EditPhotos activity, along with the ref
intent.putExtra("imagePath", imgDecodableString);
if (imgDecodableString == null){
Toast.makeText(this, "You haven't picked an image!",
Toast.LENGTH_LONG).show();
return;
}
else {
final int result = 1;
startActivityForResult(intent, result);
}
}
在接收活动的onCreate()方法中,以下代码显示图像:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_photos);
final ImageView imageView;
// Get the image view idea from the XML file using findViewById().
imageView = (ImageView) findViewById(R.id.imageView);
// Now get the intent passed using the getIntent().getStringExtra() method.
imagePath= getIntent().getStringExtra("imagePath");
// Set up a bitmap and call the decodeFile() method to get the image data from the image path.
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
// Set the image view to the bitmap above to display the image.
imageView.setImageBitmap(bitmap);
}
然而,当我尝试完全相同的程序将相同的图像从此接收活动传递到第三个活动时,我没有得到任何图像。显然,我错过了一些东西。任何人都可以帮忙吗?
以下是第三个活动的onCreate()方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_resize_photos);
String imagePath = getIntent().getStringExtra("imagePath");
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
ImageView imageView = (ImageView)findViewById(R.id.imageView);
// Set the image view to the bitmap above to display the image.
imageView.setImageBitmap(bitmap);
}