Picasso library does not load images from SD card on Android

两盒软妹~` 提交于 2019-12-21 04:27:21

问题


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"/>

回答1:


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.



来源:https://stackoverflow.com/questions/31270090/picasso-library-does-not-load-images-from-sd-card-on-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!