Picasso library does not load images from SD card on Android - android

I take a file from path from image gallery and try to load it a image view as follows. File path is: /storage/sdcard0/DCIM/Camera/1436267579864.jpg. I also tried passing Uri I also have read privileges to SD card.
It ends up in onError() method. However similar method works fine for web urls. How can I resolve this?
private void getImage(File file) {
if(file.exists()) {
Picasso.with(activity)
.load(file)
.error(R.drawable.noimage)
.into(imgPreview, new Callback() {
#Override
public void onSuccess() {
if (progressBar != null && imgPreview != null) {
imgPreview.setVisibility(View.VISIBLE);
imgPreview.setTag("loaded");
progressBar.setVisibility(View.GONE);
}
}
#Override
public void onError() {
if (progressBar != null && imgPreview != null) {
imgPreview.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
}
}
});
}
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Though it is too late , but I stuck into same problem, so I solved in the following way . Just using and appending "file://" in the starting of path. take a look at this:
Picasso.with(context) //
.load("file://" +myFilePath) //
.error(R.mipmap.error)
.placeholder(R.mipmap.ic_launcher)
.fit()
.tag(MyActivity.this) //
.into(imageView, new ImageLoadedCallback(progressBar) {
#Override
public void onSuccess() {
progressBar.setVisibility(View.GONE);
}
#Override
public void onError() {
Log.d("Picasso Error", "Error");
}
});
This solves my problem . Just answering so that if some one fall in the same problem and came here for solution then he may get his problem solved through this.

Related

Unable to download image using Picasso on Android

I'm trying to download the following image using Picasso https://s3-media4.fl.yelpcdn.com/bphoto/94E7Ti0RTDbA6mGotZw5DA/o.jpg
I can see it fine in a browser. However when I attempt to download it using Picasso, I get an error (a breakpoint in my onError() method gets hit).
This is an extract of my code:
final RequestCreator rc = with(context).load(fullImagePath);
if (fit != null && fit) {
rc.fit();
}
// If no callback listener exists, create one.
if (callbackListener == null) {
callbackListener = new Callback() {
#Override
public void onSuccess() {
L.p("onSuccess retrieving " + fullImagePath);
}
#Override
public void onError() {
// Something went wrong
L.p("Error retrieving " + fullImagePath);
}
};
}
rc.into(fImageView, callbackListener);
I found this: https://github.com/square/picasso/issues/500 however it's a bit dated and the OkHttpClient class no longer has the setProtocols() method.
Thanks!
Inserting that link as "http" instead of "https" will work.
The below code works for me for both http and https links.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
String https = "https://s3-media4.fl.yelpcdn.com/bphoto/94E7Ti0RTDbA6mGotZw5DA/o.jpg";
String http = "http://www.sunseed.org.uk/projectpack/files/2014/05/Library_1400_800-1200x686.jpg";
Picasso.with(this)
.load(https)
.placeholder(R.drawable.me)
.into(imageView);
}
Also be sure you write the below permission in manifest file:
<uses-permission android:name="android.permission.INTERNET" />
also write the following in build.gradle file
implementation 'com.squareup.picasso:picasso:2.5.2'

Image are not displaying from Cache memory using Picasso Library

I am using Picasso library for image download and displaying it in imageView. This library also store images in cache and memory. When my internet is turn on, i am able to view images on imageView. So i think, it should also be store in cache or file memory. Now my internet is turnOFF, but it doest not display to images. kindly have a look.
Picasso.with(context)
.load(url) .placeholder(R.drawable.defaultimg)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(holder.imageview2, new ImageLoadedCallback(holder.loadingBar) {
#Override
public void onSuccess() {
if (holder.loadingBar != null) {
holder.loadingBar.setVisibility(View.GONE);
}
}
#Override
public void onError(){
holder.loadingBar.setVisibility(View.VISIBLE);
Picasso.with(context)
.load(url) .placeholder(R.drawable.defaultimg)
.into(holder.imageview2, new ImageLoadedCallback(holder.loadingBar) {
#Override
public void onSuccess() {
if (holder.loadingBar != null) {
holder.loadingBar.setVisibility(View.GONE);
}
}
#Override
public void onError() {
if (holder.loadingBar != null) {
holder.loadingBar.setVisibility(View.GONE);
}
}
});
}
});
Finally I resolved that issue. Thanks #dev.bmax
image url was not correct. There is bug in Picasso. If we have url like as
https://i.ytimg.com/vi/DMVEcfQmPOs/maxresdefault.jpg?500|700
Picasso is able to displaying image when internet TURN ON
but if we TURN OFF to internet, it does not decode the url. and not display the image also.
We have to remove ?500|700 then i was able to view image in OFFLine mode also.
//url.substring(0,url.indexOf("?"))
https://i.ytimg.com/vi/DMVEcfQmPOs/maxresdefault.jpg
Thanks!

Downloading image from firebase storage using picasso: out of memory exception

I am working on an android app which needs to download images from firebase backend but after downloading and showing 5 to 6 images in my recyler view, out of memory exception is thrown.
I have used an image compressing library due to which the size of each image is nearly 300 to 400 KB.
I have added <application
android:largeHeap="true" but still there is same issue
My code :
//Retriving image from picasso
Picasso.Builder builder = new Picasso.Builder(c);
builder.listener(new Picasso.Listener() {
#Override
public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
Toast.makeText(c,exception.getMessage(),Toast.LENGTH_LONG).show();
}
});
Picasso pic = builder.build();
pic.load(currentPost.getDownloadlinkDB()).into(((MyViewHolder_Image) holder).imageView, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Toast.makeText(c, "Problem in downloading image from server", Toast.LENGTH_SHORT).show();
}
});
Snapshot of the exception
How to solve this problem?
Add this to your Manifest file. Inside application tag
<application
android:largeHeap="true"

When I download my app from Play Store, it cannot access Internet, why?

I developed an app, all of time I used real device(samsung note 5), sometimes used emulator as test device.Today I upload the app to play store. As explained in caption, when I or my friends download the app and run it, it cannot get data from internet and adview is blank. But all of developing time I didnt face this situation. I dont understand.
First photo, real device connected to computer with cable, I run app by Android Studio, apk is sent Android studio to my real device.Close and run it again, close and run it again. Result is the same. Everything is ok.
https://i.stack.imgur.com/CQgOJ.jpg
Second photo, downloaded from play store and run app. All texts and balls are empty.
https://i.stack.imgur.com/7azlc.png
Manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CLEAR_APP_CACHE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.android.vending.BILLING" />
Spinner Item Selected (No toast mesaage appear in running app)
private AdapterView.OnItemSelectedListener spinnerOnItemSelectedListener = new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
mDrawDateSpinnerPosition = position;
final String drawDate = parent.getItemAtPosition(position).toString();
if (NetworkUtil.getConnectivityStatus(getContext())) {
mProgressDialog = ProgressDialog.show(getContext(), getResources().getString(R.string.please_wait), getResources().getString(R.string.loading), true, false);
new Handler().post(new Runnable() {
#Override
public void run() {
try {
mVolleyUtil.makeRequestForLotto(
getContext(),
Constants.SAYISAL,
DateUtil.formattedDateToNewFormattedDate(drawDate, Constants.DD_MM_YYYY, Constants.YYYY_MM_DD),
createSuccessListener(drawDate),
createErrorListener(drawDate));
} catch (Exception e) {
Log.e("spinnerSelectedListener", e.getMessage());
}
mProgressDialog.dismiss();
}
});
} else {
ToastUtil.show(getContext(), R.string.no_internet_access, Toast.LENGTH_LONG);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
CreateErrorListener (No toast message appears in running app)
private Response.ErrorListener createErrorListener(final String drawDate) {
return new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
mVolleyUtil.getVolleyController().cancelPendingRequests(this);
mResult = null;
mProgressDialog.dismiss();
if (!NetworkUtil.getConnectivityStatus(getContext())) {
ToastUtil.show(getContext(), R.string.no_internet_access, Toast.LENGTH_LONG);
} else {
ToastUtil.show(getContext(), "Error occured, try again.", Toast.LENGTH_LONG);
}
}
};
}
VolleyRequest
mVolleyController.getRequestQueue().add(
new GsonRequest<>(Request.Method.GET, link + drawDate + ".json",
createErrorListener, LottoDTO.class, createSuccessListener));
Please give me an idea. What is happenning?
I found solution to my own question.
ıf minified is enabled, you must use #serializedname in pojo(model) class.
Like this:
public class LottoDTO implements Serializable {
#SerializedName("success")
private boolean success;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
}
So GSON knows how to match data between pojo and JSON object.
Solution from : https://stackoverflow.com/a/30002203/3605998
Now, everything is OK. Thanks.

Load Image from Firebase Storage with Picasso to ImageView in Infowindow, Picasso only shows placeholder

I try to load a image from my Firebase Storage with Picasso into an Imageview that is placed in a InfoWindowAdapter from a Marker.
I'm very desperate about it. The problem is Picasso only shows the placeholder Icon.
My code for the FirebaseStorageRefernce looks like this referring to this Firebase Blog post:
StorageReference load = getImage(id);
load.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
downloadUri = uri;
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.e("Failure",e.toString());
}
});
The downloadUri that I get is valid and works fine. Now I use this downloadUri for Picasso.
Picasso picasso = new Picasso.Builder(getApplicationContext()).listener(new Picasso.Listener() {
#Override
public void onImageLoadFailed(Picasso picasso, Uri uri, Exception e) {
e.printStackTrace();
}
}).build();
picasso.setLoggingEnabled(true);
picasso.load(downloadUri).placeholder(R.drawable.toxic_bait_icon).fit().into(thump, new MarkerCallback(marker));
In reference to an answer that I found here on StackOverFlow, I made a MarkerCallback Class:
public class MarkerCallback implements Callback {
com.google.android.gms.maps.model.Marker marker = null;
MarkerCallback(Marker marker)
{
this.marker = marker;
}
#Override
public void onError(){
Log.e(getClass().getSimpleName(), "Error loading thumbnail!");
}
#Override
public void onSuccess(){
Log.d("onSuccess","Picasso Callback");
if(marker != null && marker.isInfoWindowShown()){
Log.d("onSuccess","Picasso Callback in if");
marker.hideInfoWindow();
marker.showInfoWindow();
}
}}
You can see that I added some Log.d messages on the onSuccess method to track if it's called. But it looks like the onSucess method isn't called anytime because the Log.d message never appears in LogCat.
Also Picasso doesn't show any exception, errors, failures, nor a log even though I enabled it.
picasso.setLoggingEnabled(true);
The only thing that happens is that the ImageView in the Infowindow shows the Placeholder.
I thought maybe its because the image size too large to load, but the image I try to load is only 652.86 KB.
PS: Picasso also doesn't show the ribbons in the left corner to debug the cache behaviour. I try to activate with:
picasso.setIndicatorsEnabled(true);
I use Picasso 2.5.2. I have no idea what I'm doing wrong or how to debug this if Picasso doesn't show logs nor ribbons. I hope someone can help me. I need this feature so badly.
Addendum:
I have also check the permissions, referring to this post, they are OK.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
android:name="android.permission.INTERNET"/>
<uses-permission
android:name="android.permission.READ_SYNC_SETTINGS"/>
<uses-permission
android:name="android.permission.WRITE_SYNC_SETTINGS"/>
<uses-permission
android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Update
After some trial-and-error, I found out that Picasso shows the log, but i don't see it in logcat. After I restart my phone, I saw the log in the logcat after a while. The log disappears again, but this time even a restart doesn't help the, log doesn't reappear.
But I've also done some research and found another answer that refers to my problem.
So I changed my code but it doesn't help. The issue is still the same. Picasso shows only the placeholder.
Why don't you do everything at once? Inside the Firebase Storage getDownloadUrl() method:
StorageReference load = getImage(id);
load.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
// Pass it to Picasso to download, show in ImageView and caching
Picasso.with(context).load(uri.toString()).into(imageView);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle any errors
}
});
Using Kotlin extension functions to Task<Uri> as receiver:
fun Task<Uri>.loadIntoPicasso(imageView: ImageView){
addOnSuccessListener { Picasso.with(imageView.context).load(it).into(imageView) }
}

Categories

Resources