我添加了使用权限,包括WRITE_EXTERNAL_STORAGE,android.permission.CAMERA,READ_EXTERNAL_STORAGE到AndroidManifest.xml。
当我在Nexus6(API 24)中运行我的应用时,它向我发出以下错误:
java.io.IOException:Permission denied
这是我的代码:
/**
* A simple {@link Fragment} subclass.
*/
public class HomeProfileFragment extends Fragment {
public static final int REQUEST_IMAGE_CAPTURE = 1;
private String imageFilePath;
public HomeProfileFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home_profile, container, false);
///#################
String[] perms = {"android.permission.RECORD_AUDIO", "android.permission.CAMERA"};
File imageFile = null;
try{
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
imageFile = createImageFile();
imageFilePath = imageFile.getAbsolutePath();
} else {
/// Throw an error
}
}catch (IOException e){
e.printStackTrace();
}
int permsRequestCode = 200;
requestPermissions(perms, permsRequestCode);
///#################
if (imageFile != null){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
if(takePictureIntent.resolveActivity(getActivity().getPackageManager())!=null){
startActivityForResult(takePictureIntent,REQUEST_IMAGE_CAPTURE);
}
}
return view;
}
///##################
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){
switch(permsRequestCode){
case 200:
boolean audioAccepted = grantResults[0]== PackageManager.PERMISSION_GRANTED;
boolean cameraAccepted = grantResults[1]==PackageManager.PERMISSION_GRANTED;
break;
}
}
///##################
private File createImageFile()throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String fileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(fileName, ".jpg", storageDir);
return image;
}
private void addPhotoToGallery(String filepath){
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(filepath);
Uri uri =Uri.fromFile(f);
mediaScanIntent.setData(uri);
getActivity().sendBroadcast(mediaScanIntent);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Activity.RESULT_CANCELED){
} if (requestCode == Activity.RESULT_CANCELED){
addPhotoToGallery(imageFilePath);
}
}
///#################################
private boolean shouldAskPermission(){
return(Build.VERSION.SDK_INT> Build.VERSION_CODES.LOLLIPOP_MR1);
}
}
如何在打开之前使用权限相机运行时?
答案 0 :(得分:0)
您必须在清单
中声明用户权限<uses-feature android:name="android.hardware.camera2" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
它必须位于清单中,因此安装程序可以要求用户将这些权限授予应用程序。