我正在尝试使用代码打开相机拍照 它的工作适用于Android 6.0。 但在android版本7.0中它给出了错误
错误
file:///storage/emulated/0/04082017_1136image.jpg通过ClipData.Item.getUri()
暴露在app之外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);
}
答案 0 :(得分:2)
尝试这不是创造问题的意图,一旦你拍照并保存到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版本上完美运行。干杯!
答案 1 :(得分:1)
要在targetSdkVersion 24或更高版本中打开相机,我们必须使用FileProvider类来访问文件。
请参阅此回答Open Camera in Nougut
答案 2 :(得分:-1)
试用此代码
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);
}
}
}