Android調用系統圖庫獲取圖片的方法
本文實例為大傢分享瞭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; /** * * <p> * 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(); } }
第六: 運行結果
到這裡也算告一段落,現在我們已經拿到圖片的uri。剩下的圖片裁剪和上傳,在這裡:Android實現圖片裁剪和上傳
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- Android開發之permission動態權限獲取詳解
- Android權限詢問的實例詳解
- Android11及以上文件讀寫權限申請詳細介紹
- Android實現獲取短信驗證碼並自動填充
- Android錄音功能的實現以及踩坑實戰記錄