Android 選擇相冊照片并返回功能的實現(xiàn)代碼
首先由于進行讀寫操作,要在 AndroidManifest.xml中聲明權(quán)限:
<uses-permission android:name=“android.permission.READ_EXTERNAL_STORAGE” /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
調(diào)用系統(tǒng)相冊:
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 copy private static final int CHOOSE_PHOTO=0; Intent intent = new Intent("android.intent.action.GET_CONTENT"); intent.setType("image/*"); startActivityForResult(intent, CHOOSE_PHOTO);
然后回調(diào):
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case CHOOSE_PHOTO: if (resultCode == RESULT_OK) { Bitmap bitmap = null; //判斷手機系統(tǒng)版本號 if (Build.VERSION.SDK_INT >= 19) { //4.4及以上系統(tǒng)使用這個方法處理圖片 bitmap = ImgUtil.handleImageOnKitKat(this, data); //ImgUtil是自己實現(xiàn)的一個工具類 } else { //4.4以下系統(tǒng)使用這個方法處理圖片 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; //判斷手機系統(tǒng)版本號 if (Build.VERSION.SDK_INT >= 19) { //4.4及以上系統(tǒng)使用這個方法處理圖片 bitmap = ImgUtil.handleImageOnKitKat(this, data); //ImgUtil是自己實現(xiàn)的一個工具類 } else { //4.4以下系統(tǒng)使用這個方法處理圖片 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及以上系統(tǒng)使用這個方法處理圖片 @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]; //解析出數(shù)字格式的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以下系統(tǒng)使用這個方法處理圖片 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進行質(zhì)量壓縮 public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//質(zhì)量壓縮方法,這里100表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中 int options = 100; while (baos.toByteArray().length / 1024 > 100) { //循環(huán)判斷如果壓縮后圖片是否大于100kb,大于繼續(xù)壓縮 baos.reset();//重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, options, baos);//這里壓縮options%,把壓縮后的數(shù)據(jù)存放到baos中 options -= 10;//每次都減少10 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把壓縮后的數(shù)據(jù)baos存放到ByteArrayInputStream中 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream數(shù)據(jù)生成圖片 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; //現(xiàn)在主流手機比較多是800*480分辨率,所以高和寬我們設置為 float hh = 800f;//這里設置高度為800f float ww = 480f;//這里設置寬度為480f //縮放比。由于是固定比例縮放,只用高或者寬其中一個數(shù)據(jù)進行計算即可 int be = 1;//be=1表示不縮放 if (w > h && w > ww) {//如果寬度大的話根據(jù)寬度固定大小縮放 be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) {//如果高度高的話根據(jù)寬度固定大小縮放 be = (int) (newOpts.outHeight / hh); } if (be <= 0) be = 1; newOpts.inSampleSize = be;//設置縮放比例 //重新讀入圖片,注意此時已經(jīng)把options.inJustDecodeBounds 設回false了 bitmap = BitmapFactory.decodeFile(srcPath, newOpts); return compressImage(bitmap);//壓縮好比例大小后再進行質(zhì)量壓縮 } }
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及以上系統(tǒng)使用這個方法處理圖片 @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]; //解析出數(shù)字格式的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以下系統(tǒng)使用這個方法處理圖片 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進行質(zhì)量壓縮 public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//質(zhì)量壓縮方法,這里100表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中 int options = 100; while (baos.toByteArray().length / 1024 > 100) { //循環(huán)判斷如果壓縮后圖片是否大于100kb,大于繼續(xù)壓縮 baos.reset();//重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, options, baos);//這里壓縮options%,把壓縮后的數(shù)據(jù)存放到baos中 options -= 10;//每次都減少10 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把壓縮后的數(shù)據(jù)baos存放到ByteArrayInputStream中 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream數(shù)據(jù)生成圖片 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; //現(xiàn)在主流手機比較多是800*480分辨率,所以高和寬我們設置為 float hh = 800f;//這里設置高度為800f float ww = 480f;//這里設置寬度為480f //縮放比。由于是固定比例縮放,只用高或者寬其中一個數(shù)據(jù)進行計算即可 int be = 1;//be=1表示不縮放 if (w > h && w > ww) {//如果寬度大的話根據(jù)寬度固定大小縮放 be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) {//如果高度高的話根據(jù)寬度固定大小縮放 be = (int) (newOpts.outHeight / hh); } if (be <= 0) be = 1; newOpts.inSampleSize = be;//設置縮放比例 //重新讀入圖片,注意此時已經(jīng)把options.inJustDecodeBounds 設回false了 bitmap = BitmapFactory.decodeFile(srcPath, newOpts); return compressImage(bitmap);//壓縮好比例大小后再進行質(zhì)量壓縮 } }
為了兼容新老版本的手機,我們做了一個判斷,如果是4.4及以上系統(tǒng)的手機就調(diào)用handleImageOnKitKat()方法來處理圖片,否則就調(diào)用handleImageBeforeKitKat()方法來處理圖片。之所以要這么做,是因為Android系統(tǒng)從4.4版本開始,選取相冊的圖片不再返回圖片真是的Uri了,而是一個封裝過的Uri,因此如果是4.4版本以上的手機需要對這個Uri進行解析才行。
當然了,獲取到圖片路徑后不推薦直接使用 BitmapFactory.decodeFile(imgPath)來獲取bitmap,因為某些圖片體積可能很大,直接加載到內(nèi)存中有可能會導致程序崩潰(我就遇到過了..你可以直接加載手機高像素拍的原圖片試試看=。=)。 所以更好的做法是先對圖片進行適當?shù)膲嚎s,然后再加載到內(nèi)存中(上述代碼中實現(xiàn)了)。
總結(jié)
以上所述是小編給大家介紹的Android 選擇相冊照片并返回功能的實現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
Android 通過網(wǎng)絡圖片路徑查看圖片實例詳解
這篇文章主要介紹了Android 通過網(wǎng)絡圖片路徑查看圖片實例詳解的相關資料,需要的朋友可以參考下2017-06-06Android intent之間復雜參數(shù)傳遞方法詳解
這篇文章主要介紹了Android intent之間復雜參數(shù)傳遞方法,較為詳細的分析了Android中intent參數(shù)傳遞的常見方法與使用技巧,需要的朋友可以參考下2016-10-10