【问题标题】:Android N FileUriExposedExceptionAndroid N FileUriExposedException
【发布时间】:2016-11-13 19:59:11
【问题描述】:

伙计们,我的应用程序使用股票相机拍照我使用以下代码拍照

public void takepic(View view) {

        TextView schtitle = (TextView) findViewById(R.id.Sitename);
        String schname = schtitle.getText().toString();
            String[] tokens = schname.split(" ");
            String timeStamp = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss").format(new Date());
            String imageFileName = tokens[0] + "-" + timeStamp + ".jpg";
            TextView myAwesomeTextView = (TextView)findViewById(R.id.filetext);

          //in your OnCreate() method
          myAwesomeTextView.setText(imageFileName);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    


                File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                String name = imageFileName;
                File file = new File(path, name );
                outputFileUri = Uri.fromFile(file);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
                startActivityForResult(intent, TAKE_PICTURE); 


       } 

除了牛轧糖之外,这对所有的都很好

我明白他们在棉花糖中所做的那样,你现在需要权限并且不能再使用文件 URI

我找不到任何关于如何在牛轧糖中拍照的示例代码,有人可以指出我如何修改代码以允许这种情况发生的正确方向吗?

任何帮助表示赞赏

标记

【问题讨论】:

标签: camera file-uri android-7.1-nougat


【解决方案1】:

问题是 file:// uri 不再被允许。

您必须使用 FileProvider 来代替它。

看这里:https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en

【讨论】:

    【解决方案2】:

    只是在这里引用以供参考。

    我们现在有一个更新的文档,说明如何使用带有文件 uri 的相机意图将拍摄的照片存储在 android 的训练部分下。

    https://developer.android.com/training/camera/photobasics.html

    【讨论】:

      【解决方案3】:
       public void openShare(View view) {
              // save bitmap to cache directory
              try {
                  bitmap = ((BitmapDrawable) iv.getDrawable()).getBitmap();
      
                  File cachePath = new File(getCacheDir(), "images");
      
                  if (!cachePath.exists())
                      cachePath.mkdirs();
      
                  FileOutputStream stream = new FileOutputStream(cachePath + "/" + imageName);
                  bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                  stream.close();
      
                  shareImage();
      
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      
          private void shareImage() {
              File imagePath = new File(getCacheDir(), "images");
              File newFile = new File(imagePath, imageName);
              Uri contentUri = MyFileProvider.getUriForFile(this, "com.example.kushaalsingla.contentsharingdemo", newFile);
      
              if (contentUri != null) {
                  Intent shareIntent = new Intent();
                  shareIntent.setAction(Intent.ACTION_SEND);
                  shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
                  shareIntent.setDataAndType(contentUri, getContentResolver().getType(contentUri));
                  shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
                  startActivity(Intent.createChooser(shareIntent, "Choose an app"));
              }
          }
      
      ################## 添加清单
             <provider
                  android:name=".MyFileProvider"
                  android:authorities="com.example.kushaalsingla.contentsharingdemo"
                  android:grantUriPermissions="true"
                  android:exported="false">
                  <meta-data
                      android:name="android.support.FILE_PROVIDER_PATHS"
                      android:resource="@xml/filepaths" />
              </provider>
      
      ################## filepaths.xml #######################
      <?xml version="1.0" encoding="utf-8"?>
      <paths xmlns:android="http://schemas.android.com/apk/res/android">
          <cache-path name="shared_images" path="images/"/>
      </paths>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-07
        • 2018-10-08
        • 2017-12-02
        • 2017-04-29
        • 1970-01-01
        • 1970-01-01
        • 2018-07-23
        • 1970-01-01
        相关资源
        最近更新 更多