我有 Activity1,它有一个 ImaveView,包括图像预览。一旦我按下按钮,我就会从 Activity1 转到 Activity2。在 Activity2 中,我没有图像预览,但有一个选项按钮“保存 Activit1 的图像?” 是还是不是。
目前我正在以错误的方式做事,就像将图像保存在磁盘中然后从磁盘读回一样。但是有什么办法不保存图像我可以将一个 imageView 从 Activity1 传输到 Activity2 吗?
这是我在 Activity1 > ImageView 中获取图片的方法,然后需要将其移至 Activity2。任何想法?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imageView = (ImageView)this.findViewById(R.id.picture);
Button photoButton = (Button) this.findViewById(R.id.capture_btn);
Button btnShareToEmail = (Button) this.findViewById(R.id.btnshare);
btnShareToEmail.setOnClickListener(this);
//photoButton.setOnClickListener(new View.OnClickListener() {
//@Override
// public void onClick(View v) {
// Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// startActivityForResult(cameraIntent, CAMERA_REQUEST);
// }
//});
// without frozen
new Handler().postDelayed(new Runnable() { public void run() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}}, 100);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
编辑:
// 设置 - Activity1
Intent winShare = new Intent(getBaseContext(), Activity2.class);
winShare.putExtra("Title", "r2.jpg");
winShare.putExtra("image1", photo);
//int image_link = getIntent().getIntExtra("image1");
startActivityForResult(winShare,0);
// 获取 - 活动 2
Bitmap bitmap = (Bitmap) getIntent().getParcelableExtra("image1"); // BITMAP_SHARED_KEY = "bitmap_shared_key"
imageView.setImageBitmap(bitmap);
// save it
imageView.buildDrawingCache();
Bitmap bm=imageView.getDrawingCache();
OutputStream fOut = null;
Uri outputFileUri;
try {
File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MYAPPPPPPPPPS" + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
fOut = new FileOutputStream(sdImageMainDirectory);
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
Toast.makeText(this, "Error occured. Please try again later.",Toast.LENGTH_SHORT).show();
}