Android 将图片从一个活动传递到另一个活动

Android 将图片从一个活动传递到另一个活动,android,gridview,android-activity,android-adapter,Android,Gridview,Android Activity,Android Adapter,当我在网格视图中单击一个项目并将其转到另一个活动并在那里查看时,我有一个网格视图我的代码如下: gridView.setAdapter(new ImageAdapter(this)); gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int posit

当我在网格视图中单击一个项目并将其转到另一个活动并在那里查看时,我有一个网格视图我的代码如下:

gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            // TODO Auto-generated method stub
             Intent i = new Intent(getApplicationContext(), MainActivity.class);
                // passing array index
                i.putExtra("id", position);
                startActivity(i);
        }
});

您试图在此处使用错误的键获得额外的
getInt(“s”)

试试这个

int position = getIntent().getIntExtra("id",0);

您试图在此处使用错误的键获得额外的
getInt(“s”)

试试这个

int position = getIntent().getIntExtra("id",0);

首先将图像转换为字节数组,然后传递到Intent,在下一个活动中,从Bundle中获取字节数组,并将其转换为图像(位图)并设置为ImageView

将位图转换为字节数组:-

   Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//    Bitmap bmp = BitmapFactory.decodeFile(path); You can use this also.
    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) 首先将图像保存到SD卡中,然后在下一个活动中将此图像设置到ImageView中


3) 将位图传递到Intent并在下一个活动中从bundle获取位图,但问题是,如果您的位图/图像大小较大,则不会在下一个活动中加载图像。

首先将图像转换为字节数组,然后传递到Intent,然后在下一个活动中从bundle获取字节数组并转换为图像(位图)并设置为ImageView

将位图转换为字节数组:-

   Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//    Bitmap bmp = BitmapFactory.decodeFile(path); You can use this also.
    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) 首先将图像保存到SD卡中,然后在下一个活动中将此图像设置到ImageView中


3) 将位图传递到Intent并在下一个活动中从bundle中获取位图,但问题是,如果您的位图/图像大小较大,则不会在下一个活动中加载图像。

我强烈建议使用不同的方法。 如果你真的想这样做,这是可能的,但它需要大量内存,而且速度也很慢。如果你有一部旧手机和一个大的位图,它可能不起作用。例如,你可以把它作为额外的传递

intent.putExtra("data", bitmap)

位图实现了Parcelable,因此您可以将其放入额外的文件夹中。同样,一个bundle具有
putParcelable
f您想要在活动之间传递它,我会将它存储在一个文件中。这对你来说效率更高,工作量更少。您可以使用MODE_private在数据文件夹中创建任何其他应用程序都无法访问的私有文件。

我强烈建议使用不同的方法。 如果你真的想这样做,这是可能的,但它需要大量内存,而且速度也很慢。如果你有一部旧手机和一个大的位图,它可能不起作用。例如,你可以把它作为额外的传递

intent.putExtra("data", bitmap)
位图实现了Parcelable,因此您可以将其放入额外的文件夹中。同样,一个bundle具有
putParcelable
f您想要在活动之间传递它,我会将它存储在一个文件中。这对你来说效率更高,工作量更少。您可以使用“私人”模式在数据文件夹中创建任何其他应用程序都无法访问的私人文件。

您应该使用

int position = i.getExtras().getInt("id" , 0);
而是这个

int position = i.getExtras().getInt("s");
这是因为您已将“id”作为一个键,因此无论何时检索,该键都必须相同,默认情况下,int的值为0

int position = i.getExtras().getInt("id" , 0);
而是这个

int position = i.getExtras().getInt("s");

这是因为您已将“id”作为一个键,因此无论何时检索,该键都必须相同,默认情况下int的值为0

您没有传递任何名为
“s”
的内容。那么问题出在哪里?我遇到了一个错误null异常无法运行活动您没有传递任何名为
“s”的内容
。那么问题出在哪里?我遇到了一个错误null异常无法运行activity@JeremiahMe检查我的答案。你可以将它与其他选项一起使用also@JeremiahMe检查我的答案。你也可以将它与其他选项一起使用