Search code examples
androidandroid-volleynetworkimageview

How to set a bitmap image from local storage to NetworkImageView


i want to set a image from storage device to NetworkImageView. Here i am implemented but it is displaying nothing. In picturePath i am getting image path but it is not displaying. 1. I am setting the bitmap image from server to NetworkImageView. 2. Then I am giving edit option there i need to upload local images but it is not going on as per my code

Here my code is...

if (requestCode == RESULT_LOAD_IMAGE_C && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        picturePath = cursor.getString(columnIndex);
        cursor.close();

        NetworkImageView logo = (NetworkImageView) findViewById(R.id.logo);
        logo.setImageBitmap(null);
        logo.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }

Error log...

04-10 16:26:12.779: E/Volley(6509): [574] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Bad URL /storage/extSdCard/DCIM/tiger.jpg
04-10 16:26:12.779: E/Volley(6509): java.lang.RuntimeException: Bad URL /storage/extSdCard/DCIM/tiger.jpg
04-10 16:26:12.779: E/Volley(6509):     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:127)
04-10 16:26:12.779: E/Volley(6509):     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:110)
04-10 16:26:12.779: E/Volley(6509): Caused by: java.net.MalformedURLException: Protocol not found: /storage/extSdCard/DCIM/tiger.jpg
04-10 16:26:12.779: E/Volley(6509):     at java.net.URL.<init>(URL.java:178)
04-10 16:26:12.779: E/Volley(6509):     at java.net.URL.<init>(URL.java:127)
04-10 16:26:12.779: E/Volley(6509):     at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:101)
04-10 16:26:12.779: E/Volley(6509):     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)
04-10 16:26:12.779: E/Volley(6509):     ... 1 more

Solution

  • Answered from here

    if (requestCode == RESULT_LOAD_IMAGE_C && resultCode == RESULT_OK && null != data) 
    {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
    
        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();
    
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        picturePath = cursor.getString(columnIndex);
        cursor.close();
    
        NetworkImageView logo = (NetworkImageView) findViewById(R.id.logo);
        //File pictureFile=new File(picturePath);
        logo.setImageUrl(picturePath, new ImageLoader(Volley.newRequestQueue(getApplicationContext()), new MyCache()));
    
    }
    

    Using custom imageloader cache

    public class MyCache implements com.android.volley.toolbox.ImageLoader.ImageCache {
    
    @Override
    public Bitmap getBitmap(String path) {
        if (path.contains("file://")) {
            return BitmapFactory.decodeFile(path);
        } else {
            // Here you can add an actual cache
            return null;
        }
    }
    @Override
    public void putBitmap(String key, Bitmap bitmap) {
        // Here you can add an actual cache
    }
    }
    

    This code isn't tested. use it at your own risk