158

私のアクティビティでは、Bitmapオブジェクトを作成してから、別のオブジェクトを起動する必要があります。サブアクティビティ (起動されるアクティビティ) からActivityこのオブジェクトを渡すにはどうすればよいですか?Bitmap

4

10 に答える 10

317

BitmapimplementsParcelableであるため、いつでも次の意図で渡すことができます。

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

もう一方の端でそれを取得します。

Intent intent = getIntent(); 
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
于 2010-03-17T02:50:49.340 に答える
25

実際には、ビットマップを Parcelable として渡すと、「JAVA BINDER FAILURE」エラーが発生します。ビットマップをバイト配列として渡し、次のアクティビティで表示するために構築してみてください。

ここで私のソリューションを共有しました:
バンドルを使用して Android アクティビティ間で画像 (ビットマップ) をどのように渡しますか?

于 2011-10-25T14:06:30.737 に答える
21

Parceable(1mb) のサイズ制限のため、アクティビティ間のバンドルでビットマップを parceable として渡すことはお勧めできません。ビットマップを内部ストレージのファイルに保存し、保存されたビットマップをいくつかのアクティビティで取得できます。ここにいくつかのサンプルコードがあります。

内部ストレージのファイルmyImageにビットマップを保存するには:

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 をビットマップにデコードできます。

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

null のチェックとビットマップのスケーリングの多くは省略されています。

于 2014-07-08T11:50:26.850 に答える
4

画像が大きすぎてストレージに保存およびロードできない場合は、ビットマップへのグローバル静的参照 (受信アクティビティ内) を使用することを検討する必要があります。これは、「isChangingConfigurations」の場合にのみ onDestory で null にリセットされます。 true を返します。

于 2015-05-21T11:04:28.200 に答える
3

Intent にはサイズ制限があるためです。public static オブジェクトを使用して、ビットマップをサービスからブロードキャストに渡します....

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);
            }
        }
    };
于 2015-09-24T07:40:22.390 に答える
1

上記の解決策はすべてうまくいきません。ビットマップを送信するとparceableByteArrayエラーも発生しますandroid.os.TransactionTooLargeException: data parcel size

解決

  1. 内部ストレージにビットマップを次のように保存しました。
public String saveBitmap(Bitmap bitmap) {
        String fileName = "ImageName";//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;
    }
  1. putExtra(String)として送信します
Intent intent = new Intent(ActivitySketcher.this,ActivityEditor.class);
intent.putExtra("KEY", saveBitmap(bmp));
startActivity(intent);
  1. 他のアクティビティで次のように受け取ります。
if(getIntent() != null){
  try {
           src = BitmapFactory.decodeStream(openFileInput("myImage"));
       } catch (FileNotFoundException e) {
            e.printStackTrace();
      }

 }


于 2019-01-29T12:30:42.943 に答える
-2

私の場合、上記の方法はうまくいきませんでした。ビットマップをインテントに入れるたびに、2 番目のアクティビティが開始されませんでした。ビットマップを byte[] として渡したときにも同じことが起こりました。

このリンクをたどったところ、魅力的で非常に高速に機能しました。

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);

そして、これが私の2番目のアクティビティの onCreate() です:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bitmap photo = Constants.photoFinishBitmap;
    if (photo != null) {
        mViewHolder.imageViewerImage.setImageDrawable(new BitmapDrawable(getResources(), photo));
    }
}
于 2013-07-26T12:19:43.783 に答える