Skip to content

Commit

Permalink
预加载时检查权限。
Browse files Browse the repository at this point in the history
  • Loading branch information
teach committed Nov 20, 2019
1 parent 8e95cf4 commit f0bd1e9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.donkingliang.imageselectdemo;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

Expand All @@ -17,6 +21,7 @@
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private static final int REQUEST_CODE = 0x00000011;
private static final int PERMISSION_WRITE_EXTERNAL_REQUEST_CODE = 0x00000012;

private RecyclerView rvImage;
private ImageAdapter mAdapter;
Expand All @@ -38,8 +43,16 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.btn_only_take).setOnClickListener(this);
findViewById(R.id.btn_take_and_clip).setOnClickListener(this);

//预加载手机图片
ImageSelector.preload(this);
int hasWriteExternalPermission = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasWriteExternalPermission == PackageManager.PERMISSION_GRANTED) {
//预加载手机图片。加载图片前,请确保app有读取储存卡权限
ImageSelector.preload(this);
} else {
//没有权限,申请权限。
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_WRITE_EXTERNAL_REQUEST_CODE);
}
}

@Override
Expand All @@ -53,6 +66,26 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
}

/**
* 处理权限申请的回调。
*
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == PERMISSION_WRITE_EXTERNAL_REQUEST_CODE) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//预加载手机图片
ImageSelector.preload(this);
} else {
//拒绝权限。
}
}
}

@Override
public void onClick(View v) {
switch (v.getId()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,6 @@ public void onClick(DialogInterface dialog, int which) {
* 从SDCard加载图片。
*/
private void loadImageForSDCard() {
final long time = System.currentTimeMillis();
ImageModel.loadImageForSDCard(this, new ImageModel.DataCallback() {
@Override
public void onSuccess(ArrayList<Folder> folders) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.donkingliang.imageselector.model;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
Expand All @@ -20,6 +22,8 @@
import java.util.Collections;
import java.util.List;

import androidx.core.content.ContextCompat;

public class ImageModel {

/**
Expand All @@ -34,14 +38,23 @@ public class ImageModel {
*
* @param context
*/
public static void preload(final Context context) {
public static void preloadAndRegisterContentObserver(final Context context) {
isNeedCache = true;
if (observer == null) {
observer = new PhotoContentObserver(context.getApplicationContext());
context.getApplicationContext().getContentResolver().registerContentObserver(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, false, observer);
}
loadImageForSDCard(context, true, null);
preload(context);
}

private static void preload(final Context context) {
int hasWriteExternalPermission = ContextCompat.checkSelfPermission(context,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasWriteExternalPermission == PackageManager.PERMISSION_GRANTED) {
//有权限,加载图片。
loadImageForSDCard(context, true, null);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class ImageSelector {
* @param context
*/
public static void preload(Context context) {
ImageModel.preload(context);
ImageModel.preloadAndRegisterContentObserver(context);
}

/**
Expand Down

0 comments on commit f0bd1e9

Please sign in to comment.