Android中圖片壓縮方案詳解及源碼下載
Android中圖片壓縮方案詳解及源碼下載
圖片的展示可以說(shuō)在我們?nèi)魏我粋€(gè)應(yīng)用中都避免不了,可是大量的圖片就會(huì)出現(xiàn)很多的問(wèn)題,比如加載大圖片或者多圖時(shí)的OOM問(wèn)題,可以移步到Android高效加載大圖及多圖避免程序OOM.還有一個(gè)問(wèn)題就是圖片的上傳下載問(wèn)題,往往我們都喜歡圖片既清楚又占的內(nèi)存小,也就是盡可能少的耗費(fèi)我們的流量,這就是我今天所要講述的問(wèn)題:圖片的壓縮方案的詳解。
1、質(zhì)量壓縮法
設(shè)置bitmap options屬性,降低圖片的質(zhì)量,像素不會(huì)減少
第一個(gè)參數(shù)為需要壓縮的bitmap圖片對(duì)象,第二個(gè)參數(shù)為壓縮后圖片保存的位置
設(shè)置options 屬性0-100,來(lái)實(shí)現(xiàn)壓縮。
private 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;
}
質(zhì)量壓縮不會(huì)減少圖片的像素。它是在保持像素不變的前提下改變圖片的位深及透明度等,來(lái)達(dá)到壓縮圖片的目的。進(jìn)過(guò)它壓縮的圖片文件大小會(huì)有改變,但是導(dǎo)入成bitmap后占得內(nèi)存是不變的。因?yàn)橐3窒袼夭蛔儯运蜔o(wú)法無(wú)限壓縮,到達(dá)一個(gè)值之后就不會(huì)繼續(xù)變小了。顯然這個(gè)方法并不適用于縮略圖,其實(shí)也不適用于想通過(guò)壓縮圖片減少內(nèi)存的適用,僅僅適用于想在保證圖片質(zhì)量的同時(shí)減少文件大小的情況而已。
2、采樣率壓縮法
private Bitmap getimage(String srcPath) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
//開(kāi)始讀入圖片,此時(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ī)比較多是1280*720分辨率,所以高和寬我們?cè)O(shè)置為
float hh = 1280f;//這里設(shè)置高度為1280f
float ww = 720f;//這里設(shè)置寬度為720f
//縮放比。由于是固定比例縮放,只用高或者寬其中一個(gè)數(shù)據(jù)進(jìn)行計(jì)算即可
int be = 1;//be=1表示不縮放
if (w > h && w > ww) {//如果寬度大的話(huà)根據(jù)寬度固定大小縮放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {//如果高度高的話(huà)根據(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ì)量壓縮
}
這個(gè)方法的好處是大大的縮小了內(nèi)存的使用,在讀存儲(chǔ)器上的圖片時(shí),如果不需要高清的效果,可以先只讀取圖片的邊,通過(guò)寬和高設(shè)定好取樣率后再加載圖片,這樣就不會(huì)過(guò)多的占用內(nèi)存。
3、縮放法
通過(guò)縮放圖片像素來(lái)減少圖片占用內(nèi)存大小。
方式一
public static void compressBitmapToFile(Bitmap bmp, File file){
// 尺寸壓縮倍數(shù),值越大,圖片尺寸越小
int ratio = 2;
// 壓縮Bitmap到對(duì)應(yīng)尺寸
Bitmap result = Bitmap.createBitmap(bmp.getWidth() / ratio, bmp.getHeight() / ratio, Config.ARGB_8888);
Canvas canvas = new Canvas(result);
Rect rect = new Rect(0, 0, bmp.getWidth() / ratio, bmp.getHeight() / ratio);
canvas.drawBitmap(bmp, null, rect, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 把壓縮后的數(shù)據(jù)存放到baos中
result.compress(Bitmap.CompressFormat.JPEG, 100 ,baos);
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
方式二
ByteArrayOutputStream out = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 85, out);
float zoom = (float)Math.sqrt(size * 1024 / (float)out.toByteArray().length);
Matrix matrix = new Matrix();
matrix.setScale(zoom, zoom);
Bitmap result = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true);
out.reset();
result.compress(Bitmap.CompressFormat.JPEG, 85, out);
while(out.toByteArray().length > size * 1024){
System.out.println(out.toByteArray().length);
matrix.setScale(0.9f, 0.9f);
result = Bitmap.createBitmap(result, 0, 0, result.getWidth(), result.getHeight(), matrix, true);
out.reset();
result.compress(Bitmap.CompressFormat.JPEG, 85, out);
}
縮放法其實(shí)很簡(jiǎn)單,設(shè)定好matrix,在createBitmap就可以了。但是我們并不知道縮放比例,而是要求了圖片的最終大小。直接用大小的比例來(lái)做的話(huà)肯定是有問(wèn)題的,用大小比例的開(kāi)方來(lái)做會(huì)比較接近,但是還是有差距。但是只要再做一下微調(diào)應(yīng)該就可以了,微調(diào)的話(huà)就是修改過(guò)的圖片大小比最終大小還大的話(huà),就進(jìn)行0.8的壓縮再比較,循環(huán)直到大小合適。這樣就能得到合適大小的圖片,而且也能比較保證質(zhì)量。
4、JNI調(diào)用libjpeg庫(kù)壓縮
JNI靜態(tài)調(diào)用 bitherlibjni.c 中的方法來(lái)實(shí)現(xiàn)壓縮Java_net_bither_util_NativeUtil_compressBitmap
net_bither_util為包名,NativeUtil為類(lèi)名,compressBitmap為native方法名,我們只需要調(diào)用saveBitmap()方法就可以,bmp 需要壓縮的Bitmap對(duì)象, quality壓縮質(zhì)量0-100, fileName 壓縮后要保存的文件地址, optimize 是否采用哈弗曼表數(shù)據(jù)計(jì)算 品質(zhì)相差5-10倍。
jstring Java_net_bither_util_NativeUtil_compressBitmap(JNIEnv* env,
jobject thiz, jobject bitmapcolor, int w, int h, int quality,
jbyteArray fileNameStr, jboolean optimize) {
AndroidBitmapInfo infocolor;
BYTE* pixelscolor;
int ret;
BYTE * data;
BYTE *tmpdata;
char * fileName = jstrinTostring(env, fileNameStr);
if ((ret = AndroidBitmap_getInfo(env, bitmapcolor, &infocolor)) < 0) {
LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
return (*env)->NewStringUTF(env, "0");;
}
if ((ret = AndroidBitmap_lockPixels(env, bitmapcolor, &pixelscolor)) < 0) {
LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
}
BYTE r, g, b;
data = NULL;
data = malloc(w * h * 3);
tmpdata = data;
int j = 0, i = 0;
int color;
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
color = *((int *) pixelscolor);
r = ((color & 0x00FF0000) >> 16);
g = ((color & 0x0000FF00) >> 8);
b = color & 0x000000FF;
*data = b;
*(data + 1) = g;
*(data + 2) = r;
data = data + 3;
pixelscolor += 4;
}
}
AndroidBitmap_unlockPixels(env, bitmapcolor);
int resultCode= generateJPEG(tmpdata, w, h, quality, fileName, optimize);
free(tmpdata);
if(resultCode==0){
jstring result=(*env)->NewStringUTF(env, error);
error=NULL;
return result;
}
return (*env)->NewStringUTF(env, "1"); //success
}
5、質(zhì)量壓縮+采樣率壓縮+JNI調(diào)用libjpeg庫(kù)壓縮結(jié)合使用
首先通過(guò)尺寸壓縮,壓縮到手機(jī)常用的一個(gè)分辨率(1280*960 微信好像是壓縮到這個(gè)分辨率),然后我們要把圖片壓縮到一定大小以?xún)?nèi)(比如說(shuō)200k),然后通過(guò)循環(huán)進(jìn)行質(zhì)量壓縮來(lái)計(jì)算options需要設(shè)置為多少,最后調(diào)用JNI壓縮。
計(jì)算縮放比
/**
* 計(jì)算縮放比
* @param bitWidth 當(dāng)前圖片寬度
* @param bitHeight 當(dāng)前圖片高度
* @return int 縮放比
*/
public static int getRatioSize(int bitWidth, int bitHeight) {
// 圖片最大分辨率
int imageHeight = 1280;
int imageWidth = 960;
// 縮放比
int ratio = 1;
// 縮放比,由于是固定比例縮放,只用高或者寬其中一個(gè)數(shù)據(jù)進(jìn)行計(jì)算即可
if (bitWidth > bitHeight && bitWidth > imageWidth) {
// 如果圖片寬度比高度大,以寬度為基準(zhǔn)
ratio = bitWidth / imageWidth;
} else if (bitWidth < bitHeight && bitHeight > imageHeight) {
// 如果圖片高度比寬度大,以高度為基準(zhǔn)
ratio = bitHeight / imageHeight;
}
// 最小比率為1
if (ratio <= 0)
ratio = 1;
return ratio;
}
質(zhì)量壓縮+JNI壓縮
/**
* @Description: 通過(guò)JNI圖片壓縮把Bitmap保存到指定目錄
* @param curFilePath
* 當(dāng)前圖片文件地址
* @param targetFilePath
* 要保存的圖片文件地址
*/
public static void compressBitmap(String curFilePath, String targetFilePath) {
// 最大圖片大小 500KB
int maxSize = 500;
//根據(jù)地址獲取bitmap
Bitmap result = getBitmapFromFile(curFilePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 質(zhì)量壓縮方法,這里100表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中
int quality = 100;
result.compress(Bitmap.CompressFormat.JPEG, quality, baos);
// 循環(huán)判斷如果壓縮后圖片是否大于500kb,大于繼續(xù)壓縮
while (baos.toByteArray().length / 1024 > maxSize) {
// 重置baos即清空baos
baos.reset();
// 每次都減少10
quality -= 10;
// 這里壓縮quality,把壓縮后的數(shù)據(jù)存放到baos中
result.compress(Bitmap.CompressFormat.JPEG, quality, baos);
}
// JNI保存圖片到SD卡 這個(gè)關(guān)鍵
NativeUtil.saveBitmap(result, quality, targetFilePath, true);
// 釋放Bitmap
if (!result.isRecycled()) {
result.recycle();
}
}
JNI圖片壓縮工具類(lèi)
package net.bither.util;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.media.ExifInterface;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* JNI圖片壓縮工具類(lèi)
*
* @Description TODO
* @Package net.bither.util
* @Class NativeUtil
*/
public class NativeUtil {
private static int DEFAULT_QUALITY = 95;
/**
* @Description: JNI基本壓縮
* @param bit
* bitmap對(duì)象
* @param fileName
* 指定保存目錄名
* @param optimize
* 是否采用哈弗曼表數(shù)據(jù)計(jì)算 品質(zhì)相差5-10倍
*/
public static void compressBitmap(Bitmap bit, String fileName, boolean optimize) {
saveBitmap(bit, DEFAULT_QUALITY, fileName, optimize);
}
/**
* @Description: 通過(guò)JNI圖片壓縮把Bitmap保存到指定目錄
* @param image
* bitmap對(duì)象
* @param filePath
* 要保存的指定目錄
*/
public static void compressBitmap(Bitmap image, String filePath) {
// 最大圖片大小 150KB
int maxSize = 150;
// 獲取尺寸壓縮倍數(shù)
int ratio = NativeUtil.getRatioSize(image.getWidth(),image.getHeight());
// 壓縮Bitmap到對(duì)應(yīng)尺寸
Bitmap result = Bitmap.createBitmap(image.getWidth() / ratio,image.getHeight() / ratio, Config.ARGB_8888);
Canvas canvas = new Canvas(result);
Rect rect = new Rect(0, 0, image.getWidth() / ratio, image.getHeight() / ratio);
canvas.drawBitmap(image,null,rect,null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 質(zhì)量壓縮方法,這里100表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中
int options = 100;
result.compress(Bitmap.CompressFormat.JPEG, options, baos);
// 循環(huán)判斷如果壓縮后圖片是否大于100kb,大于繼續(xù)壓縮
while (baos.toByteArray().length / 1024 > maxSize) {
// 重置baos即清空baos
baos.reset();
// 每次都減少10
options -= 10;
// 這里壓縮options%,把壓縮后的數(shù)據(jù)存放到baos中
result.compress(Bitmap.CompressFormat.JPEG, options, baos);
}
// JNI保存圖片到SD卡 這個(gè)關(guān)鍵
NativeUtil.saveBitmap(result, options, filePath, true);
// 釋放Bitmap
if (!result.isRecycled()) {
result.recycle();
}
}
/**
* @Description: 通過(guò)JNI圖片壓縮把Bitmap保存到指定目錄
* @param curFilePath
* 當(dāng)前圖片文件地址
* @param targetFilePath
* 要保存的圖片文件地址
*/
public static void compressBitmap(String curFilePath, String targetFilePath) {
// 最大圖片大小 500KB
int maxSize = 500;
//根據(jù)地址獲取bitmap
Bitmap result = getBitmapFromFile(curFilePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 質(zhì)量壓縮方法,這里100表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中
int quality = 100;
result.compress(Bitmap.CompressFormat.JPEG, quality, baos);
// 循環(huán)判斷如果壓縮后圖片是否大于500kb,大于繼續(xù)壓縮
while (baos.toByteArray().length / 1024 > maxSize) {
// 重置baos即清空baos
baos.reset();
// 每次都減少10
quality -= 10;
// 這里壓縮quality,把壓縮后的數(shù)據(jù)存放到baos中
result.compress(Bitmap.CompressFormat.JPEG, quality, baos);
}
// JNI保存圖片到SD卡 這個(gè)關(guān)鍵
NativeUtil.saveBitmap(result, quality, targetFilePath, true);
// 釋放Bitmap
if (!result.isRecycled()) {
result.recycle();
}
}
/**
* 計(jì)算縮放比
* @param bitWidth 當(dāng)前圖片寬度
* @param bitHeight 當(dāng)前圖片高度
* @return int 縮放比
*/
public static int getRatioSize(int bitWidth, int bitHeight) {
// 圖片最大分辨率
int imageHeight = 1280;
int imageWidth = 960;
// 縮放比
int ratio = 1;
// 縮放比,由于是固定比例縮放,只用高或者寬其中一個(gè)數(shù)據(jù)進(jìn)行計(jì)算即可
if (bitWidth > bitHeight && bitWidth > imageWidth) {
// 如果圖片寬度比高度大,以寬度為基準(zhǔn)
ratio = bitWidth / imageWidth;
} else if (bitWidth < bitHeight && bitHeight > imageHeight) {
// 如果圖片高度比寬度大,以高度為基準(zhǔn)
ratio = bitHeight / imageHeight;
}
// 最小比率為1
if (ratio <= 0)
ratio = 1;
return ratio;
}
/**
* 通過(guò)文件路徑讀獲取Bitmap防止OOM以及解決圖片旋轉(zhuǎn)問(wèn)題
* @param filePath
* @return
*/
public static Bitmap getBitmapFromFile(String filePath){
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = true;//只讀邊,不讀內(nèi)容
BitmapFactory.decodeFile(filePath, newOpts);
int w = newOpts.outWidth;
int h = newOpts.outHeight;
// 獲取尺寸壓縮倍數(shù)
newOpts.inSampleSize = NativeUtil.getRatioSize(w,h);
newOpts.inJustDecodeBounds = false;//讀取所有內(nèi)容
newOpts.inDither = false;
newOpts.inPurgeable=true;
newOpts.inInputShareable=true;
newOpts.inTempStorage = new byte[32 * 1024];
Bitmap bitmap = null;
File file = new File(filePath);
FileInputStream fs = null;
try {
fs = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
if(fs!=null){
bitmap = BitmapFactory.decodeFileDescriptor(fs.getFD(),null,newOpts);
//旋轉(zhuǎn)圖片
int photoDegree = readPictureDegree(filePath);
if(photoDegree != 0){
Matrix matrix = new Matrix();
matrix.postRotate(photoDegree);
// 創(chuàng)建新的圖片
bitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally{
if(fs!=null) {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bitmap;
}
/**
*
* 讀取圖片屬性:旋轉(zhuǎn)的角度
* @param path 圖片絕對(duì)路徑
* @return degree旋轉(zhuǎn)的角度
*/
public static int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
/**
* 調(diào)用native方法
* @Description:函數(shù)描述
* @param bit
* @param quality
* @param fileName
* @param optimize
*/
private static void saveBitmap(Bitmap bit, int quality, String fileName, boolean optimize) {
compressBitmap(bit, bit.getWidth(), bit.getHeight(), quality, fileName.getBytes(), optimize);
}
/**
* 調(diào)用底層 bitherlibjni.c中的方法
* @Description:函數(shù)描述
* @param bit
* @param w
* @param h
* @param quality
* @param fileNameBytes
* @param optimize
* @return
*/
private static native String compressBitmap(Bitmap bit, int w, int h, int quality, byte[] fileNameBytes,
boolean optimize);
/**
* 加載lib下兩個(gè)so文件
*/
static {
System.loadLibrary("jpegbither");
System.loadLibrary("bitherjni");
}
}
圖片壓縮處理中可能遇到的問(wèn)題:
請(qǐng)求系統(tǒng)相冊(cè)有三個(gè)Action
注意:圖庫(kù)(縮略圖) 和 圖片(原圖)
ACTION_OPEN_DOCUMENT 僅限4.4或以上使用 默認(rèn)打開(kāi)原圖
從圖片獲取到的uri 格式為:content://com.android.providers.media.documents/document/image%666>>>
ACTION_GET_CONTENT 4.4以下默認(rèn)打開(kāi)縮略圖 。 以上打開(kāi)文件管理器 供選擇,選擇圖庫(kù)打開(kāi)為縮略圖頁(yè)面,選擇圖片打開(kāi)為原圖瀏覽。
從圖庫(kù)獲取到的uri格式為:content://media/external/images/media/666666
ACTION_PICK 都可用,打開(kāi)默認(rèn)是縮略圖界面,還需要進(jìn)一步點(diǎn)開(kāi)查看。
參考代碼:
public void pickFromGallery() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
startActivityForResult(new Intent(Intent.ACTION_GET_CONTENT).setType("image/*"),
REQUEST_PICK_IMAGE);
} else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_KITKAT_PICK_IMAGE);
}
}
根據(jù)URI獲取對(duì)應(yīng)的文件路徑
在我們從圖庫(kù)中選擇圖片后回調(diào)給我們的data.getData()可能是URI,我們平時(shí)對(duì)文件的操作基本上都是基于路徑然后進(jìn)行各種操作與轉(zhuǎn)換,如今我們需要將URI對(duì)應(yīng)的文件路徑找出來(lái),具體參考代碼如下:
public static String getPathByUri(Context context, Uri data){
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return getPathByUri4BeforeKitkat(context, data);
}else {
return getPathByUri4AfterKitkat(context, data);
}
}
//4.4以前通過(guò)Uri獲取路徑:data是Uri,filename是一個(gè)String的字符串,用來(lái)保存路徑
public static String getPathByUri4BeforeKitkat(Context context, Uri data) {
String filename=null;
if (data.getScheme().toString().compareTo("content") == 0) {
Cursor cursor = context.getContentResolver().query(data, new String[] { "_data" }, null, null, null);
if (cursor.moveToFirst()) {
filename = cursor.getString(0);
}
} else if (data.getScheme().toString().compareTo("file") == 0) {// file:///開(kāi)頭的uri
filename = data.toString().replace("file://", "");// 替換file://
if (!filename.startsWith("/mnt")) {// 加上"/mnt"頭
filename += "/mnt";
}
}
return filename;
}
//4.4以后根據(jù)Uri獲取路徑:
@SuppressLint("NewApi")
public static String getPathByUri4AfterKitkat(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
if (isExternalStorageDocument(uri)) {// ExternalStorageProvider
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];
}
} else if (isDownloadsDocument(uri)) {// DownloadsProvider
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);
} else if (isMediaDocument(uri)) {// MediaProvider
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);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {// MediaStore
// (and
// general)
return getDataColumn(context, uri, null, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {// File
return uri.getPath();
}
return null;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context
* The context.
* @param uri
* The Uri to query.
* @param selection
* (Optional) Filter used in the query.
* @param selectionArgs
* (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
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 column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_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());
}
源碼奉上,自行參考:源碼下載
相關(guān)文章
Android使用ViewPager實(shí)現(xiàn)啟動(dòng)引導(dǎo)頁(yè)
這篇文章主要為大家詳細(xì)介紹了Android使用ViewPager實(shí)現(xiàn)第一次啟動(dòng)引導(dǎo)頁(yè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
Flutter之Timer實(shí)現(xiàn)短信驗(yàn)證碼獲取60s倒計(jì)時(shí)功能的代碼
這篇文章主要介紹了Flutter之Timer實(shí)現(xiàn)短信驗(yàn)證碼獲取60s倒計(jì)時(shí)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Android測(cè)量每秒幀數(shù)Frames Per Second (FPS)的方法
這篇文章主要介紹了Android測(cè)量每秒幀數(shù)Frames Per Second (FPS)的方法,涉及Android針對(duì)多媒體文件屬性操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android共享元素動(dòng)畫(huà)效果顯示問(wèn)題解決
什么是共享元素呢?可以理解為當(dāng)頁(yè)面跳轉(zhuǎn)是,看起來(lái)一個(gè)View屬于界面A又屬于界面B,下面這篇文章主要給大家介紹了關(guān)于Android共享元素動(dòng)畫(huà)效果顯示問(wèn)題的相關(guān)資料,需要的朋友可以參考下2022-02-02
Android開(kāi)發(fā)之子線(xiàn)程操作UI的幾種方法
這篇文章主要介紹了Android開(kāi)發(fā)之子線(xiàn)程操作UI的幾種方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
Android中imageView圖片放大縮小及旋轉(zhuǎn)功能示例代碼
這篇文章主要介紹了Android中imageView圖片放大縮小及旋轉(zhuǎn)功能示例代碼,需要的朋友可以參考下2017-08-08
Android使用注解進(jìn)行代碼檢查的實(shí)現(xiàn)方法
這篇文章主要介紹了Android如何使用注解進(jìn)行代碼檢查,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09

