将位图图像从一个活动传递到另一个活动

时间:2012-11-27 05:00:31

标签: android android-intent bitmap bitmapimage

在我的应用程序中,我显示来自图库的图像,一旦我选择一个图像,图像应该被发送到新活动,其中所选图像将被设置为背景。但是,我能够从图库中获取图像但是一旦我选择一个应用程序崩溃。提前谢谢

活动-1(图像显示在图库中,所选图像将发送到新活动)

public class Gallery extends Activity {

private static int RESULT_LOAD_IMAGE = 1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery);

        Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);


        buttonLoadImage.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {

                Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
    }



    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {






            Uri contentUri = data.getData();          
            String[] proj = { MediaStore.Images.Media.DATA };         
            Cursor cursor = managedQuery(contentUri, proj, null, null, null);         
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);         
            cursor.moveToFirst();         
            String tmppath = cursor.getString(column_index);           
            Bitmap croppedImage = BitmapFactory.decodeFile(tmppath);


            // Bitmap croppedImage = BitmapFactory.decodeFile(croppedImage);
            Intent intent = new Intent(Gallery.this,GesturesActivity.class);
            intent.putExtra("bmp",croppedImage);
            startActivity(intent);

            Log.v("sending image","sending image");


        }


    }
}

活动-1(XML)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    >
    <ImageView
            android:id="@+id/imgView"
            android:layout_width="fill_parent"
            android:layout_weight="1" android:layout_height="wrap_content"></ImageView>
    <Button 
            android:layout_height="wrap_content" 
            android:text="Load Picture" 
            android:layout_width="wrap_content" 
            android:id="@+id/buttonLoadPicture" 
            android:layout_weight="0" 
            android:layout_gravity="center"></Button>
</LinearLayout>

活动-2(应将所选图像设置为屏幕背景图像的活动)

  public class GesturesActivity extends Activity {


        private final int MENU_CAMERA = Menu.FIRST;


        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);


            Bitmap bmp = (Bitmap)this.getIntent().getParcelableExtra("bmp");
            BitmapDrawable background = new BitmapDrawable(bmp);
            getWindow().setBackgroundDrawable(background);  //background image of the screen



            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.advert);
            View view = new SandboxView(this, bitmap);

            setContentView(view);
        }



        public boolean onPrepareOptionsMenu(Menu menu) {
            menu.clear();

                menu.add(0, 11, 0, "Take Snapshot");

                    return super.onPrepareOptionsMenu(menu);
        }


        public boolean onOptionsItemSelected(MenuItem item) {

            return super.onOptionsItemSelected(item);
        }



    }

8 个答案:

答案 0 :(得分:18)

有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 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)将Bitmap传递给Intent并从bundle获取下一个活动中的位图,但问题是如果你的Bitmap / Image大小很大,那么下一个活动中的图像就不会加载。

答案 1 :(得分:6)

由于您要从图库中检索图像,为什么不将ID传递给下一个活动并检索该活动中的图像而不是传递图像?这将有助于你记忆力和表现。

答案 2 :(得分:5)

位图实现 Parcelable ,因此您可以始终在意图中传递它。尝试以下代码:

第一项活动。

Intent mIntent = new Intent(this, ActivityTwo.class);
mIntent.putExtra("bmp_img", bmp);

要获取目标活动的输出,请尝试:

第二项活动。

Bitmap mBitmap = (Bitmap) intent.getParcelableExtra("bmp_img");

总是使用 getParcelableExtra(“key”)来获取活动中传递的位图。

答案 3 :(得分:1)

我认为您可以将Bitmap定义为静态,并通过调用 classname.bitmap 来获取位图..并在下一个Activity中设置为背景

答案 4 :(得分:0)

IGP对此进行了总结,但在我看来,最有效的方法是将图像的URI传递给下一个活动,而不是Bitmap本身。我实际上不确定是否可以使用Intents将整个Bitmaps(或转换后的ByteArrays)数据从一个活动传递到另一个活动 - 我相信Bundle可以包含多少数据存在限制。

而是传递您用于在第一个活动中显示图像的参考。我假设你正在使用某种延迟加载?如果没有,我强烈建议你这样做。这样,您只需通过URI重新查询位图。

  

但是,我能够尽快从画廊中获取图像   选择一个应用程序崩溃

我仍然对这些问题如何到达而感到困惑。检查日志,也许你可以自己搞清楚。

答案 5 :(得分:0)

使用此功能,您可以将位图传递给另一个活动。

如果你使用drawable而不是首先将drawable转换为位图。

Bitmap bitmap = ((BitmapDrawable)d).getBitmap();

要使用intent将该位图传递给另一个活动,请使用下面的代码片段。

intent.putExtra("Bitmap", bitmap);

对于fetch,另一个活动中的位图意图使用此

Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");

关注此 Link for More Detail.

答案 6 :(得分:0)

我发现最简单(但绝对不是最优雅)的方法是使用静态类成员。例如:

class PassedData
{
    public Bitmap bm1, bm2, etc;

    private PassedData current;

    public static PassedData getCurrent() {return current;}

    public PassedData()
    {
        current = this;
    }
}

然后每个活动都可以引用PassedData.getCurrent()。

答案 7 :(得分:0)

<强>活动

在Activites

之间传递位图
Intent intent = new Intent(this, Activity.class);
intent.putExtra("bitmap", bitmap);

并在“活动课程”

Bitmap bitmap = getIntent().getParcelableExtra("bitmap");

<强>片段

在片段之间传递位图

SecondFragment fragment = new SecondFragment();
Bundle bundle = new Bundle();
bundle.putParcelable("bitmap", bitmap);
fragment.setArguments(bundle);

在SecondFragment内部接收

Bitmap bitmap = getArguments().getParcelable("bitmap");

如果您的活页夹事务失败,这意味着您将大型元素从一个活动转移到另一个活动,从而超出了活页夹事务缓冲区。

因此,在这种情况下,您必须将位图压缩为字节数组,然后在另一个活动中解压缩,就像这样

在FirstActivity中

Intent intent = new Intent(this, SecondActivity.class);

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPG, 100, stream);
byte[] bytes = stream.toByteArray(); 
intent.putExtra("bitmapbytes",bytes);

以及SecondActivity

byte[] bytes = getIntent().getByteArrayExtra("bitmapbytes");
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);