Android开发在Activity外申请权限调用相机打开相册
<p><span style="font-size: 18pt">问题描述:</span></p><p><span style="font-size: 18pt"> <span style="font-size: 15px">最近在项目中遇到一个需要调用相册和打开相机的需求,但是,在Android 6.0以后,调用相册属于危险权限,需要开发者动态获取,这就意味着我们申请权限是与Activity绑定的,但如果一个App中需要多个地方请求打开相册,那我们要每个地方都要写一遍打开相册的程序吗(当然你可以Ctrl c v),但是,这对于任何一个有追求的程序员来说,都是不恰当的,所以我们要定义一个公共接口,做到在任何一个需要调用打开相册的地方随时调用,增加了代码的复用性。好记性不如烂笔头,把这个过程记录下来,let's go!!</span></span></p>
<p><span style="font-size: 18pt">解决思路:</span></p>
<p><span style="font-size: 14px"> 既然申请权限是与Activity绑定的,那么我们就创建一个的Activity专门用于完成打开相册/相机,申请权限的任务,当其它Activity需要用到这个功能时,直接跳转到这个Activity,完成任务后,再返回照片的真实路径就行了,Ok,思路有了,话不多说,直接撸码。</span></p>
<p><span style="font-size: 14px">1. 首先创建一个Activity专门用于申请权限和打开相册相机的功能,这里我命名为 </span>SelectImageActivity</p>
<p>打开相机的程序如下:第一个方法是打开相机,第二个方法是创建一个图片文件,这种写法是Android官方写法,这里定义了一个 currentPhotoPath 用来保存图片的路径,后面需要返回给调用它的ativity</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 调用相机</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> selectImageFromCamera() {
Intent takePictureIntent </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Intent(MediaStore.ACTION_IMAGE_CAPTURE);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 初始化打开相机的意图</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> (takePictureIntent.resolveActivity(getPackageManager()) != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
File photoFile </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
photoFile </span>=<span style="color: rgba(0, 0, 0, 1)"> createImageFile();
} </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) {
e.printStackTrace();
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (photoFile != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
Uri photoURI </span>= FileProvider.getUriForFile(<span style="color: rgba(0, 0, 255, 1)">this</span>, "com.example.encryption.fileProvider"<span style="color: rgba(0, 0, 0, 1)">, photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, SELECT_FROM_CAMERA);
}
}
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建图片文件</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> File createImageFile() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException {
String timeStamp </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> SimpleDateFormat("yyyyMMdd_HHmmss").format(<span style="color: rgba(0, 0, 255, 1)">new</span> Date());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 文件名以时间命名</span>
String imageFileName = "JPEG_" + timeStamp + "_";<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 完善文件名</span>
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取文件存储路径</span>
File image = File.createTempFile(imageFileName, ".jpg"<span style="color: rgba(0, 0, 0, 1)">, storageDir);
currentPhotoPath </span>=<span style="color: rgba(0, 0, 0, 1)"> image.getAbsolutePath();
</span><span style="color: rgba(0, 0, 255, 1)">return</span> image; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回文件</span>
}</pre>
</div>
<p>接下来就是调用相册的代码了,前面提到过,调用相册需要动态申请权限,虽然参考Android官方提供的代码不申请也是可以正常打开的,但是获取不到图片的真实路径(可能是我技术不到位),这里就申请一次吧</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 询问用户是否授权</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> selectImageFromAlbum() {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 询问用户是否授予权限</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> (ContextCompat.checkSelfPermission(SelectImageActivity.<span style="color: rgba(0, 0, 255, 1)">this</span>, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=<span style="color: rgba(0, 0, 0, 1)"> PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(SelectImageActivity.</span><span style="color: rgba(0, 0, 255, 1)">this</span>, <span style="color: rgba(0, 0, 255, 1)">new</span> String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 请求权限</span>
} <span style="color: rgba(0, 0, 255, 1)">else</span> {<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 已经授予权限</span>
openAlbum();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 调用相册</span>
<span style="color: rgba(0, 0, 0, 1)"> }
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 判断用户是否授予权限</span>
<span style="color: rgba(0, 0, 0, 1)"> @Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onRequestPermissionsResult(<span style="color: rgba(0, 0, 255, 1)">int</span> requestCode, @NonNull @NotNull String[] permissions, @NonNull @NotNull <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">[] grantResults) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (requestCode == 1<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (grantResults.length > 0 && grantResults == PackageManager.PERMISSION_GRANTED) {<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 用户给予权限</span>
<span style="color: rgba(0, 0, 0, 1)"> openAlbum();
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
Toast.makeText(SelectImageActivity.</span><span style="color: rgba(0, 0, 255, 1)">this</span>, "请授予读取相册权限!"<span style="color: rgba(0, 0, 0, 1)">, Toast.LENGTH_SHORT).show();
}
}
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 从相册选取图片处理</span>
<span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String handleImage(Intent data) {
String imagePath </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
Uri uri </span>=<span style="color: rgba(0, 0, 0, 1)"> data.getData();
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (DocumentsContract.isDocumentUri(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">, uri)) {
String docId </span>=<span style="color: rgba(0, 0, 0, 1)"> DocumentsContract.getDocumentId(uri);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> ("com.android.providers.media.documents"<span style="color: rgba(0, 0, 0, 1)">.equals(uri.getAuthority())) {
String id </span>= docId.split(":");
String selection </span>= MediaStore.Images.Media._ID + "=" +<span style="color: rgba(0, 0, 0, 1)"> id;
imagePath </span>=<span style="color: rgba(0, 0, 0, 1)"> getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
} </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> ("com.android.providers.downloads.documents"<span style="color: rgba(0, 0, 0, 1)">.equals(uri.getAuthority())) {
Uri contentUri </span>= ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"<span style="color: rgba(0, 0, 0, 1)">), Long.parseLong(docId));
imagePath </span>= getImagePath(contentUri, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
}
} </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> ("content"<span style="color: rgba(0, 0, 0, 1)">.equalsIgnoreCase(uri.getScheme())) {
imagePath </span>= getImagePath(uri, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
} </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> ("file"<span style="color: rgba(0, 0, 0, 1)">.equalsIgnoreCase(uri.getScheme())) {
imagePath </span>=<span style="color: rgba(0, 0, 0, 1)"> uri.getPath();
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> imagePath;// 用户选择图片的真实地址
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获得相册中图片的真实路径</span>
<span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String getImagePath(Uri externalContentUri, String selection) {
String path </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
Cursor cursor </span>= getContentResolver().query(externalContentUri, <span style="color: rgba(0, 0, 255, 1)">null</span>, selection, <span style="color: rgba(0, 0, 255, 1)">null</span>, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (cursor != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (cursor.moveToNext()) {
path </span>=<span style="color: rgba(0, 0, 0, 1)"> cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> path;
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打开相册</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> openAlbum() {
Intent intent </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Intent("android.intent.action.GET_CONTENT"<span style="color: rgba(0, 0, 0, 1)">);
intent.setType(</span>"image/*"<span style="color: rgba(0, 0, 0, 1)">);
startActivityForResult(intent, SELECT_FROM_ALBUM); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 跳转意图</span>
}</pre>
</div>
<p>调用完相册和相机后需要返回到 SelectImageActivity进行处理,这里我们重写 onActivityResult 方法处理结果,为什么只需要处理相册的返回结果呢?因为相机拍摄图片的路径我们已经知道了,保存在 currentPhotoPath 了,所以这里只处理从相册中选择图片的地址</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Activity返回结果处理</span>
<span style="color: rgba(0, 0, 0, 1)"> @Override
</span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onActivityResult(<span style="color: rgba(0, 0, 255, 1)">int</span> requestCode, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
</span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onActivityResult(requestCode, resultCode, data);
String imagePath </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (resultCode == RESULT_OK) {<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回码为真</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> (requestCode ==<span style="color: rgba(0, 0, 0, 1)"> SELECT_FROM_ALBUM) {
</span><span style="color: rgba(0, 0, 255, 1)">assert</span> data != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
currentPhotoPath </span>=<span style="color: rgba(0, 0, 0, 1)"> handleImage(data);
}
returnImagePath(currentPhotoPath);
}</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
finish();</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果返回值不是RESULT_OK, 这种情况可能是用户放弃选择图片,此时直接结束该activity否则会显示当前activity</span>
<span style="color: rgba(0, 0, 0, 1)"> }
}</span></pre>
</div>
<p>现在我们已经能获取拍摄图片或者用户选择图片的真实地址了,那么我们需要把这个地址返回给调用它的Activity,代码实现如下</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> returnImagePath(String imgPath) {
Intent intent </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Intent();
intent.putExtra(</span>"imagePath"<span style="color: rgba(0, 0, 0, 1)">, imgPath);
setResult(RESULT_OK, intent);
finish();
}</span></pre>
</div>
<p>好了,基本完成了,现在我们可以获取到图片的真实地址并进行返回,但是还需要知道调用的Activity是要打开相册的还是打开相机,因此我们需要接收上一级Activity发送过来的信息,代码如下</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"> @Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCreate(Bundle savedInstanceState) {
</span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_image);
</span><span style="color: rgba(0, 0, 255, 1)">int</span> imageWhere = getIntent().getIntExtra("imageFromWhere", 0<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)"> (imageWhere) {
</span><span style="color: rgba(0, 0, 255, 1)">case</span> SELECT_FROM_CAMERA:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 拍摄新图片</span>
<span style="color: rgba(0, 0, 0, 1)"> selectImageFromCamera();
</span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">case</span> SELECT_FROM_ALBUM:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 从相册中选取</span>
<span style="color: rgba(0, 0, 0, 1)"> openAlbum();
</span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
}
}</span></pre>
</div>
<p>到这里,我们接口以及设置完毕了,但由于SelectImageActivity是个空白的Activity,跳转过程会有空白一闪而过,对于“高质量”程序员,这是不能忍受的,所以我这里把SelectImageActivity设置为透明,这样就不会有空白了,怎么设置呢?,只需要在AndroidManifest文件中把当前的Activity后面加上这句</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">activity </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="function.SelectImageActivity"</span><span style="color: rgba(255, 0, 0, 1)"> android:theme</span><span style="color: rgba(0, 0, 255, 1)">="@android:style/Theme.Translucent"</span><span style="color: rgba(0, 0, 255, 1)">/></span></pre>
</div>
<p>注意,如果你使用这种方法,SelectImageActivity不能继承AppCompatActivity,而是向我一样继承Activity,否则会报错,到这里SelectImageActivity的配置完成,下面我们需要调用一下,调用SelectImageActivity的Activity中的代码如下</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)"> </span></pre>
<pre>Intent intent = new Intent(EncryptionActivity.this, SelectImageActivity.class);<br><br><span style="color: rgba(0, 0, 255, 1)">case</span> R.id.tv_image_select_from_album:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 从相册中选取</span></pre>
<pre><em id="__mceDel"> intent.putExtra("imageFromWhere"<span style="color: rgba(0, 0, 0, 1)">, SELECT_FROM_ALBUM);
startActivityForResult(intent, </span>1<span style="color: rgba(0, 0, 0, 1)">);<br></span><span style="color: rgba(0, 0, 255, 1)"> break</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">case</span> R.id.tv_image_select_from_camera:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 拍摄新图片<br></span> intent.putExtra("imageFromWhere"<span style="color: rgba(0, 0, 0, 1)">, SELECT_FROM_CAMERA);<br> startActivityForResult(intent, </span>1<span style="color: rgba(0, 0, 0, 1)">);<br></span><span style="color: rgba(0, 0, 255, 1)"> break</span>;</em></pre>
</div>
<p>ok,这里我们在需要调用的Activity中发送两个整形数字,来告诉SelectImageActivity我们是要调用相机还是打开相册,当SelectImageActivity完成并返回后,我们就能在onActivityResult中接收到图片的真实路径了</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"> @Override
</span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onActivityResult(<span style="color: rgba(0, 0, 255, 1)">int</span> requestCode, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
</span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onActivityResult(requestCode, resultCode, data);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (resultCode ==<span style="color: rgba(0, 0, 0, 1)"> RESULT_OK){
Bitmap bitmap </span>= BitmapFactory.decodeFile(data.getStringExtra("imagePath"<span style="color: rgba(0, 0, 0, 1)">));
mIvSelectImage.setImageBitmap(bitmap);
}
}</span></pre>
</div>
<p>这里,我直接把图片放在一个Image View中,全部过程完成!</p>
<p>Android开发新人,写的不好,大神勿喷,一个简单的案例,希望能够帮助到你</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/microDeLe/p/15086902.html
頁:
[1]