Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picasso library does not load images from SD card on 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"/>
like image 720
pats Avatar asked Jul 07 '15 13:07

pats


1 Answers

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.

like image 157
Coas Mckey Avatar answered Nov 15 '22 07:11

Coas Mckey