【问题标题】:Passing Picture from one activity to another activity将图片从一个活动传递到另一个活动
【发布时间】:2014-02-20 07:03:51
【问题描述】:

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

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);
        }
});

我要查看的活动

Intent i = getIntent(); 
// Selected image id
int position = i.getExtras().getInt("s");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(imageAdapter.moodpic[position]);

【问题讨论】:

  • 你没有传递任何名为"s"的东西。
  • 那么问题出在哪里?
  • 我收到错误 null 异常无法运行活动

标签: android gridview android-activity android-adapter


【解决方案1】:

您尝试在此处使用错误的密钥获取 Extra getInt("s")

试试这个

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

【讨论】:

    【解决方案2】:

    你应该改变

    int position = i.getExtras().getInt("id" , 0);
    

    而不是这个

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

    这是因为您已将 "id" 作为键,所以每当您检索时,该键必须相同,默认情况下 int 的值为 0 em>

    【讨论】:

      【解决方案3】:

      我强烈推荐一种不同的方法。 如果您真的想这样做,这是可能的,但它会消耗大量内存并且速度也很慢。如果您的手机较旧且位图较大,则可能无法使用。例如,您可以将其作为额外内容传递

      intent.putExtra("data", bitmap)
      

      Bitmap 实现 Parcelable,所以你可以把它放在一个额外的地方。同样,一个包有putParcelable 如果你想在活动之间传递它,我会将它存储在一个文件中。这样效率更高,工作量也更少。您可以使用 MODE_PRIVATE 在您的数据文件夹中创建任何其他应用都无法访问的私有文件。

      【讨论】:

        【解决方案4】:

        首先将 Image 转换为字节数组,然后传入 Intent,在下一个活动中从 Bundle 中获取字节数组并转换为 Image(Bitmap) 并设置为 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 = 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 并从捆绑包中获取下一个活动中的位图,但问题是如果您的位图/图像大小当时很大,则图像不会在下一个活动中加载。

        【讨论】:

        • @JeremiahMe 检查我的答案。
        • 您也可以将其与其他选项一起使用
        猜你喜欢
        • 2015-12-18
        • 2012-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-16
        • 2018-03-06
        • 1970-01-01
        相关资源
        最近更新 更多