私のアクティビティでは、Bitmap
オブジェクトを作成してから、別のオブジェクトを起動する必要があります。サブアクティビティ (起動されるアクティビティ) からActivity
このオブジェクトを渡すにはどうすればよいですか?Bitmap
10 に答える
Bitmap
implementsParcelable
であるため、いつでも次の意図で渡すことができます。
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
もう一方の端でそれを取得します。
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
実際には、ビットマップを Parcelable として渡すと、「JAVA BINDER FAILURE」エラーが発生します。ビットマップをバイト配列として渡し、次のアクティビティで表示するために構築してみてください。
ここで私のソリューションを共有しました:
バンドルを使用して Android アクティビティ間で画像 (ビットマップ) をどのように渡しますか?
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 のチェックとビットマップのスケーリングの多くは省略されています。
画像が大きすぎてストレージに保存およびロードできない場合は、ビットマップへのグローバル静的参照 (受信アクティビティ内) を使用することを検討する必要があります。これは、「isChangingConfigurations」の場合にのみ onDestory で null にリセットされます。 true を返します。
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);
}
}
};
上記の解決策はすべてうまくいきません。ビットマップを送信するとparceableByteArray
エラーも発生しますandroid.os.TransactionTooLargeException: data parcel size
。
解決
- 内部ストレージにビットマップを次のように保存しました。
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;
}
putExtra(String)
として送信します
Intent intent = new Intent(ActivitySketcher.this,ActivityEditor.class);
intent.putExtra("KEY", saveBitmap(bmp));
startActivity(intent);
- 他のアクティビティで次のように受け取ります。
if(getIntent() != null){
try {
src = BitmapFactory.decodeStream(openFileInput("myImage"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
私の場合、上記の方法はうまくいきませんでした。ビットマップをインテントに入れるたびに、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));
}
}