0

我有一個位圖圖像在我的活動是這樣的:如何從活動傳遞的位圖圖像到碎片

Bitmap image4 = BitmapFactory.decodeResource(getResources(), R.drawable.hello); 
     image1.setImageBitmap(image4); 

我試圖用這個代碼片段通過這一形象:

FragmentManager fragmentManager = getFragmentManager(); 
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
       fragmentTransaction.addToBackStack(null); 
       Secondfrag yourFragment = new Secondfrag(); 
       Bundle b = new Bundle(); 
       b.putParcelable("BitmapImage",image4); 

       yourFragment.setArguments(b); 
       fragmentTransaction.add(R.id.frag, yourFragment, "FRAGMENT"); 
       fragmentTransaction.commit(); 

,並試圖讓這個圖像中的片段是這樣的:

Bundle b=this.getArguments(); 
     String page=b.getString("BitmapImage"); 

     image2.setImageURI(Uri.parse(page)); 

但在我Fragment.How可熔酚醛樹脂無圖像顯示有這個問題嗎?

+1

傳遞引用是個好主意,我認爲 –

+0

作爲@TahmidRahman說,你可以通過在內部保存通過參考:) –

+0

你可以這樣做存儲.. – Arjun

回答

0

從目的地片斷轉換成字節數組

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

Bundle b = new Bundle(); 
b.putByteArray("image",byteArray); 


    // your fragment code 
fragment.setArguments(b); 

獲得價值

byte[] byteArray = getArgument().getByteArrayExtra("image"); 
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 

感謝https://stackoverflow.com/a/33797090/4316327

0

傳遞圖像引用是一種好方法,而不是傳遞整個圖像數據。

你的情況,如果你想從內部存儲圖像可以傳遞Uri你可以通過你的形象Uri

。但是,如果您只想要特定的資源(即drawable),那麼它只能傳遞其名稱(即String)。

您可以檢查該解決方案Passing Uri to an Intent

0

您可以直接通過您的圖像路徑/ URI中捆綁。

在你的情況下,你正在使用你的資源,使位圖。您可以使用相同的代碼來獲取片段中的位圖。資源在片段中可用作getActivity()。getResources();

Bitmap image4 = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.hello); 
    image1.setImageBitmap(image4); 
0

請試試這個方法

private void nextpageintent(String photoUri){ 

Bundle bundle = new Bundle(); 
bundle.putString("photo", photoUri); 
picture picture = new picture(); 
picture .setArguments(bundle); 
FragmentTransaction transaction = null; 
transaction = getFragmentManager().beginTransaction(); 
transaction.replace(R.id.container, picture); 
transaction.addToBackStack(null); 
transaction.commit(); 

}

0

在這裏,這個問題可以簡單地解決使用Singleton類的getter setter方法, 首先使一個本類的位圖存儲。

class BitmapStorage { 
private static volatile BitmapStorage instance = null; 
private Bitmap bitmap = null; 
private BitmapStorage() {} 

    public static BitmapStorage getInstance() { 
     if (instance == null) { 
      synchronized(BitmapStorage.class) { 
       if (instance == null) { 
        instance = new BitmapStorage(); 
       } 
      } 
     } 
     return instance; 
    } 

    public Bitmap getBitmap(){ 
      return bitmap; 
    } 

    public void setBitmap(Bitmap bitmap){ 
      this.bitmap = bitmap; 
    } 
} 

然後在您的活動中將位圖設置爲您的模型類。像

BitmapStrorage bs = getInstance(); 
Bitmap image4 = BitmapFactory.decodeResource(getResources(), R.drawable.hello); 
Bs.setBitmap(image4); 

只要根據片段的生命週期則onCreate方法第一次調用片段通話,所以在這裏我們可以檢查位爲空或不是,並得到位圖,並使用它。可能不會檢查這個解決方案,但我的事情是爲你工作的。

0

投票Zeerak Hameem你應該考慮以不同的方式

相關問題