先创建一个目录
// 创建目录 获得SD卡的实际路径。 AppDir=Environment.getExternalStorageDirectory().getAbsolutePath()+"/jinesc.zc/"; File dir = new File(AppDir); if (!dir.exists()) dir.mkdirs();
下面是调用相机保存文件的代码
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (intent.resolveActivity(getPackageManager()) != null) { ContentValues contentValues = new ContentValues(2); String filePath = AppDir+System.currentTimeMillis() + ".jpg";//要保存照片的绝对路径 contentValues.put(MediaStore.Images.Media.DATA, filePath); //保存再指定位置 //如果想拍完存在系统相机的默认目录,改为 //String filePath = System.currentTimeMillis() + ".jpg";//要保存照片的绝对路径 textview.setText(filePath); //contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, filePath); //保存再系统相册里 contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); Uri mPhotoUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues); intent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoUri); startActivityForResult(intent, 103); }
安卓6.0动态权限的获取
// 申请相机权限 if (ActivityCompat.checkSelfPermission(Assets_input.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { // 申请权限 if (ActivityCompat.shouldShowRequestPermissionRationale(Assets_input.this, Manifest.permission .CAMERA)) { Toast.makeText(this, "请至权限中心打开本应用的相机访问权限", Toast.LENGTH_SHORT).show(); } ActivityCompat.requestPermissions(Assets_input.this, new String[]{Manifest.permission.CAMERA}, com.dommy.qrcodelib.util.Constant.REQ_PERM_CAMERA); return; } // // 申请文件读写权限(部分朋友遇到相册选图需要读写权限的情况,这里一并写一下) if (ActivityCompat.checkSelfPermission(Assets_input.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { // 申请权限 if (ActivityCompat.shouldShowRequestPermissionRationale(Assets_input.this, Manifest.permission .WRITE_EXTERNAL_STORAGE)) { Toast.makeText(Assets_input.this, "请至权限中心打开本应用的文件读写权限", Toast.LENGTH_SHORT).show(); } ActivityCompat.requestPermissions(Assets_input.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, com.dommy.qrcodelib.util.Constant.REQ_PERM_EXTERNAL_STORAGE); return; } // 申请文件读写权限(部分朋友遇到相册选图需要读写权限的情况,这里一并写一下) if (ActivityCompat.checkSelfPermission(Assets_input.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { // 申请权限 if (ActivityCompat.shouldShowRequestPermissionRationale(Assets_input.this, Manifest.permission .WRITE_EXTERNAL_STORAGE)) { Toast.makeText(Assets_input.this, "请至权限中心打开本应用的文件读写权限", Toast.LENGTH_SHORT).show(); } ActivityCompat.requestPermissions(Assets_input.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, com.dommy.qrcodelib.util.Constant.REQ_PERM_EXTERNAL_STORAGE); return; }
AndroidManifest.xml <uses-permission android:name="android.permission.INTERNET" /> <!-- 网络权限 --> <uses-permission android:name="android.permission.VIBRATE" /> <!-- 震动权限 --> <uses-permission android:name="android.permission.CAMERA" /> <!-- 摄像头权限 --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.PERMISSIONS_STORAGE"/>
发表评论