如何从sdcard将图像文件读入位图?
_path = Environment.getExternalStorageDirectory().getAbsolutePath();
System.out.println("pathhhhhhhhhhhhhhhhhhhh1111111112222222 " + _path);
_path= _path + "/" + "flower2.jpg";
System.out.println("pathhhhhhhhhhhhhhhhhhhh111111111 " + _path);
Bitmap bitmap = BitmapFactory.decodeFile(_path, options );
Run Code Online (Sandbox Code Playgroud)
我得到位图的NullPointerException.这意味着位图为空.但我有一个图像".jpg"文件存储在SD卡中作为"flower2.jpg".有什么问题?
我在使用新的Volley库实现Image缓存时遇到了麻烦.在演示文稿中,代码看起来像这样
mRequestQueue = Volley.newRequestQueue(context);
mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache());
Run Code Online (Sandbox Code Playgroud)
BitmapLruCache显然不包含在工具包中.知道如何实现它或指向一些资源?
http://www.youtube.com/watch?v=yhv8l9F44qo @ 14:38
谢谢!
我使用此代码将位图保存在外部存储中但如果它不存在则不会创建该文件夹:
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOutputStream = null;
File file = new File(path + "/Captures/", "screen.jpg");
try {
fOutputStream = new FileOutputStream(file);
capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream);
fOutputStream.flush();
fOutputStream.close();
MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
return;
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
return;
}
Run Code Online (Sandbox Code Playgroud)
如果不存在,如何将图像保存在新目录中,如果设备中有文件夹,则保存默认值?
我是新手,也可以在Volley和缓存上工作:P.虽然我已经阅读了许多与Volley图像缓存相关的文章和帖子,但我仍然不清楚使用Volley的图像缓存的最佳/首选方式.像磁盘缓存还是内存?Volley已经提供了哪些支持以及如何(与L1和L2缓存支持相关)?我在我的情况下使用NetworkImageView,填充列表视图,其中包含要从网络中提取的图像.提前致谢!
这与关于Volley Image缓存的这个问题有关.所以,现在我想实现DiskLruCache,但我不知道如何做到这一点.
我从github下载了Jar文件并将其添加到我的项目中.
接下来我该怎么办?如何更改Volley的现有代码并集成DiskLruCache?
现有代码:
初始化排球:
queue = Volley.newRequestQueue(getActivity());
imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(
10);
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
});
Run Code Online (Sandbox Code Playgroud)
从服务器获取响应并解析:
jsArrayRequest = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
if (Const.DEBUGGING) {
Log.d(Const.DEBUG,
"Response => " + response.toString());
Log.d(Const.DEBUG, "Length = " + response.length());
}
parseResponse(response, …Run Code Online (Sandbox Code Playgroud)