在android中创建app的缓存目录中的图像位图

时间:2015-01-07 13:04:40

标签: android caching bitmap android-bitmap

如何在android中创建位于app缓存目录中的图像位图?这是我到目前为止所尝试的,但位图始终为空。返回的位图不为空如果我尝试解码sdcard中的任何图像。如何为缓存目录的映像创建位图?请指导我。 (图像文件路径为“/data/data/app_pkg_name/cache/test.jpg”)

private  Bitmap decodeBitmapFromPath(String imagePath, int reqWidth, int reqHeight) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;

    BitmapFactory.decodeFile(imagePath, options);

    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    options.inJustDecodeBounds = false;
    Bitmap bmp = BitmapFactory.decodeFile(imagePath, options);

    return bmp;
}

1 个答案:

答案 0 :(得分:0)

以这种方式试试..

private  Bitmap decodeBitmapFromPath(String imagePath, int reqWidth, int reqHeight) 
{
FileInputStream in;
BufferedInputStream buf;
Bitmap bMap = null;
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;


options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

options.inJustDecodeBounds = false;

try {
        in = new FileInputStream(path);
        buf = new BufferedInputStream(in);
        bMap = BitmapFactory.decodeStream(buf);

        if (in != null) {
            in.close();
        }
        if (buf != null) {
            buf.close();
        }
    } catch (OutOfMemoryError e) {
        }

return bMap;
}