【问题标题】:Issue in opening camera in android在android中打开相机的问题
【发布时间】:2017-08-04 06:51:41
【问题描述】:

我正在尝试使用代码打开相机拍照 它的工作正常到 android 6.0 。 但是在 android 7.0 版本中它给出了错误

错误

file:///storage/emulated/0/04082017_1136image.jpg 通过 ClipData.Item.getUri() 暴露在应用之外

private void takePhotoFromCamera()
{
    AnimateImageButton();
    boolean result = Utility.checkPermission(MainActivity.this);
    if (result) {
        try {
            _isOpenGallery = false;

            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
            File file = new File(Environment.getExternalStorageDirectory() + File.separator + timeStamp + "image.jpg");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
            startActivityForResult(intent, REQUEST_CAMERA);
            System.out.println("Hello >>>>>>>> : " + file.getAbsolutePath());
        }catch (Exception e)
        {

            Log.d("logforcamera",e.getMessage());
        }
    }
}

活动结果代码

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    File file = new File(Environment.getExternalStorageDirectory() + File.separator + timeStamp + "image.jpg");
    System.out.println("Helllloooo >>>>>>>>>>> : " + file.getAbsolutePath());
    Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), widthX, heightY);
    System.out.println("Bitmap : " + bitmap);
    if (bitmap != null)
    {
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
        showSelectedImage(bitmap);
    }

【问题讨论】:

标签: android camera android-7.0-nougat


【解决方案1】:

尝试一下,一旦您拍照并保存到 sd 卡并取回 uri 在 Nougat 中是不同的......

在您的应用程序上实现 FileProvider 非常容易。首先,您需要在 AndroidManifest.xml 中添加一个 FileProvider 标签,如下所示:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    <application
        ...
        <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/provider_paths"/>
        </provider>
    </application>
</manifest>

然后在res文件夹下的xml文件夹中创建provider_paths.xml文件。如果文件夹不存在,可能需要创建它。

res/xml/provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

完成! FileProvider 现在已声明并可以使用了。

最后一步是在 MainActivity.java 中更改下面的代码行

Uri photoURI = Uri.fromFile(createImageFile());

ri photoURI = FileProvider.getUriForFile(MainActivity.this,
            BuildConfig.APPLICATION_ID + ".provider",
            createImageFile());

然后....完成了!您的应用程序现在应该可以在包括 Android Nougat 在内的任何 Android 版本上完美运行。干杯!

【讨论】:

    【解决方案2】:

    要在 targetSdkVersion 24 或更高版本中打开相机,我们必须使用 FileProvider 类来访问文件。

    看到这个答案Open Camera in Nougut

    【讨论】:

      【解决方案3】:

      试试这个代码

      package edu.gvsu.cis.masl.camerademo;
      import android.app.Activity;
      import android.content.Intent;
      import android.graphics.Bitmap;
      import android.os.Bundle; 
      import android.view.View;
      import android.widget.Button;
      import android.widget.ImageView;
      
      public class MyCameraActivity extends Activity {
        private static final int CAMERA_REQUEST = 1888; 
        private ImageView imageView;
      
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          this.imageView = (ImageView)this.findViewById(R.id.imageView1);
          Button photoButton = (Button) this.findViewById(R.id.button1);
          photoButton.setOnClickListener(new View.OnClickListener() {
      
              @Override
              public void onClick(View v) {
                  Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                  startActivityForResult(cameraIntent, CAMERA_REQUEST); 
              }
          });
      }
      
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
          if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {  
              Bitmap photo = (Bitmap) data.getExtras().get("data"); 
              imageView.setImageBitmap(photo);
          }  
      } 
      

      }

      【讨论】:

        猜你喜欢
        • 2022-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-24
        • 2017-05-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多