从android中的内部存储路径读取图像文件

时间:2015-07-27 06:05:39

标签: android bitmap internal-storage

我正在尝试从存储在内部存储中的图像文件创建Bitmap。我使用以下代码,当我检查位图时,它总是返回null。请帮助,让我知道我错过了什么。

  

注意:我检查了它存在的图像路径,然后我进入了logcat   /data/data/mypackagename/files/foldername/Images/MDSs2LJgLP.png

代码:

if(catVo.getImage_path() != null)

{

    File location = new File(catVo.getImage_path());

                            FileInputStream fi = new FileInputStream(location.getAbsoluteFile());

    Bitmap bitmap = BitmapFactory.decodeStream(fi);                                         
    //Bitmap bitmap = BitmapFactory.decodeFile(path);   
    categoryImage.setImageBitmap(Utils.getRoundedBitmap(bitmap));

}

3 个答案:

答案 0 :(得分:0)

用户关注方法:

 public static Bitmap decodeSampledBitmap(String filePath)
{

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, options.outWidth,
    options.outHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}

答案 1 :(得分:0)

您可以使用简单方法

来实现这一目标
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
mImageView.setImageBitmap(bitmap);

有些时候BitmapFactory.decodeFile(filePath)会产生您需要处理的错误的OutOFMEmory错误。

答案 2 :(得分:0)

使用此通用方法: - 旋转,OutOfMemoryError,所需大小问题已修复/

private Bitmap getBitmap(String imagePath,int... desiredSize){
    try {
        ExifInterface exif = new ExifInterface(imagePath);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);
        int angle = 0;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            angle = 90;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            angle = 180;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            angle = 270;
        }else if (orientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL) {
            angle=360;
        }
        Matrix mat = new Matrix();
        mat.postRotate(angle);

        InputStream in = new FileInputStream(imagePath);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);
        in.close();
        in = null;

        int inWidth = options.outWidth;
        int inHeight = options.outHeight;

        in = new FileInputStream(imagePath);
        options = new BitmapFactory.Options();
        if(desiredSize.length>0){
            int desiredWidth=desiredSize[0];
            int desiredHeight=desiredSize[1];
            float tempDesiredSize[] = getScaleStr(inHeight, inWidth, desiredWidth, desiredHeight);
            desiredWidth=(int)tempDesiredSize[0];
            desiredHeight=(int)tempDesiredSize[1];
            options.inSampleSize = Math.max(inWidth/desiredWidth, inHeight/desiredHeight);
        }else{
            final int REQUIRED_SIZE = 1024;
            int scale = 1;
            while (true) {
                if (inWidth / 2 < REQUIRED_SIZE || inHeight / 2 < REQUIRED_SIZE) {
                    break;
                }
                inWidth /= 2;
                inHeight /= 2;
                scale *= 2;
            }
            options.inSampleSize = scale;
        }
        options.inDither=true;                     //Disable Dithering mode
        options.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
        options.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
        options.inTempStorage=new byte[15 * 1024 * 1024];
        options.inJustDecodeBounds=false;
        options.inPreferredConfig = Config.RGB_565;
        Bitmap srcBitmap = BitmapFactory.decodeStream(in, null, options);
        int w = srcBitmap.getWidth();
        int h = srcBitmap.getHeight();
        srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, w, h, mat, true);
        return srcBitmap;
    } catch(FileNotFoundException e){

    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
    }
    return null;
}