0

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
raghavan
  • 39
  • 10

1 Answers1

3

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

Community
  • 1
  • 1
  • I am getting confuse, can u edit my code as per above – raghavan Apr 10 '15 at 09:25
  • Thanks n i imported those library files, but my error is "The method fromFile(File) in the type Uri is not applicable for the arguments (String)" because picturePath is a String. – raghavan Apr 10 '15 at 09:50
  • I am struck in above error and any way Thanks a lot :) – raghavan Apr 10 '15 at 10:20
  • did u check i have updated answer after that? did it work for you? –  Apr 10 '15 at 10:22
  • The method setImageUrl(String, ImageLoader) in the type NetworkImageView is not applicable for the arguments (String, consumer_edit_profile.MyCache)....If i update like above i am getting this error.. – raghavan Apr 10 '15 at 10:24
  • now check the updated anwser, the error has to go now –  Apr 10 '15 at 10:32
  • Sorry I am getting errors Please check my error log :( – raghavan Apr 10 '15 at 10:58