在 android 中打开相机时出现问题

Issue in opening camera in android

我正在尝试使用代码打开相机拍照 它的工作正常到 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());
        }
    }
}

activity 结果码

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);
    }

试试这个代码

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);
    }  
} 

}

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

查看此答案

尝试这不是制造问题的意图,一旦您拍照并保存到 SD 卡并取回牛轧糖中的 uri 不同....

在您的应用程序上实现 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 版本(包括 Android Nougat)上完美运行。干杯!