美好的一天 我试图创建捕获图像的应用程序,然后在gridview中显示它 但是当我点击按钮开始捕获时,我会出现此错误。
logcat的:
java.lang.SecurityException: Permission Denial: starting Intent {
act=android.media.action.IMAGE_CAPTURE flg=0x3
cmp=com.android.camera2/com.android.camera.CaptureActivity
clip={text/uri-list
U:file:///storage/emulated/0/Pictures/MyInvoice/IMG_20160223_032401.jpg}
(has extras) } from ProcessRecord{8f9b88f
1356:com.example.labon.invoicemanger/u0a62} (pid=1356, uid=10062) with
revoked permission android.permission.CAMERA
at android.os.Parcel.readException(Parcel.java:1599)
我的代码:
myLists = new ArrayList<Images>();
adapter = new ImageListAdapter(getActivity(), R.layout.img_list_view, myLists);
Button myButton = (Button) view.findViewById(R.id.camerabutton);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);// create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); // start the image capture Intent
}
});
myGridView = (GridView) view.findViewById(R.id.gridView);
myGridView.setAdapter(adapter); /**
* Create a file Uri for saving an image or video
*/
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
/**
* returning image /
*/
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
// if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
//return null;
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
//file path of captured image
String filePath = cursor.getString(columnIndex);
//file path of captured image
File f = new File(filePath);
String filename = f.getName();
cursor.close();
//Convert file path into bitmap image using below line.
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
//put bitmapimage in your imageview
ImageView imageView = (ImageView) view.findViewById(R.id.imageListView);
imageView.setImageBitmap(BitmapFactory.decodeByteArray(images.getImageBlob(), 0, images.getImageBlob().length));
//newImageView.setImageBitmap(yourSelectedImage); }
if (resultCode == getActivity().RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getActivity(), "User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getActivity(), "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
} }
答案 0 :(得分:5)
你在棉花糖上运行吗?因为如果您是,则权限结构已更改。您应该阅读文档@ http://developer.android.com/training/permissions/index.html。
用户必须明确授予权限。
答案 1 :(得分:-1)
您需要向androidmanifest.xml添加用户权限
<uses-permission android:name="android.permission.CAMERA" />
如果您需要将图像存储到存储空间,则必须添加:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />