Android调用系统图库获取图片

1、开发工具与关键技术:Eclipse、AndroidStudio
2、撰写时间:2020年05月28日

   在做移动开发相信很多人都会用到调用系统的图库获取图片吧,那么今天我跟大家讲讲如何调用系统的图库获取图片呢!由于本次的内容有点多,所以,分几个步骤吧!废话就不多说啦!避免浪费大家的时间,回归正题。请看代码

第一步:在build.gradle的文件下确保安卓版本是6.0以上(targetSdkVersion 23 以上)

    defaultConfig {
        targetSdkVersion 29
}

第二步:在manifests文件清单中添加权限

<!--允许程序设置内置sd卡的写权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--允许程序读写手机状态和身份-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<!--注意:这个的包名后面的单词一定要小写,否则会报错-->
<provider
      android:name="androidx.core.content.FileProvider"
      android:authorities="项目包名.fileprovider"
      android:exported="false"
      android:grantUriPermissions="true"> //这个一定要设置为true,否则不可以使用
       <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
             android:resource="@xml/file_paths" />
 </provider>

第三步:在res的目录下的xml中添加file_paths.xml文件(这个是固定写法)

   <?xml version="1.0" encoding="utf-8"?>
<resources>
    <paths>
        <root-path
            name="root"
            path="." />
        <cache-path
            name="image_cache"
            path="." />
    </paths>
</resources>

第四步:创建工具类

1、调用图库的MPermissionUtils 工具类

 package com.example.hote.util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 

* 6.0 运行时权限处理工具类。 */ public class MPermissionUtils { private static String TAG=MPermissionUtils.class.getSimpleName(); private static int mRequestCode = -1; public static void requestPermissionsResult(Activity activity, int requestCode , String[] permission, OnPermissionListener callback) { requestPermissions(activity, requestCode, permission, callback); } public static void requestPermissionsResult(android.app.Fragment fragment, int requestCode , String[] permission, OnPermissionListener callback) { requestPermissions(fragment, requestCode, permission, callback); } public static void requestPermissionsResult(Fragment fragment, int requestCode , String[] permission, OnPermissionListener callback) { requestPermissions(fragment, requestCode, permission, callback); } /** * 请求权限结果,对应onRequestPermissionsResult()方法。 */ public static void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (requestCode == mRequestCode) { if (verifyPermissions(grantResults)) { if (mOnPermissionListener != null) mOnPermissionListener.onPermissionGranted(); } else { if (mOnPermissionListener != null) mOnPermissionListener.onPermissionDenied(); } //调试打印授权情况 Log.d(TAG,"-----------------------------权限授权信息------------------------------------"); for(int i=0,len=permissions.length;i<len;i++){ String strText="未知情况"; switch (grantResults[i]){ case 0: strText="授权"; break; case 1: strText="被拒绝"; break; } Log.d(TAG,"权限:"+permissions[i]+" 的授权情况为: "+strText); } Log.d(TAG,"-----------------------------------------------------------------------------"); } } /** * 是否彻底拒绝了某项权限 */ public static boolean hasAlwaysDeniedPermission(@NonNull Context context, @NonNull String... deniedPermissions) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return false; boolean rationale; for (String permission : deniedPermissions) { rationale = ActivityCompat.shouldShowRequestPermissionRationale((Activity) getContext(context), permission); if (!rationale) return true; } return false; } /** * 显示提示对话框 */ public static void showTipsDialog(final Context context) { new AlertDialog.Builder(context) .setTitle("提示信息") .setMessage("当前应用缺少必要权限,该功能暂时无法使用。如若需要,请单击【确定】按钮前往设置中心进行权限授权。") .setNegativeButton("取消", null) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { startAppSettings(context); } }).show(); } //=============私有方法=================================== /** * 请求权限处理 * * @param object activity or fragment * @param requestCode 请求码 * @param permissions 需要请求的权限 * @param callback 结果回调 */ @TargetApi(Build.VERSION_CODES.M) private static void requestPermissions(Object object, int requestCode , String[] permissions, OnPermissionListener callback) { checkCallingObjectSuitability(object); mOnPermissionListener = callback; if (checkPermissions(getContext(object), permissions)) { if (mOnPermissionListener != null) mOnPermissionListener.onPermissionGranted(); } else { List<String> deniedPermissions = getDeniedPermissions(getContext(object), permissions); int len = deniedPermissions.size(); if (len > 0) { mRequestCode = requestCode; String[] strDeniedPermissions = deniedPermissions.toArray(new String[len]); if (object instanceof Activity) { ((Activity) object).requestPermissions(strDeniedPermissions, requestCode); } else if (object instanceof Fragment) { ((Fragment) object).requestPermissions(strDeniedPermissions, requestCode); } else if (object instanceof android.app.Fragment) { ((android.app.Fragment) object).requestPermissions(strDeniedPermissions, requestCode); } } } } /** * 获取上下文 */ private static Context getContext(Object object) { Context context; if (object instanceof Fragment) { context = ((Fragment) object).getActivity(); } else if (object instanceof android.app.Fragment) { context = ((android.app.Fragment) object).getActivity(); } else { context = (Activity) object; } return context; } /** * 启动当前应用设置页面 */ private static void startAppSettings(Context context) { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.parse("package:" + context.getPackageName())); context.startActivity(intent); } /** * 验证权限是否都已经授权 */ private static boolean verifyPermissions(int[] grantResults) { // 如果请求被取消,则结果数组为空 if (grantResults.length <= 0) return false; // 循环判断每个权限是否被拒绝 for (int grantResult : grantResults) { if (grantResult != PackageManager.PERMISSION_GRANTED) { return false; } } return true; } /** * 获取权限列表中所有需要授权的权限 * * @param context 上下文 * @param permissions 权限列表 * @return 需要授权的权限列表 */ private static List<String> getDeniedPermissions(Context context, String... permissions) { List<String> deniedPermissions = new ArrayList<>(); for (String permission : permissions) { if (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_DENIED) { deniedPermissions.add(permission); } } return deniedPermissions; } /** * 检查所传递对象的正确性 * * @param object 必须为 activity or fragment */ private static void checkCallingObjectSuitability(Object object) { if (object == null) { throw new NullPointerException("Activity or Fragment should not be null"); } boolean isActivity = object instanceof Activity; boolean isSupportFragment = object instanceof Fragment; boolean isAppFragment = object instanceof android.app.Fragment; if (!(isActivity || isSupportFragment || isAppFragment)) { throw new IllegalArgumentException( "Caller must be an Activity or a Fragment"); } } /** * 检查所有的权限是否已经被授权 * * @param permissions 权限列表 * @return 所有的权限是否已经被授权 */ private static boolean checkPermissions(Context context, String... permissions) { if (isOverMarshmallow()) { for (String permission : permissions) { if (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_DENIED) { return false; } } } return true; } /** * 判断当前手机API版本是否 >= 6.0 */ private static boolean isOverMarshmallow() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M; } public interface OnPermissionListener { void onPermissionGranted(); void onPermissionDenied(); } private static OnPermissionListener mOnPermissionListener; }

2、GetImagePath 的工具类

package com.example.hote.util;

import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;

public class GetImagePath {

    //  4.4以上  content://com.android.providers.media.documents/document/image:3952
//  4.4以下  content://media/external/images/media/3951
    @SuppressLint("NewApi")
    public static String getPath(final Context context, final Uri uri) {

        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

        // DocumentProvider
        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
            // ExternalStorageProvider
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                if ("primary".equalsIgnoreCase(type)) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }
            }
            // DownloadsProvider
            else if (isDownloadsDocument(uri)) {
                final String id = DocumentsContract.getDocumentId(uri);
                final Uri contentUri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

                return getDataColumn(context, contentUri, null, null);
            }
            // MediaProvider
            else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                Uri contentUri = null;
                if ("image".equals(type)) {
                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }

                final String selection = "_id=?";
                final String[] selectionArgs = new String[]{
                        split[1]
                };

                return getDataColumn(context, contentUri, selection, selectionArgs);
            }
        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {
            // Return the remote address
            if (isGooglePhotosUri(uri))
                return uri.getLastPathSegment();

            return getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }

    //Android 4.4以下版本自动使用该方法
    public static String getDataColumn(Context context, Uri uri, String selection,
                                       String[] selectionArgs) {

        Cursor cursor = null;
        final String column = "_data";
        final String[] projection = {
                column
        };

        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                final int index = cursor.getColumnIndexOrThrow(column);
                return cursor.getString(index);
            }
        } finally {
            if (cursor != null)
                cursor.close();
        }
        return null;
    }


    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is ExternalStorageProvider.
     */
    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is DownloadsProvider.
     */
    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is MediaProvider.
     */
    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is Google Photos.
     */
    public static boolean isGooglePhotosUri(Uri uri) {
        return "com.google.android.apps.photos.content".equals(uri.getAuthority());
    }
}

第五步:Activity的代码

public class InfoActivity extends AppCompatActivity 
 {
    private static final int IMAGE_REQUEST_CODE = 100;
    private static final int IMAGE_REQUEST_CODE_GE7 = 101;
    private static final int CAMERA_REQUEST_CODE = 104;
private static final int REQUEST_EXTERNAL_STORAGE_CODE = 200;

private File mGalleryFile;//存放图库选择是返回的图片

//相册的File对象
mGalleryFile=new File(path,IMAGE_GALLERY_NAME.jpg”)

//*********核心代码***********
 view.findViewById(R.id.tv_d_selectPhoto_pic)
      .setOnClickListener(new View.OnClickListener() {
     @Override
    public void onClick(View v) {
       //检查权限
     String[] strPermissions=new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE  //外部存储器写权限
        };
       MPermissionUtils.requestPermissionsResult(UserFragment.this, REQUEST_EXTERNAL_STORAGE_CODE, strPermissions, new MPermissionUtils.OnPermissionListener() {
     //权限通过的回调
        @Override
      public void onPermissionGranted() {
       //访问系统图库
       Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
       intent.addCategory(Intent.CATEGORY_OPENABLE);//设置打开文件的模式读取
       intent.setType("image/*");//告诉系统我要获取图片
       if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
       //Android7.0及以上
       Uri uriForFile= FileProvider.getUriForFile(mActivityContext,
              "com.example.hote.fileprovider",mGalleryFile);
       intent.putExtra(MediaStore.EXTRA_OUTPUT,uriForFile);
       intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
       intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
       //启动页面,设置请求码
         startActivityForResult(intent, IMAGE_REQUEST_CODE_GE7);
       }else{
        //Android7.0一下
       intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mGalleryFile));
        startActivityForResult(intent, IMAGE_REQUEST_CODE);
       }
          dialog.dismiss();//关闭dialog
          }
       //权限不通过的回调
        @Override
        public void onPermissionDenied() {
         //判断是否有永久拒绝的权限
        if (MPermissionUtils.hasAlwaysDeniedPermission(mActivityContext, strPermissions)) {
             MPermissionUtils.showTipsDialog(mActivityContext);
             } else {
               Toast.makeText(mActivityContext, "您拒绝了应用需要的权限,将无法在图库中选择头像。请再试一次,并同意权限。"
                     , Toast.LENGTH_SHORT).show();
                  }
             }
          });
        }
   });

//权限请求的回调
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        MPermissionUtils.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

//对uri进行获取
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && (data != null || requestCode == CAMERA_REQUEST_CODE)) {
            switch (requestCode) {
                case IMAGE_REQUEST_CODE://版本<7.0  图库返回
                    //获取图片的全路径
                    Uri uri = data.getData();
                    Log.e("ImagUri", uri.getPath());
                    //进行图像裁剪
                    sartPhotoZoom(uri);
                    break;
                case IMAGE_REQUEST_CODE_GE7://版本>= 7.0 图库返回
                    //获取文件路径
                    String strPath = GetImagePath.getPath(mActivityContext, data.getData());
                    if (strPath!=null) { 
                        File imgFile = new File(strPath);
                        //通过FileProvider创建一个content类型的Uri
                        Uri dataUri = FileProvider.getUriForFile(mActivityContext, "com.example.hote.fileprovider", imgFile);
                        Log.e("ImagUri", dataUri.getPath());
                        //进行图像裁剪
                        startPhotoZoom(dataUri);
                    } else {
                        Toast.makeText(mActivityContext, "选择图片失败", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case CAMERA_REQUEST_CODE://相机的返回
                    Uri inputUrl;
                    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
                        //Android7.0及以上
                        //通过FileProvider创建一个content类型的Uri                inputUrl=FileProvider.getUriForFile(mActivityContext,"com.example.hote.fileprovider",mCameraFile);
                    }else {
                        //Android7.0以下
                        inputUrl=Uri.fromFile(mCameraFile);
                    }
              } else {
            Toast.makeText(mActivityContext, "操作失败", Toast.LENGTH_SHORT).show();
        }
    }

第六: 运行结果

Android调用系统图库获取图片_第1张图片

    在这里还能遇见你,真好!说明你很有耐心,是一个想学习有追求的人。嘻嘻!不错哦!其实呢!到这里也算告一段落,现在我们已经拿到图片的uri。剩下的图片裁剪和上传,在这里:https://blog.csdn.net/weixin_43741599/article/details/106836462.

你可能感兴趣的:(java移动开发)