2014-02-24 169 views
1

這只是荒謬的..我試圖開發圖像應用程序。我得到的是什麼問題是:在按鈕上單擊我去畫廊和拿起圖片,我在圖像視圖中設置圖像是在同一個意圖。現在點擊按鈕(如繼續),我想將該圖像發送到另一個有圖像視圖的活動。我是新來的機器人,我不知道。我從幾天開始揉揉腦袋。所以請任何人幫助我擺脫這個..提前感謝。並且還支持我將該圖像全屏顯示在另一個活動上。將圖像從一項活動發送給另一項活動?

+0

轉到此處:[http://stackoverflow.com/questions/17878407/send-image-from-one-activity-to-another?rq=1](http://stackoverflow.com/questions/17878407/send-image-from-one-activity-to-another?rq = 1) –

回答

1

當你從一個活動發送圖像到另一個活動,你應該轉換位圖對象的byte [] Arary,然後將其發送。

恰似,

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

    Intent n = new Intent(); 
    n.setClass(getApplicationContext(), MainActivity.class); 
    n.putExtra("picture", byteArray); 
    startActivity(n); 

現在只需檢索在onCreate()方法你的第二個活動。

Bundle extras = getIntent().getExtras(); 
    byte[] byteArray = extras.getByteArray("picture"); 
    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 
    if (bmp != null) { 
    selected.setImageBitmap(bmp); 

    } 
1

請嘗試以下代碼。

對於從第一項活動以圖像,試試這個代碼

imageView.buildDrawingCache(); 
Bitmap bitmap = imageView.getDrawingCache(); 
Intent intent = new Intent(this, SecondActivity.class); 
intent.putExtra("BitmapImage", bitmap); 

這裏更換imageView與您的ImageView ID。

在其他活動中,只需編寫此代碼即可從第一個活動獲取圖像。

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage"); 
+0

在這裏放入額外的位圖作爲第二個參數嗎? –

+0

它是'位圖位圖',位圖對象 – InnocentKiller

+0

將請你提供任何像這樣的教程 –

1

好吧,除非你絕對要,而且圖像非常小,不通過Intent發送圖像本身。您應該已經從Gallery Intent接收圖像URI。只需將該URI作爲額外內容傳遞給用於啓動新Activity的Intent,然後使用該URI在該Activity的ImageView上調用setImageURI()即可。

+0

+1這是最可行的解決方案。 –

0

同意@kcoppock,不要通過意圖傳遞圖像。因爲您可能會在意圖傳球時收到!!!Fail to deliver!!!錯誤。這會由於內存和堆棧限制而發生。

所以你可以通過@kcoppock來解決問題;通過使用圖庫中的圖像URI。然後使用decodeResourcesBitmapFactory並使用該圖像。

否則,您可以使用以前的活動GetterSetter方法,然後通過Getters方法訪問其他活動圖像。

相關問題