2016-04-26 19 views
0

私のアプリケーションには既存のFileがあり、オプションでBitmapに変換します。私はそのための方法を持っています。しかし、常にこのメソッドはnullを返し、ビットマップのプロパティを取得しようとすると、私はNullPointerExceptionをキャッチします。ここでは、コードです:Androidのファイルをビットマップに変換すると、常にnullが返されます。

public static Bitmap convertToBitmap(File file) { 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    options.inSampleSize = 4; 
    return BitmapFactory.decodeFile(file.getAbsolutePath(), options); 
} 

私はオプションを削除し、BitmapFactory.decodeFile(file.getAbsolutePath())を呼び出す場合、私はいくつかのデバイス上OutOfMemoryErrorを取得します。私はそれで何ができますか?

答えて

0

あなたはまだ問題がある場合は知っている以下のコード:)

final Uri uri= Uri.fromFile(new File(file)) 
    try{ 
     Bitmap selectedImageBitMap= MediaStore.Images.Media.getBitmap(this.getContentResolver(),uri); 
     //you have your image bitmap :) 
     } 
    catch (FileNotFoundException fileNotFoundException){ 
     Log.d("sandeep","could not find file"); 
     } 
    catch(IOException ioexception) { 
     Log.d("sandeep", "IOException"); 
     } 

レムを試すことができます:)ハッピーコーディング:)

+1

感謝を試してみてください。しかし、残念ながら、私はOutOfMemoryErrorをキャッチしました:( –

0

を支援するため、この

BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
Bitmap bitmap =BitmapFactory.decodeFile(file.getAbsolutePath(),bmOptions); 
bitmap =Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true); 
関連する問題