2016-10-16 32 views
-4

這是代碼傳輸位圖的另一個活動

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    Intent intent = new Intent(this, Main2Activity.class); 
    startActivity(intent); 

    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 

    Bitmap bp = (Bitmap) data.getExtras().get("data"); 
    iv.setImageBitmap(bp); 

} 

我已經創建了一個其他活動,但我不知道在secondactivity轉讓此位圖。

+0

如果您尋求幫助而不是要求幫助,那會很好。 – Bas

+0

您可能想嘗試瞭解Intents是如何使用的。你顯然有'data.getExtras()。get(「data」)',並且有一個相應的Intent類的putExtra方法,你需要啓動一個Activity。另外,請在發佈之前嘗試搜索潛在的副本 –

+1

請注意,「Intent」需要相當小,這意味着您的「Bitmap」需要相當小(遠低於1MB)。 – CommonsWare

回答

0

這是如何發送數據到下一個活動。並在下一個活動中檢索它。

intent.putExtra("bitmap", bp); 

檢索它:

// getIntent() Returns the intent that started the activity. 
Intent intent = getIntent(); 
Bitmap bp = intent.getParcelableExtra("bitmap"); 

而像@ cricket_007建議您可能要看一看意圖。如果位圖作爲文件形式存在或資源 https://developer.android.com/reference/android/content/Intent.html

0

更改您的代碼如下

Bitmap bp = (Bitmap) intent.getParcelableExtra("data"); 
iv.setImageBitmap(bp); 
0

,它是更好地通過位圖的URI或資源ID,而不是位圖本身傳遞位圖需要高內存比URI。

如果您仍想在第一個活動中傳遞位圖下面的代碼。

Intent i = new Intent(this, Second.class) 
i.putExtra("Image", bitmap); 
startActivity(i); 

和你的第二個活動,你可以得到它

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Image"); 
0

雖然你可以通過Bitmap作爲parcelable額外的,它並不總是對我過去工作。我相信,如果壓縮Bitmap並將其作爲字節數組傳遞給第二個活動,則會更好。

在第一個活動:

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
byte[] byteArray = stream.toByteArray(); 

Intent intent = new Intent(FirstActivity.this, SecondActivity.class); 
intent.putExtra("bitmap",byteArray); 

在第二個活動:

byte[] byteArray = getIntent().getByteArrayExtra("bitmap"); 
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 
0

位圖實現Parcelable,所以你總是可以通過它的意圖:

Intent intent = new Intent(this, NewActivity.class); 
intent.putExtra("BitmapImage", bitmap); 

和在另一端檢索它:

Intent intent = getIntent(); 
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage"); 
相關問題