2015-02-05 117 views
0

在我的應用程序中,我需要將Uri圖像從我的第一個活動轉移到另一個。我知道如何通過意圖發送一個位圖。我是一個很大的程序員,所以我不知道做什麼會更好:轉換Uri的意圖或將Uri更改爲位圖然後發送?如何將Uri圖像從一項活動轉移到另一項活動?

+0

如果只是一個過程只是一個公共的靜態字段添加到您的活動類和分配在你執行這個意圖之前,你可以看到它。 – 2015-02-05 16:12:33

+0

您能否請您提供示例代碼的答案。如果有效,我會很樂意接受它。 – 2015-02-05 16:14:36

+0

不要通過意圖發送整個位圖;發送uri本身作爲額外的 – Eenvincible 2015-02-05 16:16:17

回答

2

與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字符串:

Uri uri; 
String stringUri; 
stringUri = uri.toString(); 

字符串到Uri:

Uri uri; 
String stringUri; 
uri = Uri.parse(stringUri); 
+0

如何在第二個活動中訪問Uri? – 2015-02-05 16:19:10

+0

查看我附加的代碼。 – 2015-02-05 16:23:11

+0

我在一個Uri的第二個Activity中放置了buttom代碼,並且出現了一個錯誤,我應該將它放在一個字符串中 – 2015-02-05 16:27:01

3

爲避免你所得到的錯誤,由三木·弗蘭科給出的代碼,將行:

Uri= extras.getString("KEY"); 

有:

uri= Uri.parse(extras.getString("KEY")); 

這只是爲了讓代碼因爲我認爲你不明白Miki試圖通過代碼解釋什麼。
如果您現在就解決問題,請讓我們發佈。

0
 //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); 
     } 
0
  1. 第一個活動

    Uri uri = data.getData(); 
         Intent intent=new Intent(Firstclass.class,secondclass.class); 
         intent.putExtra("imageUri", uri.toString()); 
         startActivity(intent); 
    
  2. 二等

    Imageview iv_photo=(ImageView)findViewById(R.id.iv_photo); 
    Bundle extras = getIntent().getExtras(); 
    myUri = Uri.parse(extras.getString("imageUri")); 
    iv_photo.setImageURI(myUri); 
    
相關問題