'Picasso 2.5.2 does not load a local image
I am using Picasso 2.5.2 to use it with OkHttp 2.5.0
The code in onCreate()
is like below:
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
}
I see the image in the files
folder in my app folder but it does not display in the image view.
The image size: 1.4 MB
Solution 1:[1]
I have resolved my issue by turning the file into a bitmap then use it in the ImageView:
Bitmap bitmap = BitmapFactory.decodeFile(mypath.toString());
fullImage.setImageBitmap(bitmap);
Solution 2:[2]
If someone is stuck like me, I did like this, checking on the Picasso webpage ( https://square.github.io/picasso/ ) - section RESOURCE LOADING, line 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);
}
}
previously copied the file from Uri to local position like this:
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();
registered ONLY THE NAME OF THE FILE like this: cmdEditVM.setDrawable(***fileName***, positionForNewDrawable);
It had been retrieved from the Uri with the following (taken from 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;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Stef |