我正在创建一个应用。在这个应用程序中,我想打开我的相机,我想保存它。
所以我写了代码来做那个
takepic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count++;
String file = dir+count+".jpg";
File newfile = new File(file);
try {
newfile.createNewFile();
}
catch (IOException e)
{
}
Uri outputFileUri = Uri.fromFile(newfile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
});
并且我已将权限添加到清单文件
<?xml version="1.0" encoding="utf-8"?>
<uses-feature android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
但是当我尝试打开相机时,我收到了这个错误:
java.lang.SecurityException: Permission Denial:
为什么我在添加权限
后才会收到此消息这是我在日志中的错误
04-05 14:03:56.839 9562-9562/com.example.pavithraprabodha.selfimania E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pavithraprabodha.selfimania, PID: 9562
java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.android.camera/.Camera clip={text/uri-list U:file:///storage/emulated/0/Pictures/Selfimania/1.jpg} (has extras) } from ProcessRecord{420a1c9 9562:com.example.pavithraprabodha.selfimania/u0a58} (pid=9562, uid=10058) with revoked permission android.permission.CAMERA
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2658)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1507)
at android.app.Activity.startActivityForResult(Activity.java:3917)
at android.support.v4.app.ActivityCompatJB.startActivityForResult(ActivityCompatJB.java:30)
at android.support.v4.app.ActivityCompat.startActivityForResult(ActivityCompat.java:162)
at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:925)
at android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:1008)
at android.support.v4.app.Fragment.startActivityForResult(Fragment.java:940)
at android.support.v4.app.Fragment.startActivityForResult(Fragment.java:929)
at com.example.pavithraprabodha.selfimania.Add_new_fragement$2.onClick(Add_new_fragement.java:89)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:2)
如果您的compileSdkVersion小于23,在marshmallow之下,您只需要在Manifest中定义以下权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
如果您要保存图片或视频,请同时定义WRITE_EXTERNAL_STORAGE权限。
现在对于棉花糖,您必须动态定义权限,如下所示:
private static final int REQUEST_CAMERARESULT=201;
ibtn_cam.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(getActivity().checkSelfPermission(Manifest.permission.CAMERA)== PackageManager.PERMISSION_GRANTED){
///method to get Images
takeVideo();
}else{
if(shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)){
Toast.makeText(getActivity(),"Your Permission is needed to get access the camera",Toast.LENGTH_LONG).show();
}
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.CAMERA}, REQUEST_CAMERARESULT);
}
}else{
takeVideo();
}
}
});
代替takeVideo你可以使用任何方法打开相机,我已经使用
public void takeVideo(){
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
// set video quality
// 1- for high quality video
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the video capture Intent
getParentFragment().startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(fileUri.getPath());
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
}
要将视频保存到图库,您必须使用sendBroadcast(mediaScanIntent)的新意图,如上所示
如果您正在编写所有这些代码,请不要忘记使用上下文将sendBroadcast(mediaScanIntent)作为getActivity()调用.sendBroadcast(mediaScanIntent)
检查一下:question
答案 1 :(得分:0)