[英]How to transfer a Uri image from one activity to another?
在我的應用程序中,我需要將 Uri 圖像從我的第一個 Activity 傳輸到另一個。 我知道如何通過意圖發送位圖。 我是一個更大的程序員,所以我不知道做什么會更好:有意圖地傳輸 Uri 或將 Uri 更改為位圖然后發送?
與 putExtra 一起使用以發送 Uri 路徑:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent .setClass(ThisActivity.this, NewActivity.class);
intent .putExtra("KEY", Uri);
startActivity(intent );
在 newActivity OnCreate 方法中:
Bundle extras = getIntent().getExtras();
if (extras != null && extras.containsKey("KEY")) {
Uri= extras.getString("KEY");
}
使用那些 func: Uri to String:
Uri uri;
String stringUri;
stringUri = uri.toString();
字符串到 Uri:
Uri uri;
String stringUri;
uri = Uri.parse(stringUri);
為避免出現錯誤,在 Miki franko 給出的代碼中,替換以下行:
Uri= extras.getString("KEY");
和 :
uri= Uri.parse(extras.getString("KEY"));
這只是為了使代碼工作,因為我認為您沒有理解 Miki 試圖通過代碼解釋的內容。
如果您現在解決了問題,請通知我們。
//First Activity to get a Uri
String uri_Str = Uri.toString();
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent .putExtra("uri_Str", uri_Str);
startActivity(intent);
//Second Activity get a Uri path
Bundle b = getIntent().getExtras();
if (b != null) {
String uri_Str= b.getString("uri_Str");
Uri uri = Uri.parse(uri_Str);
}
第一次活動
Uri uri = data.getData(); Intent intent=new Intent(Firstclass.class,secondclass.class); intent.putExtra("imageUri", uri.toString()); startActivity(intent);
二等艙
Imageview iv_photo=(ImageView)findViewById(R.id.iv_photo); Bundle extras = getIntent().getExtras(); myUri = Uri.parse(extras.getString("imageUri")); iv_photo.setImageURI(myUri);
在你的第一堂課中,你可以像這樣傳遞圖像 uri:
Intent intent = new Intent();
intent.putExtra("your_key", imageUri.toString());
startActivity(intent);
在第二個或接收器活動中,您可以通過以下方式訪問圖像 uri:
Bundle extras = getIntent().getExtras();
if(extras != null){
Uri imageUri = Uri.parse(extras.getString("your_key"));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.