[英]Picasso 2.5.2 does not load a local image
我正在使用 Picasso 2.5.2 将它与 OkHttp 2.5.0 一起使用onCreate()
中的代码如下所示:
File mypath = new File(getFilesDir().getAbsolutePath()+"/"+ date + "image.png");
if (mypath.exists())
{
Picasso.with(FullscreenImage.this).load(mypath).into(fullImage);
Toasty.info(FullscreenImage.this, mypath.toString()).show();
}
else
{
// Download the image from Firebase and create the image in the
// same directory and name
}
我在我的应用程序文件夹的files
夹中看到图像,但它没有显示在图像视图中。 图片大小:1.4 MB
我已通过将文件转换为位图来解决问题,然后在ImageView中使用它:
Bitmap bitmap = BitmapFactory.decodeFile(mypath.toString());
fullImage.setImageBitmap(bitmap);
如果有人像我一样卡住,我确实喜欢这个,查看毕加索网页( https://square.github.io/picasso/ ) - 资源加载部分,第 3 行:
String drPath = localData[position].getDrawablePath();
if(!drPath.equals(""))
{
var f = **new File(context.getFilesDir(), drPath)**;
try
{
Picasso.get()
.load(f)
.fit()
.centerCrop()
.into(holder.itemImage);
}
}
之前将文件从 Uri 复制到本地 position,如下所示:
try {
File f = new File(CategoryFragment.this
.getContext()
.getFilesDir()
, fileName);
f.setWritable(true, false);
OutputStream outputStream = new FileOutputStream(f);
byte[] buffer = new byte[1024];
int length = 0;
while((length=inputStream.read(buffer)) > 0) {
outputStream.write(buffer,0,length);
}
outputStream.close();
inputStream.close();
只注册文件的名称,如下所示: cmdEditVM.setDrawable(***fileName***, positionForNewDrawable);
它是通过以下内容从 Uri 中检索到的(取自 SO):
public String getFileName (Uri uri)
{
String result = null;
if (uri.getScheme().equals("content"))
{
Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
try
{
if (cursor != null && cursor.moveToFirst())
{
int columnIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
if (columnIndex >= 0) {result = cursor.getString(columnIndex);}
}
}
finally
{
if (cursor != null) {cursor.close();}
}
}
if (result == null)
{
result = uri.getPath();
int cut = result.lastIndexOf('/');
if (cut != -1)
{
result = result.substring(cut + 1);
}
}
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.