Android getBitmap无法找到指定的图片

时间:2015-09-03 10:11:26

标签: android android-layout android-camera

我尝试使用相机的PictureCallback检索存储在我的存储空间中的图像但是getBitmap方法似乎无法找到图像并返回以下错误:< / p>

  

java.io.FileNotFoundException:没有内容提供者:   文件://storage/emulated/0/MyApp/IMG_20150903_154338.jpg

我向你保证,该图像在该位置可用。我检查了位置。

  

拍摄照片

     

通过回调获取图片

     

在ImageView中显示图像

图片已保存。没有显示。

这是我的代码:

captureImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mCamera.takePicture(null, null, new PictureCallback() {
                    @Override
                    public void onPictureTaken(byte[] data, Camera camera) {

File storageDir = new File(Environment.getExternalStorageDirectory() + "/", "MyApp");

                        if (!storageDir.exists()) {
                            storageDir.mkdirs();//directory created
                        }
                        // get the current timestamp
                        String timest = new SimpleDateFormat("yyyyMMdd_HHmmss")
                                .format(new Date());
                        //Create your picture file
                        File pictureFile;
                        pictureFile = new File(storageDir.getPath() + File.separator + "IMG_" + timest + ".jpg");


                        try {
                            FileOutputStream fos = new FileOutputStream(pictureFile);
                            fos.write(data);
                            fos.close();
                        } catch (Exception e) {
                            Log.d("MyApp", e.toString());
                        }

                        Uri uri = Uri.fromFile(pictureFile);

                        try {
                                Bitmap bmp = MediaStore.Images.Media.getBitmap(CameraActivity.this.getContentResolver() , Uri.parse("file://"+ storageDir.getPath() + File.separator + "IMG_" + timest + ".jpg"));

                                imageViewer = (ImageView) findViewById(R.id.imageViewer);
                                imageViewer.setImageBitmap(bmp);
                            } catch (Exception e) {
                                Log.d("MyApp", e.toString());
                            }

1 个答案:

答案 0 :(得分:1)

您是否已将从外部存储读取的权限添加到androidmanifest.xml?

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>