From f0bd1e96841ae272464ce398c5ed62c97e675580 Mon Sep 17 00:00:00 2001 From: teach Date: Wed, 20 Nov 2019 16:56:31 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A2=84=E5=8A=A0=E8=BD=BD=E6=97=B6=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E6=9D=83=E9=99=90=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../imageselectdemo/MainActivity.java | 37 ++++++++++++++++++- .../imageselector/ImageSelectorActivity.java | 1 - .../imageselector/model/ImageModel.java | 17 ++++++++- .../imageselector/utils/ImageSelector.java | 2 +- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/donkingliang/imageselectdemo/MainActivity.java b/app/src/main/java/com/donkingliang/imageselectdemo/MainActivity.java index 83cbf80..16d985d 100644 --- a/app/src/main/java/com/donkingliang/imageselectdemo/MainActivity.java +++ b/app/src/main/java/com/donkingliang/imageselectdemo/MainActivity.java @@ -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; @@ -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; @@ -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 @@ -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()) { diff --git a/imageselector/src/main/java/com/donkingliang/imageselector/ImageSelectorActivity.java b/imageselector/src/main/java/com/donkingliang/imageselector/ImageSelectorActivity.java index f2c30aa..59f2c9e 100644 --- a/imageselector/src/main/java/com/donkingliang/imageselector/ImageSelectorActivity.java +++ b/imageselector/src/main/java/com/donkingliang/imageselector/ImageSelectorActivity.java @@ -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 folders) { diff --git a/imageselector/src/main/java/com/donkingliang/imageselector/model/ImageModel.java b/imageselector/src/main/java/com/donkingliang/imageselector/model/ImageModel.java index a23990c..fe3f055 100644 --- a/imageselector/src/main/java/com/donkingliang/imageselector/model/ImageModel.java +++ b/imageselector/src/main/java/com/donkingliang/imageselector/model/ImageModel.java @@ -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; @@ -20,6 +22,8 @@ import java.util.Collections; import java.util.List; +import androidx.core.content.ContextCompat; + public class ImageModel { /** @@ -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); + } } /** diff --git a/imageselector/src/main/java/com/donkingliang/imageselector/utils/ImageSelector.java b/imageselector/src/main/java/com/donkingliang/imageselector/utils/ImageSelector.java index d2542bf..3d412ec 100644 --- a/imageselector/src/main/java/com/donkingliang/imageselector/utils/ImageSelector.java +++ b/imageselector/src/main/java/com/donkingliang/imageselector/utils/ImageSelector.java @@ -50,7 +50,7 @@ public class ImageSelector { * @param context */ public static void preload(Context context) { - ImageModel.preload(context); + ImageModel.preloadAndRegisterContentObserver(context); } /**