国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 選擇相冊照片并返回功能的實現代碼

2019-10-22 18:14:07
字體:
來源:轉載
供稿:網友

首先由于進行讀寫操作,要在 AndroidManifest.xml中聲明權限:

<uses-permission android:name=“android.permission.READ_EXTERNAL_STORAGE” /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

調用系統相冊:

private static final int CHOOSE_PHOTO=0; Intent intent = new Intent(“android.intent.action.GET_CONTENT”); intent.setType(”image/*”); startActivityForResult(intent, CHOOSE_PHOTO); [java] view plain copyprivate static final int CHOOSE_PHOTO=0; Intent intent = new Intent("android.intent.action.GET_CONTENT"); intent.setType("image/*"); startActivityForResult(intent, CHOOSE_PHOTO); 

然后回調:

@Override   protected void onActivityResult(int requestCode, int resultCode, Intent data) {     switch (requestCode) {       case CHOOSE_PHOTO:         if (resultCode == RESULT_OK) {           Bitmap bitmap = null;           //判斷手機系統版本號           if (Build.VERSION.SDK_INT >= 19) {             //4.4及以上系統使用這個方法處理圖片             bitmap = ImgUtil.handleImageOnKitKat(this, data);    //ImgUtil是自己實現的一個工具類           } else {             //4.4以下系統使用這個方法處理圖片             bitmap = ImgUtil.handleImageBeforeKitKat(this, data);           }           ImageView view = (ImageView) findViewById(R.id.personal_info_header_img);           view.setImageBitmap(bitmap);         }         break;       default:         break;     }   } 
@Override   protected void onActivityResult(int requestCode, int resultCode, Intent data) {     switch (requestCode) {       case CHOOSE_PHOTO:         if (resultCode == RESULT_OK) {           Bitmap bitmap = null;           //判斷手機系統版本號           if (Build.VERSION.SDK_INT >= 19) {             //4.4及以上系統使用這個方法處理圖片             bitmap = ImgUtil.handleImageOnKitKat(this, data);    //ImgUtil是自己實現的一個工具類           } else {             //4.4以下系統使用這個方法處理圖片             bitmap = ImgUtil.handleImageBeforeKitKat(this, data);           }           ImageView view = (ImageView) findViewById(R.id.personal_info_header_img);           view.setImageBitmap(bitmap);         }         break;       default:         break;     }   } 

將對圖像的相關操作封裝成一個ImgUtil類,便于使用:

import android.annotation.TargetApi; import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.preference.PreferenceManager; import android.provider.DocumentsContract; import android.provider.MediaStore; import android.text.TextUtils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; /**  * Created by wbin on 2016/3/22.  */ public class ImgUtil {   //4.4及以上系統使用這個方法處理圖片   @TargetApi(19)   public static Bitmap handleImageOnKitKat(Context context, Intent data) {     String imagePath = null;     Uri uri = data.getData();     if (DocumentsContract.isDocumentUri(context, uri)) {       //如果是document類型的Uri,則通過document id處理       String docId = DocumentsContract.getDocumentId(uri);       if (“com.android.providers.media.documents”.equals(uri.getAuthority())) {         String id = docId.split(”:”)[1]; //解析出數字格式的id         String selection = MediaStore.Images.Media._ID + ”=” + id;         imagePath = getImagePath(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);       } else if (“com.android.providers.downloads.documents”.equals(uri.getAuthority())) {         Uri contentUri = ContentUris.withAppendedId(             Uri.parse(”content://downloads/public_downloads”), Long.valueOf(docId));         imagePath = getImagePath(context, contentUri, null);       }     } else if (“content”.equalsIgnoreCase(uri.getScheme())) {       //如果不是document類型的Uri,則使用普通方式處理       imagePath = getImagePath(context, uri, null);     }     return getImage(imagePath);   }   //4.4以下系統使用這個方法處理圖片   public static Bitmap handleImageBeforeKitKat(Context context, Intent data) {     Uri uri = data.getData();     String imagePath = getImagePath(context, uri, null);     return getImage(imagePath);   }   public static String getImagePath(Context context, Uri uri, String selection) {     String path = null;     //通過Uri和selection來獲取真實的圖片路徑     Cursor cursor = context.getContentResolver().query(uri, null, selection, null, null);     if (cursor != null) {       if (cursor.moveToFirst()) {         path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));       }       cursor.close();     }     return path;   }   //對bitmap進行質量壓縮   public static Bitmap compressImage(Bitmap image) {     ByteArrayOutputStream baos = new ByteArrayOutputStream();     image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//質量壓縮方法,這里100表示不壓縮,把壓縮后的數據存放到baos中     int options = 100;     while (baos.toByteArray().length / 1024 > 100) {  //循環判斷如果壓縮后圖片是否大于100kb,大于繼續壓縮       baos.reset();//重置baos即清空baos       image.compress(Bitmap.CompressFormat.JPEG, options, baos);//這里壓縮options%,把壓縮后的數據存放到baos中       options -= 10;//每次都減少10     }     ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把壓縮后的數據baos存放到ByteArrayInputStream中     Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream數據生成圖片     return bitmap;   }   //傳入圖片路徑,返回壓縮后的bitmap   public static Bitmap getImage(String srcPath) {     if (TextUtils.isEmpty(srcPath)) //如果圖片路徑為空 直接返回       return null;     BitmapFactory.Options newOpts = new BitmapFactory.Options();     //開始讀入圖片,此時把options.inJustDecodeBounds 設回true了     newOpts.inJustDecodeBounds = true;     Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);//此時返回bm為空     newOpts.inJustDecodeBounds = false;     int w = newOpts.outWidth;     int h = newOpts.outHeight;     //現在主流手機比較多是800*480分辨率,所以高和寬我們設置為     float hh = 800f;//這里設置高度為800f     float ww = 480f;//這里設置寬度為480f     //縮放比。由于是固定比例縮放,只用高或者寬其中一個數據進行計算即可     int be = 1;//be=1表示不縮放     if (w > h && w > ww) {//如果寬度大的話根據寬度固定大小縮放       be = (int) (newOpts.outWidth / ww);     } else if (w < h && h > hh) {//如果高度高的話根據寬度固定大小縮放       be = (int) (newOpts.outHeight / hh);     }     if (be <= 0)       be = 1;     newOpts.inSampleSize = be;//設置縮放比例     //重新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了     bitmap = BitmapFactory.decodeFile(srcPath, newOpts);     return compressImage(bitmap);//壓縮好比例大小后再進行質量壓縮   } } 
import android.annotation.TargetApi; import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.preference.PreferenceManager; import android.provider.DocumentsContract; import android.provider.MediaStore; import android.text.TextUtils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; /**  * Created by wbin on 2016/3/22.  */ public class ImgUtil {   //4.4及以上系統使用這個方法處理圖片   @TargetApi(19)   public static Bitmap handleImageOnKitKat(Context context, Intent data) {     String imagePath = null;     Uri uri = data.getData();     if (DocumentsContract.isDocumentUri(context, uri)) {       //如果是document類型的Uri,則通過document id處理       String docId = DocumentsContract.getDocumentId(uri);       if ("com.android.providers.media.documents".equals(uri.getAuthority())) {         String id = docId.split(":")[1]; //解析出數字格式的id         String selection = MediaStore.Images.Media._ID + "=" + id;         imagePath = getImagePath(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);       } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {         Uri contentUri = ContentUris.withAppendedId(             Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));         imagePath = getImagePath(context, contentUri, null);       }     } else if ("content".equalsIgnoreCase(uri.getScheme())) {       //如果不是document類型的Uri,則使用普通方式處理       imagePath = getImagePath(context, uri, null);     }     return getImage(imagePath);   }   //4.4以下系統使用這個方法處理圖片   public static Bitmap handleImageBeforeKitKat(Context context, Intent data) {     Uri uri = data.getData();     String imagePath = getImagePath(context, uri, null);     return getImage(imagePath);   }   public static String getImagePath(Context context, Uri uri, String selection) {     String path = null;     //通過Uri和selection來獲取真實的圖片路徑     Cursor cursor = context.getContentResolver().query(uri, null, selection, null, null);     if (cursor != null) {       if (cursor.moveToFirst()) {         path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));       }       cursor.close();     }     return path;   }   //對bitmap進行質量壓縮   public static Bitmap compressImage(Bitmap image) {     ByteArrayOutputStream baos = new ByteArrayOutputStream();     image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//質量壓縮方法,這里100表示不壓縮,把壓縮后的數據存放到baos中     int options = 100;     while (baos.toByteArray().length / 1024 > 100) {  //循環判斷如果壓縮后圖片是否大于100kb,大于繼續壓縮       baos.reset();//重置baos即清空baos       image.compress(Bitmap.CompressFormat.JPEG, options, baos);//這里壓縮options%,把壓縮后的數據存放到baos中       options -= 10;//每次都減少10     }     ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把壓縮后的數據baos存放到ByteArrayInputStream中     Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream數據生成圖片     return bitmap;   }   //傳入圖片路徑,返回壓縮后的bitmap   public static Bitmap getImage(String srcPath) {     if (TextUtils.isEmpty(srcPath)) //如果圖片路徑為空 直接返回       return null;     BitmapFactory.Options newOpts = new BitmapFactory.Options();     //開始讀入圖片,此時把options.inJustDecodeBounds 設回true了     newOpts.inJustDecodeBounds = true;     Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);//此時返回bm為空     newOpts.inJustDecodeBounds = false;     int w = newOpts.outWidth;     int h = newOpts.outHeight;     //現在主流手機比較多是800*480分辨率,所以高和寬我們設置為     float hh = 800f;//這里設置高度為800f     float ww = 480f;//這里設置寬度為480f     //縮放比。由于是固定比例縮放,只用高或者寬其中一個數據進行計算即可     int be = 1;//be=1表示不縮放     if (w > h && w > ww) {//如果寬度大的話根據寬度固定大小縮放       be = (int) (newOpts.outWidth / ww);     } else if (w < h && h > hh) {//如果高度高的話根據寬度固定大小縮放       be = (int) (newOpts.outHeight / hh);     }     if (be <= 0)       be = 1;     newOpts.inSampleSize = be;//設置縮放比例     //重新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了     bitmap = BitmapFactory.decodeFile(srcPath, newOpts);     return compressImage(bitmap);//壓縮好比例大小后再進行質量壓縮   } } 

為了兼容新老版本的手機,我們做了一個判斷,如果是4.4及以上系統的手機就調用handleImageOnKitKat()方法來處理圖片,否則就調用handleImageBeforeKitKat()方法來處理圖片。之所以要這么做,是因為Android系統從4.4版本開始,選取相冊的圖片不再返回圖片真是的Uri了,而是一個封裝過的Uri,因此如果是4.4版本以上的手機需要對這個Uri進行解析才行。 

當然了,獲取到圖片路徑后不推薦直接使用 BitmapFactory.decodeFile(imgPath)來獲取bitmap,因為某些圖片體積可能很大,直接加載到內存中有可能會導致程序崩潰(我就遇到過了..你可以直接加載手機高像素拍的原圖片試試看=。=)。 所以更好的做法是先對圖片進行適當的壓縮,然后再加載到內存中(上述代碼中實現了)。

總結

以上所述是小編給大家介紹的Android 選擇相冊照片并返回功能的實現代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 邢台市| 类乌齐县| 三河市| 海门市| 太仆寺旗| 马尔康县| 连平县| 长宁县| 平顺县| 桂东县| 钦州市| 霸州市| 潼关县| 灯塔市| 河曲县| 康保县| 土默特左旗| 乃东县| 咸阳市| 平舆县| 香河县| 宁化县| 滦南县| 河源市| 巴青县| 襄垣县| 手游| 东辽县| 郴州市| 汉源县| 色达县| 广宁县| 滨州市| 荣成市| 桦南县| 沙田区| 锡林浩特市| 芷江| 青州市| 桓仁| 启东市|