如何将位图图像从本地存储设置到 NetworkImageView
How to set a bitmap image from local storage to 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
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()));
}
使用自定义图像加载器缓存
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
}
}
此代码未经测试。使用它需要您自担风险
我想将图像从存储设备设置到 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
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()));
}
使用自定义图像加载器缓存
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
}
}
此代码未经测试。使用它需要您自担风险