Search code examples
androidurifilenotfoundexceptionuniversal-image-loader

Force forward slash to // instead of automatically combine to / in string


Currently, I used Image Loader Universal library to load files from File path in sdcard.

I get issue with forward slash in Uri from File Path.

The correct string to load file is this one : content://media/external/images/media/24415

But when I put as these codes :

imageLoader.displayImage(
                Uri.fromFile(new java.io.File(FILE_PATH)).toString(),
                // .replace("%3A", "/" + java.io.File.separator),
                markableImageView, mDio,
                new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String imageUri, View view) {
                    }

                    @Override
                    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    }

                    @Override
                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                        if (loadedImage != null & view != null) {
                            ((ImageView) view).setImageBitmap(Bitmap.createScaledBitmap(
                                    loadedImage,
                                    mContext.getResources().getInteger(R.integer.width_file_view),
                                    mContext.getResources().getInteger(R.integer.height_file_view),
                                    false));
                        }
                    }
                }, new ImageLoadingProgressListener() {
                    @Override
                    public void onProgressUpdate(
                            String imageUri, View view, int current, int total) {
                    }
                });
    }

It automatically combine 2 forward slashes // to 1 forward slash / (it means content:/media/external/images/media/24415), so the Uri is not correct for library can read file, FileNotFoundException occured.

It is not I want, what I want is still keep //, not combined to / (it means content://media/external/images/media/24415)

People who know,

please help me, Thank you.


Solution

  • The problem I get is combine from // to / in string. So now there are ways to avoid like :

    1 - Split the string as following codes :

    // Should load file from sd card by parsing Uri
        String FILE_PATH = mAlAlbumItem.get(pos).getFilePath();
    
        // content://media/external/images/media/24415
    
        String[] SPLIT = FILE_PATH.split("//");
    
        if (FILE_PATH != null) {
            // Display image
    
            /**
             * Should use separate string to void wrap these words "//" to "/"
             */
            try {
                Enterprise.imageLoader.displayImage(
                        SPLIT[0] + "//" + SPLIT[1],
                        markableImageView, mDio,
                        new SimpleImageLoadingListener() {
                            @Override
                            public void onLoadingStarted(String imageUri, View view) {
                            }
    
                            @Override
                            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                            }
    
                            @Override
                            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                                if (loadedImage != null & view != null) {
                                    ((ImageView) view).setImageBitmap(Bitmap.createScaledBitmap(
                                            loadedImage,
                                            mContext.getResources().getInteger(R.integer.width_file_view),
                                            mContext.getResources().getInteger(R.integer.height_file_view),
                                            false));
                                }
                            }
                        }, new ImageLoadingProgressListener() {
                            @Override
                            public void onProgressUpdate(
                                    String imageUri, View view, int current, int total) {
                            }
                        });
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }
        }
    

    2 - Parse Uri by using Scheme.FILE.wrap(FILE_PATH) or Scheme.FILE.wrap(new File(FILE_PATH).getAbsolutePath()) instead of Uri.fromFile(new File(FILE_PATH))