从外部卡显示图像给出了java.lang.nullPointerException

时间:2012-02-18 02:09:50

标签: android bitmap

我有这个片段来从sd车卡中提取图像,但我得到的只是java.lang.nullPointerException:

try {

                     BitmapFactory.Options options = new BitmapFactory.Options();
                      options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                      options.inSampleSize = 2;
                      options.inTempStorage = new byte[16*1024];
                      options.inJustDecodeBounds = true;
                      Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().toString() + "/monImage.png", options);
                      image.setImageBitmap(bitmap);

                    //  selected_photo = (ImageView) findViewById(R.id.selected_photo);
                      /*String photoPath="/"+ Environment.getExternalStorageDirectory().toString() + "/monImage.png";
                      Uri photoUri = Uri.parse(photoPath);
                                              image.setImageBitmap(MediaStore.Images.Media.getBitmap(getContentResolver(),photoUri));*/


                  }
                  catch (Exception e){Log.d("merde>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "grrrrr " +e);}

我尝试了许多oder解决方案,但是,即使我更改了图像文件,我也总是遇到同样的问题。当我更改文件名以给出错误的文件名时,它会告诉我找不到文件:(

任何人都可以帮忙吗?我见过其他人有同样的问题,但没有看到对我有效的解决方案

1 个答案:

答案 0 :(得分:1)

我试过了,这对我有用:

Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/monImage.png");

如果您遇到更多问题,可能需要外部存储的状态:

boolean mExternalStorageAvailable, mExternalStorageWriteable;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state))
{   mExternalStorageAvailable = true;
    mExternalStorageWriteable = true;
}
else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) 
{   mExternalStorageAvailable = true;
    mExternalStorageWriteable = false;
}
else 
{   mExternalStorageAvailable = false;
    mExternalStorageWriteable = false;
}