[英]How to pass image one activity to another activity?
我想傳遞相機或圖庫中的圖像以顯示另一個活動。在另一個活動中,我只是簡單地投射圖像視圖對象。但是每次都失敗。 發送者活動在這里:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==50 && resultCode==RESULT_OK && data != null){
Bitmap B=(Bitmap) data.getExtras().get("data");
}
else if(requestCode==40 && resultCode==RESULT_OK && data != null){
Bitmap B=(Bitmap) data.getExtras().get("data");
}
}
如果位圖太大,則按意圖攜帶位圖可能會導致應用程序崩潰,因此我建議您將其保存到存儲卡中,然后通過意圖傳遞本地路徑。例如,有一個位圖,則將其保存到本地路徑/storage/emulated/0/Download/mybitmap.jpg
,然后將該路徑放入/storage/emulated/0/Download/mybitmap.jpg
,如intent.putExtra("path","/storage/emulated/0/Download/mybitmap.jpg")
。在目標Activity中,通過getIntent().getStringExtra("path")
。
您可以使用以下方法保存位圖:
public static void saveImage(Bitmap bm, String fileName,boolean needCompress) {
try {
File myCaptureFile = new File(fileName);
File parent = myCaptureFile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
if(needCompress){
bm.compress(Bitmap.CompressFormat.JPEG, 85, bos);
}else{
bm.compress(Bitmap.CompressFormat.PNG, 100, bos);
}
bos.flush();
bos.close();
} catch (IOException e) {
throw new RuntimeException("IOException occurred when saving image: " + fileName, e);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.