網站首頁 編程語言 正文
本文實例為大家分享了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實現圖片裁剪和上傳
原文鏈接:https://blog.csdn.net/weixin_43741599/article/details/106405349
相關推薦
- 2022-05-03 詳解利用python-highcharts庫繪制交互式可視化圖表_python
- 2022-07-22 CSS3:盒陰影、邊界圖片、指定每一個圓角、背景、過度、動畫、
- 2022-04-12 計算機上不了網,排查了很久,結果問題是ip沖突了,
- 2022-10-16 QT編寫tcp通信工具(Server端)_C 語言
- 2022-11-08 Numpy安裝、升級與卸載的詳細圖文教程_python
- 2022-12-10 C++實現保存數據至EXCEL_C 語言
- 2022-07-09 python實現通訊錄系統_python
- 2023-01-30 PyQt中使用QProcess運行一個進程的示例代碼_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支