【问题标题】:I am unable to send image from recyclerview.adapter to another activity我无法将图像从 recyclerview.adapter 发送到另一个活动
【发布时间】:2017-07-27 17:01:43
【问题描述】:
@Override
public void onBindViewHolder(final ViewHolder holder ,int position) {
    Glide.with(c)
            .load(images.get(position))
            .placeholder(R.mipmap.ic_launcher)
            .into(holder.img);
    holder.img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try{
                String fileName = "bitmap.png";
                FileOutputStream stream = c.openFileOutput(fileName,Context.MODE_PRIVATE);
                Intent showBigPicture = new Intent(c,showBigPicture.class);
                Bitmap bitmapImage = BitmapFactory.decodeFile(images.get(position));
                bitmapImage.compress(Bitmap.CompressFormat.PNG,100,stream);
                stream.close();
                bitmapImage.recycle();
                showBigPicture.putExtra("image",fileName);
                c.startActivity(showBigPicture);

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    });
}

这在 logCat 中显示“无法解码流:java.io.FileNotFoundException: android.support.v7.widget.AppCompatImageView{e22d977 V.ED..C. ...P.... 0,0- 540,890 #7f0b0061 app:id/img}:打开失败:ENOENT(没有这样的文件或目录)”

【问题讨论】:

  • 看起来您只是将文件名传递给下一个活动。您创建位图有什么原因吗?

标签: android android-recyclerview adapter


【解决方案1】:

我相信你想关注this answer 保存位图图像。我相信您收到 FileNotFoundException 的原因是因为您将 URI 提供给 decodeFile 函数尚不存在的文件,据我所知,该函数很可能是一个 URL。简而言之,保存位图:

  1. 创建一个新的File(filename)
  2. 在步骤 1 中的文件上使用 getName 解码文件
  3. File 创建FileOutputStream
  4. 将位图图像压缩成FileOutputStream

从我可以从您的问题中推测,您似乎正在 RecyclerView 中显示图像,并且当单击图像时,您想要打开另一个显示完整图像版本的活动。如果这接近您的用例,并且您正在使用 Glide,我建议您利用其内置的自动缓存功能来减少网络调用,而不是手动保存文件。

默认情况下,只要使用相同的文件名、路径或 URL 来获取每个 Glide.load(...) 上的图像,Glide 就会启用基于磁盘和内存的缓存。如果您想控制缓存的发生方式,请在每次加载图像时使用DiskCacheStrategy 枚举来控制它:

Glide.with(c)
        .load(images.get(position))
        .diskCacheStrategy(DiskCacheStrategy.SOURCE)  # Will cache the source downloaded image before any transformations are applied
        .placeholder(R.mipmap.ic_launcher)
        .into(holder.img);    

如果您出于其他原因仍想保存文件,请使用 SimpleTarget 而不是直接加载到 ImageView 中,如下所示:

Glide.with(c)
        .load(images.get(position))
        .diskCacheStrategy(DiskCacheStrategy.SOURCE)  # Will cache the source downloaded image before any transformations are applied
        .placeholder(R.mipmap.ic_launcher)
        .asBitmap()
        .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
                        holder.img.setImageDrawable(new BitmapDrawable(bitmap));
                        saveImage(bitmap); # This being an encapsulation of the steps outlined earlier
                    }
  });

【讨论】:

  • 是的,你是对的,我想在另一个活动中显示全尺寸图像,但我不知道如何使用 .diskCacheStrategy 来做到这一点。
  • 您只需在预览活动和全尺寸图像活动中使用 Glide 加载器上的该行。测试是否使用正确缓存的最佳方法是清除所有应用程序数据,加载预览活动以确保下载图像,然后关闭任何网络连接并尝试加载完整大小的活动。全尺寸活动中的图像仍应从缓存中保存的全尺寸图像加载
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-04
相关资源
最近更新 更多