安卓权限申请操作简单例子
早在Android6.0(API23)的时代,我们使用安卓的sd卡权限、定位权限、拍照权限等涉及到用户隐私的(dangerous)敏感危险的权限(你懂得)肯定是必须要动态权限申请了,但像音量调节这一类(normal)正常权限就不受这个限制。
权限需要动态获取, 核心权限必须满足. 标准流程:
今天咱们来从一个简单操作入手,了解一下动态权限的申请操作。
目标:从安卓设备本地内存中读取一张图片,并显示出来。
预期效果:
App图标:
权限申请弹出窗:
点击“开始表演的效果”:
看上去很demo吧,那接下来就来看看怎么申请的权限吧:
首先,我们得新建一个项目:
自动的为我们建立好了一个Activity了。
然后配置AndoroidManifest.xml为如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.eathotdog">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/eathotdog"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".EatHotDog">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
然后activity_eat_hot_dog.xml如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".EatHotDog">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/bt_on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始表演"
android:background="#c8ec8a"
android:textColor="#ef6e1e"
android:textSize="15sp"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_on"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="开始表演"
android:background="#81837d"
android:textColor="#ef6e1e"
android:textSize="15sp"
android:src="@mipmap/ic_launcher"
/>
</RelativeLayout>
</LinearLayout>
最后,在EatHotDog中开始准备权限申请等。
package com.example.administrator.eathotdog;
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Switch;
import java.io.File;
import java.io.FileInputStream;
import static android.content.ContentValues.TAG;
public class EatHotDog extends AppCompatActivity implements View.OnClickListener {
private static String TAG="EatHotDog";
private Button btnOn;
private ImageView imgeOn;
private Bitmap getLocalImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_eat_hot_dog);
if (hasPermission()) {//判断是否有权限
init();//初始化控件
} else {
requestPermission();
}
}
/**
* 判断是否有权限
* @return
*/
private boolean hasPermission() {
Log.i(TAG, "hasPermission: 判断是否有权限");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
} else {
return true;
}
}
/**
* 请求权限
*/
private void requestPermission() {
Log.i(TAG, "requestPermission: 请求权限");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Log.i(TAG, "requestPermission: ");
}
requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
private void init() {
btnOn=(Button)findViewById(R.id.bt_on);
imgeOn=(ImageView)findViewById(R.id.iv_on);
//监听btn
btnOn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.bt_on:
//执行显示图片的操作
doShow();
break;
default:
Log.i(TAG, "onClick: ");
}
}
/**
* 开始表演
*/
private void doShow() {
Log.i(TAG, "doShow: ");
//读取
getLocalImage=readBitmapFromFileDescriptor(Environment.getExternalStorageDirectory().getPath()+ File.separator+"eathotdog.jpg",300,300);
//显示
imgeOn.setImageBitmap(getLocalImage);
}
/**
* 读取本地文件的方法
*/
public static Bitmap readBitmapFromFileDescriptor(String filePath, int width, int height) {
try {
FileInputStream fis = new FileInputStream(filePath);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fis.getFD(), null, options);
float srcWidth = options.outWidth;
float srcHeight = options.outHeight;
int inSampleSize = 1;
if (srcHeight > height || srcWidth > width) {
if (srcWidth > srcHeight) {
inSampleSize = Math.round(srcHeight / height);
} else {
inSampleSize = Math.round(srcWidth / width);
}
}
options.inJustDecodeBounds = false;
options.inSampleSize = inSampleSize;
return BitmapFactory.decodeFileDescriptor(fis.getFD(), null, options);
} catch (Exception ex) {
Log.e(TAG, "readBitmapFromFileDescriptor: "+ex.getMessage() );
}
return null;
}
}
最后附上一张资源图:该文章及资源仅限学习用途。
附上源码demo下载地址https://github.com/CodeLpea/EathotDog