如何将位图图像从本地存储设置为NetworkImageView

时间:2015-04-10 09:11:48

标签: android android-volley networkimageview

我想将图像从存储设备设置为NetworkImageView。我在这里实施但它什么也没显示。在picturePath我得到图像路径但它没有显示。 1.我将位图图像从服务器设置为NetworkImageView。 2.然后我在那里提供编辑选项,我需要上传本地图像,但它不按照我的代码进行

这里我的代码是......

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));

    }

错误日志...

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

1 个答案:

答案 0 :(得分:3)

  

来自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()));

}

使用自定义图像加载器缓存

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
}
}

此代码未经过测试。使用它需要您自担风险