2010-03-17 99 views

回答

253

Bitmap實現Parcelable,所以你總是可以的意圖傳遞:

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

和檢索它的另一端:

Intent intent = getIntent(); 
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage"); 
+68

如果位圖以文件或資源的形式存在,則傳遞位圖的URI或ResourceID總是最好,而不是傳遞位圖本身。傳遞整個位圖需要大量的內存。傳遞URL需要很少的內存,並允許每個活動根據需要加載和縮放位圖。 – slayton 2011-10-26 14:13:18

+0

不工作在2.3 – 2012-10-30 13:10:41

+3

不適用於我,但這一個:http://stackoverflow.com/questions/11010386/send-bitmap-using-intent-android – Houssem 2013-02-04 10:55:32

-2

在我的情況下,上述辦法,我沒有工作。每次我把位圖放在意圖中,第二個活動都沒有開始。當我將位圖傳遞給byte []時發生了同樣的情況。

我跟着這個link和它的工作像一個風情萬種,非常快:在我的第一acitiviy

package your.packagename 

import android.graphics.Bitmap; 

public class CommonResources { 
     public static Bitmap photoFinishBitmap = null; 
} 

Constants.photoFinishBitmap = photoFinishBitmap; 
Intent intent = new Intent(mContext, ImageViewerActivity.class); 
startActivity(intent); 

而這裏的onCreate()我的第二個活動:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Bitmap photo = Constants.photoFinishBitmap; 
    if (photo != null) { 
     mViewHolder.imageViewerImage.setImageDrawable(new BitmapDrawable(getResources(), photo)); 
    } 
} 
+0

我試過了,沒有工作。我跟着鏈接,看起來你應該使用'CommonResources.photoFinishBitmap'而不是'Constants.photoFinishBitmap'。 – 2014-02-26 15:55:50

+0

不好的做法。整個過程重新創建期間(例如,由於在運行時更改應用程序的權限),Activity類中的靜態字段會發生什麼情況?答案是NPE。 – Alexander 2016-08-16 14:13:42

7

傳遞位圖作爲parceable之間的捆綁活動不是一個好主意由於Parceable(1mb)的大小限制。您可以將位圖存儲在內部存儲器中的文件中,並在多個活動中檢索存儲的位圖。以下是一些示例代碼。

MYIMAGE存儲位圖文件在內部存儲:

Bitmap bitmap = BitmapFactory.decodeStream(context 
        .openFileInput("myImage"));//here context can be anything like getActivity() for fragment, this or MainActivity.this 

public String createImageFromBitmap(Bitmap bitmap) { 
    String fileName = "myImage";//no .png or .jpg needed 
    try { 
     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
     FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE); 
     fo.write(bytes.toByteArray()); 
     // remember close file output 
     fo.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     fileName = null; 
    } 
    return fileName; 
} 

然後在接下來的活動中,你可以將此文件MYIMAGE使用下面的代碼解碼爲位圖注意許多檢查空位和縮放位圖的操作是不受歡迎的。

4

如果圖片太大而無法保存&將其加載到存儲中,則應考慮只使用位圖(在接收活動內)的全局靜態引用,該位圖將在onDestory上重置爲null只有當「isChangingConfigurations」返回true時。

3

由於意圖的大小限制。 我使用公共靜態對象做傳遞位圖從服務廣播....

public class ImageBox { 
    public static Queue<Bitmap> mQ = new LinkedBlockingQueue<Bitmap>(); 
} 

掠過我的服務

private void downloadFile(final String url){ 
     mExecutorService.submit(new Runnable() { 
      @Override 
      public void run() { 
       Bitmap b = BitmapFromURL.getBitmapFromURL(url); 
       synchronized (this){ 
        TaskCount--; 
       } 
       Intent i = new Intent(ACTION_ON_GET_IMAGE); 
       ImageBox.mQ.offer(b); 
       sendBroadcast(i); 
       if(TaskCount<=0)stopSelf(); 
      } 
     }); 
    } 

我的廣播接收器

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
     public void onReceive(Context context, Intent intent) { 
      LOG.d(TAG, "BroadcastReceiver get broadcast"); 

      String action = intent.getAction(); 
      if (DownLoadImageService.ACTION_ON_GET_IMAGE.equals(action)) { 
       Bitmap b = ImageBox.mQ.poll(); 
       if(b==null)return; 
       if(mListener!=null)mListener.OnGetImage(b); 
      } 
     } 
    }; 
0

這可能是晚了,但可以提供幫助。 的第一個片段或活動不聲明一個類...例如

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     description des = new description(); 

     if (requestCode == PICK_IMAGE_REQUEST && data != null && data.getData() != null) { 
      filePath = data.getData(); 
      try { 
       bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath); 
       imageView.setImageBitmap(bitmap); 
       ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
       bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
       constan.photoMap = bitmap; 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

public static class constan { 
    public static Bitmap photoMap = null; 
    public static String namePass = null; 
} 

然後在第二類/片段做..

Bitmap bm = postFragment.constan.photoMap; 
final String itemName = postFragment.constan.namePass; 

希望它能幫助。

0

您可以創建位圖傳輸。嘗試這種....

在第一類中:

1)創建:

private static Bitmap bitmap_transfer; 

2)創建getter和setter

public static Bitmap getBitmap_transfer() { 
    return bitmap_transfer; 
} 

public static void setBitmap_transfer(Bitmap bitmap_transfer_param) { 
    bitmap_transfer = bitmap_transfer_param; 
} 

3)設置圖像:

ImageView image = (ImageView) view.findViewById(R.id.image); 
image.buildDrawingCache(); 
setBitmap_transfer(image.getDrawingCache()); 

然後,在第二段ss:

ImageView image2 = (ImageView) view.findViewById(R.id.img2); 
imagem2.setImageDrawable(new BitmapDrawable(getResources(), classe1.getBitmap_transfer())); 
相關問題