显示来自东方卡的图像给出了 java.lang.nulPointer 异常

发布于 2025-01-06 09:13:55 字数 1380 浏览 1 评论 0原文

我有这个片段可以从 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);}

我已经尝试了许多其他解决方案,但即使更改图像文件,我仍然遇到相同的问题。当我更改文件名以给出错误的文件名时,它告诉我找不到文件:(

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

i have this snippet to retieve an image from an sd car card but all i get is 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);}

I have tried many oder solutions but still, i always have the same problem even when i change the image file. And when i change the file name to give a false one, it tells me that file not found :(

can anyone help?? i've seen others having the same problem but didn't see anysolution that worked for me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚笙 2025-01-13 09:13:55

我尝试过,这对我有用:

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

I tried it and this works for me:

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

If you have more problems you may want to the state of the external storage:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文