尝试使用FileProvider保存在内部文件夹时,相机无法正常工作

时间:2018-04-11 12:56:54

标签: android android-camera android-fileprovider

我正在尝试拍摄图片并将内部存储空间保存在特定应用图片的文件夹中。但由于某些原因,它不能正常工作 - 但在拍照后它没有恢复活动。以下是代码:

在Manifest中:

<application   .. >
   <activity .... >

   <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/shared_file_paths" />
    </provider>
</application>

在res / xml / shared_file_paths.xml中:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <external-path name="CameraPicture" path="data/data/com.simlelifesolution.mycameraapp/ColorApp_Images/" />
</paths>

在MainActivity.java中:

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            takePictureImageView = (ImageView)findViewById(R.id.take_picture_image_view);
            ........

            takePictureButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                       File outputImageFile = func_createFolder();

                        if(outputImageFile!=null) {
                            outputImgUri = getImageFileUriByOsVersion(outputImageFile);
                            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputImgUri);

                            startActivityForResult(cameraIntent, REQUEST_CODE_TAKE_PICTURE);
                        }
                        else{
                            Toast.makeText(TakePictureActivity.this, "Problem creating file",Toast.LENGTH_SHORT).show();}
                }    });
        }


         private File func_createFolder()
        {
            File outputImageFile = null;

            //File mydir  = getExternalCacheDir();
            File mydir = new File("data/data/com.simlelifesolution.mycameraapp/ColorApp_Images");

            if (!mydir.exists())
                {  if (!mydir.mkdirs()) { Log.d("TAG_LOG_TAKE_PICTURE", "failed to create directory");
                                            return null;}      }

            String imageFileName = "colorAppImg_" + System.currentTimeMillis() + ".png";
            outputImageFile = new File(mydir, imageFileName);

            try {outputImageFile.createNewFile();}
            catch(Exception exp){Log.d("TAG_LOG_TAKE_PICTURE", ""+exp);}

             return outputImageFile;

        }


        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            try { if (requestCode == REQUEST_CODE_TAKE_PICTURE) {
                    if (resultCode == RESULT_OK) {
                        ContentResolver contentResolver = getContentResolver();
                        InputStream inputStream = contentResolver.openInputStream(outputImgUri);
                        Bitmap pictureBitmap = BitmapFactory.decodeStream(inputStream);
                        takePictureImageView.setImageBitmap(pictureBitmap);
                    }   }
            }catch(FileNotFoundException ex)
                {    Log.e(TAG_LOG_TAKE_PICTURE, ex.getMessage(), ex);    }
        }


         private Uri getImageFileUriByOsVersion(File file)
        {
            Uri ret = null;

            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
                {  Context ctx = getApplicationContext();
                    ret = FileProvider.getUriForFile(ctx, getPackageName() + ".provider", file); }
            else
                ret = Uri.fromFile(file);

            return ret;
        }

但如果我通过更改func_createFolder()中的下面的代码在缓存目录中创建文件,那么它可以工作并在imageview中显示图像

File mydir = new File("data/data/com.simlelifesolution.mycameraapp/ColorApp_Images");

File mydir  = getExternalCacheDir();

但是,如果我不在缓存中创建它,而是在内部存储中创建,那么文件将使用filesize 0创建,并且应用程序会卡在相机中 - 不会返回活动以显示图像ImageView的。

0 个答案:

没有答案