安卓權(quán)限申請(qǐng)操作簡(jiǎn)單例子
早在Android6.0(API23)的時(shí)代梦鉴,我們使用安卓的sd卡權(quán)限肤寝、定位權(quán)限、拍照權(quán)限等涉及到用戶隱私的(dangerous)敏感危險(xiǎn)的權(quán)限(你懂得)肯定是必須要?jiǎng)討B(tài)權(quán)限申請(qǐng)了,但像音量調(diào)節(jié)這一類(normal)正常權(quán)限就不受這個(gè)限制阀圾。
權(quán)限需要?jiǎng)討B(tài)獲取, 核心權(quán)限必須滿足. 標(biāo)準(zhǔn)流程:
image.png
今天咱們來(lái)從一個(gè)簡(jiǎn)單操作入手,了解一下動(dòng)態(tài)權(quán)限的申請(qǐng)操作狗唉。
目標(biāo):從安卓設(shè)備本地內(nèi)存中讀取一張圖片初烘,并顯示出來(lái)。
預(yù)期效果:
App圖標(biāo):
App圖標(biāo)
權(quán)限申請(qǐng)彈出窗:
權(quán)限申請(qǐng)彈出窗
點(diǎn)擊“開(kāi)始表演的效果”:
效果
看上去很demo吧,那接下來(lái)就來(lái)看看怎么申請(qǐng)的權(quán)限吧:
首先账月,我們得新建一個(gè)項(xiàng)目:
新建項(xiàng)目
自動(dòng)的為我們建立好了一個(gè)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="開(kāi)始表演"
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="開(kāi)始表演"
android:background="#81837d"
android:textColor="#ef6e1e"
android:textSize="15sp"
android:src="@mipmap/ic_launcher"
/>
</RelativeLayout>
</LinearLayout>
最后,在EatHotDog中開(kāi)始準(zhǔn)備權(quán)限申請(qǐng)等局齿。
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()) {//判斷是否有權(quán)限
init();//初始化控件
} else {
requestPermission();
}
}
/**
* 判斷是否有權(quán)限
* @return
*/
private boolean hasPermission() {
Log.i(TAG, "hasPermission: 判斷是否有權(quán)限");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
} else {
return true;
}
}
/**
* 請(qǐng)求權(quán)限
*/
private void requestPermission() {
Log.i(TAG, "requestPermission: 請(qǐng)求權(quán)限");
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);
//監(jiān)聽(tīng)btn
btnOn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.bt_on:
//執(zhí)行顯示圖片的操作
doShow();
break;
default:
Log.i(TAG, "onClick: ");
}
}
/**
* 開(kāi)始表演
*/
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;
}
}
最后附上一張資源圖:image.png
該文章及資源僅限學(xué)習(xí)用途剧劝。
附上源碼demo下載地址https://github.com/CodeLpea/EathotDog