簡體   English   中英

將圖像從一個活動傳遞到另一個活動

[英]Passing image from one activity another activity

SO 上也有類似的問題,但沒有一個對我有用。

我想在 Activity1 中獲取點擊的圖像並將其顯示在 Activity2 中。
我正在像這樣獲取點擊圖像的圖像 ID:

((ImageView) v).getId()

並通過意圖將其傳遞給另一個活動。

在第二個活動中,我使用圖像 ID 如下:

imageView.setImageResource(imgId);

我在兩個活動中都記錄了值 og image id,它是相同的。

但我收到以下異常:

android.content.res.Resources$NotFoundException: Resource is not a Drawable 
(color or path): TypedValue{t=0x12/d=0x0 a=2 r=0x7f050000}

我想這里的問題是getId()返回的是ImageView的 Id 而不是它的源圖像
所有這些圖像都存在於drawable中。

任何幫助表示贊賞。

有 3 種解決方案可以解決此問題。

1)首先將圖像轉換為字節數組,然后傳遞給 Intent,在下一個活動中,從 Bundle 中獲取字節數組並轉換為圖像(位圖)並設置到 ImageView 中。

將位圖轉換為字節數組:-

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

將字節數組傳遞給意圖:-

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);

從 Bundle 中獲取字節數組並轉換為位圖圖像:-

Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

2) 首先將圖像保存到 SDCard 中,然后在下一個活動中將此圖像設置到 ImageView 中。

3) 將位圖傳遞給 Intent 並在下一個活動中從捆綁包中獲取位圖,但問題是如果您的位圖/圖像大小當時很大,則圖像不會在下一個活動中加載。

這行不通。 你必須這樣嘗試。

將您的ImageView 的DrawingCache 設置為true,然后將背景保存為Bitmap 並通過putExtra 傳遞。

image.setDrawingCacheEnabled(true);
Bitmap b=image.getDrawingCache();
Intent i = new Intent(this, nextActivity.class);

i.putExtra("Bitmap", b);
startActivity(i);

在你的下一個活動中,

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Bitmap");
imageView.setImageBitmap(bitmap);

在您的Application類中定義一個Drawable靜態變量,然后在第一個活動中設置圖像可繪制數據,然后在您的下一個活動中從您在Application類中定義的靜態變量獲取數據。

public class G extends Application {
   public static Drawable imageDrawable;

   ...
}

第一項活動:

G.imageDrawable = imageView.getDrawable();

第二個活動:

imgCamera.setImageDrawable(G.imageDrawable);

在 onDestroy 中:

@Override
protected void onDestroy() {
    G.imageDrawable = null;
    super.onDestroy();
}

注意:您必須在清單中定義您的Application類:

<application
        android:name=".G"
        ...>

</application>

如果您要從 addapter 類之類的類移動,請使用此代碼。

Bitmap bitImg=listItem.getBitmapImage();
    ByteArrayOutputStream baoS = new ByteArrayOutputStream();
    bitImg.compress(Bitmap.CompressFormat.JPEG, 50, baoS);
    intent.putExtra("bitArray", baoS.toByteArray());
    context.getApplicationContext().startActivity(intent);

然后通過這個到下一個活動

if(getIntent().hasExtra("bitArray")) {                
Bitmap bitM = BitmapFactory.decodeByteArray( getIntent().getByteArrayExtra("bitArray"),0,getIntent().getByteArrayExtra("bitArray").length);       
        imgIT = findViewById(R.id.img_detail);
        imgIT.setImageBitmap(bitM);
    }

簡而言之,這是執行此操作的完美方法。 這是sender.class文件的代碼

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher;
Intent intent = new Intent();
Intent.setClass(<Sender_Activity>.this, <Receiver_Activity.class);
Intent.putExtra("Bitmap", bitmap);
startActivity(intent);

這是接收器類文件代碼。

Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");
ImageView viewBitmap = (ImageView)findViewById(R.id.bitmapview);
viewBitmap.setImageBitmap(bitmap);

無需壓縮。 而已

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM