あるアクティビティから別のアクティビティに Bitmap オブジェクトを渡すにはどうすればよいですか
-
20-09-2019 - |
質問
私の活動では、 Bitmap
オブジェクトがある場合は、別のオブジェクトを起動する必要があります Activity
、どうすればこれに合格できますか Bitmap
サブアクティビティからのオブジェクト (起動されるもの)?
解決
Bitmap
はParcelable
を実装して、あなたは常に意図して、それを渡すことができます:
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としてビットマップをPasssingするためParceable(1メガバイト)のサイズ制限の良いアイデアではありません。あなたは、内部ストレージ内のファイルにビットマップを保存し、いくつかの活動に格納されているビットマップを取得することができます。ここではいくつかのサンプルコードです。
内部ストレージでの 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"));
注のヌルをチェックすると、ビットマップのスケーリングの多くはommitedされます。
は、イメージが大きすぎる場合、あなたは、あなただけの場合にのみ、onDestoryにnullにリセットされます(受信アクティビティ内)ビットマップへのグローバル静的参照を、使用することを検討すべきであるセーブ&ストレージにロードすることはできません"isChangingConfigurationsは" 真を返します。
テントはサイズ制限があるため。 私は放送するサービスからパスビットマップを行うには、パブリック静的オブジェクトを使用して....
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();
}
});
}
私BroadcastReceiver
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);
}
}
};
圧縮して送信 Bitmap
受け入れられた回答は、 Bitmap
大きすぎます。 それはだと思います 1MB 限界。の Bitmap
などの別のファイル形式に圧縮する必要があります。 JPG で表される ByteArray
, 、その後、それを経由して安全に渡すことができます Intent
.
実装
この関数は、次を使用して別のスレッドに含まれています。 Kotlin コルーチン なぜなら Bitmap
圧縮はその後に連鎖します。 Bitmap
URLから作成される String
. 。の Bitmap
作成には別のスレッドが必要です。 アプリケーションが応答しない (ANR) エラー。
使用される概念
- Kotlin コルーチン ノート.
- の 読み込み、コンテンツ、エラー (LCE) パターンは以下で使用されます。ご興味がございましたら、以下で詳細をご覧ください。 この講演とビデオ.
- ライブデータ データを返すために使用されます。お気に入りをまとめてみました ライブデータ のリソース これらのメモ.
- で ステップ3,
toBitmap()
です Kotlin拡張機能 そのライブラリをアプリの依存関係に追加する必要があります。
コード
1.圧縮する Bitmap
に JPG ByteArray
作成された後。
リポジトリ.kt
suspend fun bitmapToByteArray(url: String) = withContext(Dispatchers.IO) {
MutableLiveData<Lce<ContentResult.ContentBitmap>>().apply {
postValue(Lce.Loading())
postValue(Lce.Content(ContentResult.ContentBitmap(
ByteArrayOutputStream().apply {
try {
BitmapFactory.decodeStream(URL(url).openConnection().apply {
doInput = true
connect()
}.getInputStream())
} catch (e: IOException) {
postValue(Lce.Error(ContentResult.ContentBitmap(ByteArray(0), "bitmapToByteArray error or null - ${e.localizedMessage}")))
null
}?.compress(CompressFormat.JPEG, BITMAP_COMPRESSION_QUALITY, this)
}.toByteArray(), "")))
}
}
ViewModel.kt
//Calls bitmapToByteArray from the Repository
private fun bitmapToByteArray(url: String) = liveData {
emitSource(switchMap(repository.bitmapToByteArray(url)) { lce ->
when (lce) {
is Lce.Loading -> liveData {}
is Lce.Content -> liveData {
emit(Event(ContentResult.ContentBitmap(lce.packet.image, lce.packet.errorMessage)))
}
is Lce.Error -> liveData {
Crashlytics.log(Log.WARN, LOG_TAG,
"bitmapToByteArray error or null - ${lce.packet.errorMessage}")
}
}
})
}
2.画像を次のように渡します ByteArray
経由で Intent
.
このサンプルでは、 断片 に サービス. 。二人で共有しても同じ概念です 活動内容.
フラグメント.kt
ContextCompat.startForegroundService(
context!!,
Intent(context, AudioService::class.java).apply {
action = CONTENT_SELECTED_ACTION
putExtra(CONTENT_SELECTED_BITMAP_KEY, contentPlayer.image)
})
3.変換する ByteArray
戻る Bitmap
.
ユーティリティ.kt
fun ByteArray.byteArrayToBitmap(context: Context) =
run {
BitmapFactory.decodeByteArray(this, BITMAP_OFFSET, size).run {
if (this != null) this
// In case the Bitmap loaded was empty or there is an error I have a default Bitmap to return.
else AppCompatResources.getDrawable(context, ic_coinverse_48dp)?.toBitmap()
}
}
これは遅くなるかもしれませんが、助けることができます。 最初のフラグメントまたは活性に対するクラスを宣言しない...例えば
@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;
それがお役に立てば幸いです。
あなたは、ビットマップ転送を作成することができます。これを試して....
ファーストクラスでます:
1)を作成します:
private static Bitmap bitmap_transfer;
2)ゲッターとセッターを作成します。
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());
次に、第2のクラスの
ImageView image2 = (ImageView) view.findViewById(R.id.img2);
imagem2.setImageDrawable(new BitmapDrawable(getResources(), classe1.getBitmap_transfer()));
上記の解決策はすべて私にとっては機能しません。ビットマップを次のように送信しています 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();
}
}
私の場合は、上記の方法は、私のために働いていませんでした。私は意思でビットマップを置くたびに、第二の活動は開始されませんでした。私は[]バイトとしてビットマップを渡されたときにも同じことが起こりました。
私はこのリンクを続きますそしてそれはシャルムのように働いたと非常に速います:
package your.packagename
import android.graphics.Bitmap;
public class CommonResources {
public static Bitmap photoFinishBitmap = null;
}
私の第一acitiviyでます:
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));
}
}