毕加索图像加载

6

我正在尝试使用Picasso 2.5.2从相机路径中加载图像到GridView中。

这是路径:storage/emulated/0/DCIM/Camera/IMG_20150822_133220.jpg

我已经尝试了Github仓库中提供的解决方案,但它并没有解决我的问题。

我尝试使用Picasso的Transformation,但是无法从相机路径加载图像。

我尝试了这个:

File imageFile = new File(data.path);
Picasso.with(mContext)
  .load(imageFile)
  .placeholder(R.drawable.default_error)
  .error(R.drawable.default_error)
  .resize(mItemSize, mItemSize)
  .centerCrop()
  .into(image);

请问您能否展示代码并查看logcat中是否有错误信息? - Miriana Itani
@Miriana Itani:问题已更新。我也尝试了使用uri、stringpath以及带有“file:///”前缀的两种方式,但都没有成功。我卡在这里了。 - Sagar Panwala
实际上手机型号并不重要。我已经尝试过三种不同的手机:三星SM-G355H(4.4.2)、HTC Desire 816G(4.4.2)和三星S3(4.2)。 - Sagar Panwala
是的,这就是路径。我已经检查过,并且使用以下代码进行了确认: File file = new File(filePath); if(file.exists()) - Sagar Panwala
不,它没有给我任何错误。它直接显示了ErrorPlaceholder。 - Sagar Panwala
显示剩余6条评论
3个回答

0

我之前也遇到了类似的问题,这是我解决的方法。我正在尝试截取手机屏幕的截图,然后将其保存为jpg文件在此文件路径下:

filepath: /data/data/com.example.simon.myapp/app_report/report.jpg

然而,Picasso根本没有加载它 - 它只是给了我一个空白屏幕。

所以当我截图时,我决定从文件名中删除.jpg,使文件路径类似于这样:

filepath: /data/data/com.example.simon.myapp/app_report/report

我随后使用以下代码访问并显示我的屏幕截图,没有遇到任何问题。
    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir("report", Context.MODE_PRIVATE);
    file = new File(directory, "/report");
    Log.e("filepath", file.getPath());
    Picasso.with(context).load(file).memoryPolicy(MemoryPolicy.NO_CACHE).into(screenshot);

我知道在未来的某个阶段你需要获取jpg文件扩展名,所以如果你需要获取“report.jpg”,只需使用以下方法重命名文件:

android, 如何重命名文件?

private boolean rename(File from, File to) {
    return from.getParentFile().exists() && from.exists() && from.renameTo(to);
} 

0

你必须将一个url传递到load()方法中。

File imageFile = new File(data.path);

 try{
        String url = imageFile.toURI().toURL().toString(); 
        Picasso.with(mContext)
               .load(url)
               .placeholder(R.drawable.default_error)
               .error(R.drawable.default_error)
               .resize(mItemSize, mItemSize)
               .centerCrop()
               .into(image);   
    }catch (MalformedURLException exeption){
        exeption.printStackTrace();
    }

0
不要使用硬编码路径来访问SD卡内容,因为它们在不同设备上不是通用的。
而应该使用`.getExternalStoragePublicDirectory()`和正确的两个参数`File`构造函数:
File f = new File(
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
        "Camera/IMG_20150822_133220.jpg");
Picasso.with(mContext).load(f)./* ..;

更新 哦,实际上除此之外,他们有一个相关的已知问题,那里有人说他们使用绝对路径解决了这个问题,所以你可以试试。

Picasso.with(context).load(new File(data.path).getAbsolutePath())...

这不是硬编码。我给了你在调试时获取的路径。 - Sagar Panwala
发现了一些东西,请看看我回答中的更新。 - Ivan Bartsov
Barstov。抱歉,即使使用绝对路径也无法正常工作。 - Sagar Panwala
嗯,好的,那我已经没有更多建议了。至少去在我提供的 Github 上为该 bug 添加你的评论或简单的 +1——这将更快地引起开发者的注意。 - Ivan Bartsov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接