1

我正在创建一个相机应用程序,并在主要活动中检索相机预览位图并保存到内部存储。在意图到另一个类之后,我想通过获取位图的路径来显示图像。但是在拍照后第一次显示图片很好,但有时不显示图像并给出以下错误

"Resolve Uri failed on bad bitmap Uri : data/data/com.camera.app/files/287372639100.jpg"


主类

                //IMG name
                mName = System.currentTimeMillis();
                dTime  = mName;

                try {
                    //get bitmap & save into internal storage
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = IN_SAMPLE_SIZE;
                    bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);

                    try {
                        Uri uri = Uri.parse(dTime+".jpg");
                        File f = new File(uri.toString());
                        ExifInterface exif = new ExifInterface(f.getPath());
                        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{
                            angle = 0;
                        }

                        Matrix mat = new Matrix();
                        mat.postRotate(angle);
                        //get rotated bitmap
                        Bitmap correctBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mat, true);                 

                        //save bitmap to internal storage
                        FileOutputStream fos = openFileOutput(dTime+".jpg", Context.MODE_PRIVATE);
                        correctBmp.compress(CompressFormat.JPEG, 90, fos);
                        correctBmp = Bitmap.createScaledBitmap(bitmap, 380, 600, false);
                        fos.close();

                        bitmap.recycle();
                        correctBmp.recycle();
                    }
                    catch (IOException e) {
                        Log.w("TAG", "-- Error in setting image");
                    }   
                    catch(OutOfMemoryError oom) {
                        Log.w("TAG", "-- OOM Error in setting image");
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

                // intent send data
                Intent intent = new Intent(MainActivity.this,PhotoClass.class);
                intent.putExtra("IMG_NAME", dTime);
                startActivity(intent);
                finish();

                Log.d(TAG, "onPictureTaken - bitmap sending..");

摄影课

        //get intent
        Intent intent = getIntent();
        imgName = intent.getLongExtra("IMG_NAME", 0);
        uri = Uri.parse(getFilesDir().getPath() + "/"+imgName+".jpg");

        if(imgName != 0){
            mImagePreView.setImageURI(uri);
        }

需要帮助来解决这个问题。

4

0 回答 0