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

reading image file from internal storage path in android

我正在尝试从存储在内部存储器中的图像文件创建位图。我正在使用以下代码,当我检查位图时,它总是 returns null。请帮助并让我知道我缺少什么。

Note : I checked the image path it exists and following I got in 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));

}

用户关注方式:

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

您可以使用简单的方法实现此目的

Bitmap bitmap = BitmapFactory.decodeFile(filePath);
mImageView.setImageBitmap(bitmap);

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

使用这个通用方法:- 旋转、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;
}