Android 選擇相冊(cè)照片并返回功能的實(shí)現(xiàn)代碼
首先由于進(jì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)相冊(cè):
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;
//判斷手機(jī)系統(tǒng)版本號(hào)
if (Build.VERSION.SDK_INT >= 19) {
//4.4及以上系統(tǒng)使用這個(gè)方法處理圖片
bitmap = ImgUtil.handleImageOnKitKat(this, data); //ImgUtil是自己實(shí)現(xiàn)的一個(gè)工具類
} else {
//4.4以下系統(tǒng)使用這個(gè)方法處理圖片
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;
//判斷手機(jī)系統(tǒng)版本號(hào)
if (Build.VERSION.SDK_INT >= 19) {
//4.4及以上系統(tǒng)使用這個(gè)方法處理圖片
bitmap = ImgUtil.handleImageOnKitKat(this, data); //ImgUtil是自己實(shí)現(xiàn)的一個(gè)工具類
} else {
//4.4以下系統(tǒng)使用這個(gè)方法處理圖片
bitmap = ImgUtil.handleImageBeforeKitKat(this, data);
}
ImageView view = (ImageView) findViewById(R.id.personal_info_header_img);
view.setImageBitmap(bitmap);
}
break;
default:
break;
}
}
將對(duì)圖像的相關(guān)操作封裝成一個(gè)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)使用這個(gè)方法處理圖片
@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)使用這個(gè)方法處理圖片
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來獲取真實(shí)的圖片路徑
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;
}
//對(duì)bitmap進(jìn)行質(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();
//開始讀入圖片,此時(shí)把options.inJustDecodeBounds 設(shè)回true了
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);//此時(shí)返回bm為空
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
//現(xiàn)在主流手機(jī)比較多是800*480分辨率,所以高和寬我們?cè)O(shè)置為
float hh = 800f;//這里設(shè)置高度為800f
float ww = 480f;//這里設(shè)置寬度為480f
//縮放比。由于是固定比例縮放,只用高或者寬其中一個(gè)數(shù)據(jù)進(jìn)行計(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;//設(shè)置縮放比例
//重新讀入圖片,注意此時(shí)已經(jīng)把options.inJustDecodeBounds 設(shè)回false了
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return compressImage(bitmap);//壓縮好比例大小后再進(jìn)行質(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)使用這個(gè)方法處理圖片
@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)使用這個(gè)方法處理圖片
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來獲取真實(shí)的圖片路徑
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;
}
//對(duì)bitmap進(jìn)行質(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();
//開始讀入圖片,此時(shí)把options.inJustDecodeBounds 設(shè)回true了
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);//此時(shí)返回bm為空
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
//現(xiàn)在主流手機(jī)比較多是800*480分辨率,所以高和寬我們?cè)O(shè)置為
float hh = 800f;//這里設(shè)置高度為800f
float ww = 480f;//這里設(shè)置寬度為480f
//縮放比。由于是固定比例縮放,只用高或者寬其中一個(gè)數(shù)據(jù)進(jìn)行計(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;//設(shè)置縮放比例
//重新讀入圖片,注意此時(shí)已經(jīng)把options.inJustDecodeBounds 設(shè)回false了
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return compressImage(bitmap);//壓縮好比例大小后再進(jìn)行質(zhì)量壓縮
}
}
為了兼容新老版本的手機(jī),我們做了一個(gè)判斷,如果是4.4及以上系統(tǒng)的手機(jī)就調(diào)用handleImageOnKitKat()方法來處理圖片,否則就調(diào)用handleImageBeforeKitKat()方法來處理圖片。之所以要這么做,是因?yàn)锳ndroid系統(tǒng)從4.4版本開始,選取相冊(cè)的圖片不再返回圖片真是的Uri了,而是一個(gè)封裝過的Uri,因此如果是4.4版本以上的手機(jī)需要對(duì)這個(gè)Uri進(jìn)行解析才行。
當(dāng)然了,獲取到圖片路徑后不推薦直接使用 BitmapFactory.decodeFile(imgPath)來獲取bitmap,因?yàn)槟承﹫D片體積可能很大,直接加載到內(nèi)存中有可能會(huì)導(dǎo)致程序崩潰(我就遇到過了..你可以直接加載手機(jī)高像素拍的原圖片試試看=。=)。 所以更好的做法是先對(duì)圖片進(jìn)行適當(dāng)?shù)膲嚎s,然后再加載到內(nèi)存中(上述代碼中實(shí)現(xiàn)了)。
總結(jié)
以上所述是小編給大家介紹的Android 選擇相冊(cè)照片并返回功能的實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android時(shí)分秒計(jì)時(shí)器的兩種實(shí)現(xiàn)方法
這篇文章主要介紹了Android時(shí)分秒計(jì)時(shí)器的兩種實(shí)現(xiàn)方法,分別是Chronometer控件和handler+timer+timerTask方式,非常不錯(cuò),感興趣的朋友一起看下吧2016-08-08
Android 通過網(wǎng)絡(luò)圖片路徑查看圖片實(shí)例詳解
這篇文章主要介紹了Android 通過網(wǎng)絡(luò)圖片路徑查看圖片實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
android TextView實(shí)現(xiàn)跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了android TextView實(shí)現(xiàn)跑馬燈效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android intent之間復(fù)雜參數(shù)傳遞方法詳解
這篇文章主要介紹了Android intent之間復(fù)雜參數(shù)傳遞方法,較為詳細(xì)的分析了Android中intent參數(shù)傳遞的常見方法與使用技巧,需要的朋友可以參考下2016-10-10
Flutter實(shí)現(xiàn)漸變色加描邊字體效果
這篇文章介紹了Flutter實(shí)現(xiàn)漸變色描邊字體效果的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11

