Android 圖片選擇和拍照(剪輯)及壓縮問題 工具類
阿新 • • 發佈:2019-01-05
圖片選擇和拍照在開發過程中,會遇到不少坑:
1.版本4.4以後選擇圖片後不會返回絕對路徑,但返回Uri,要再查詢一次
2.去剪輯時,設定了true的話直接返回bitmap,可能會很佔記憶體,有些機子會掛掉(OOM)或者不會返回
3.圖片未更好的壓縮,應該做到寬高比壓縮後再質量壓縮,下面例子可以把幾MB的圖片壓縮到100kb左右
4.解決7.0版本檔案訪問問題
下面程式碼處理解決了上面的問題,是個小筆記,以後就不會再麻煩找“輪子”了。
1.在AndroidManifest新增FileProvider
<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>
2.在res資料夾下新增資料夾xml,再新增file_paths.xml
<?xml version="1.0" encoding="utf-8"?> <paths> <root-path name="root" path="" /> <!--<files-path--> <!--name="files"--> <!--path="" />--> <!--<cache-path--> <!--name="cache"--> <!--path="" />--> <!-- <external-path--> <!--name="external"--> <!--path="" />--> <!--<external-path--> <!--name="external"--> <!--path="/apk" />--> <external-path name="external" path="" /> <!--<external-files-path--> <!--name="name"--> <!--path="path" />--> <!--<external-cache-path--> <!--name="name"--> <!--path="path" />--> </paths> <!--<root-path/> 代表裝置的根目錄new File("/");--> <!--<files-path/> 代表context.getFilesDir()--> <!--<cache-path/> 代表context.getCacheDir()--> <!--<external-path/> 代表Environment.getExternalStorageDirectory()--> <!--<external-files-path>代表context.getExternalFilesDirs()--> <!--<external-cache-path>代表getExternalCacheDirs()-->
3.使用:
呼叫SelectPicUtil.getByAlbum(Activity.this); 選擇相簿
呼叫SelectPicUtil.getByCamera(Activity.this);進行拍照
然後在onActivityResult進行設定返回:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Uri uri = SelectPicUtil.onActivityResult(this, requestCode, resultCode, data); //下面是需求要裁剪的示例 // Uri uri = SelectPicUtil.onActivityResult(this, requestCode, resultCode, data,1,1, 500,500, true); if (null != uri) { // 當不為空時獲取到圖片Uri decodeUriBitmap(uri); } } private void decodeUriBitmap(final Uri uri) { new Thread(new Runnable() { @Override public void run() { try { String picturePath = SelectPicUtil.getImageAbsolutePath(OtherMsgActivity.this, uri); if (!TextUtils.isEmpty(picturePath)) { Bitmap bitmapSrc = SelectPicUtil.getImageThumbnail(picturePath, 1000, 1000); // 比例壓縮圖片 if (bitmapSrc != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmapSrc.compress(Bitmap.CompressFormat.JPEG, 80, baos); // 圖片質量壓縮80% bitmapSrc.recycle(); byte[] bytes = baos.toByteArray(); try { if (null != baos) { baos.close(); } } catch (IOException e) { } // 對位元組陣列Base64編碼 final String bitmap = Base64.encodeToString(bytes, Base64.DEFAULT);// 返回Base64編碼過的位元組陣列字串 upLoadPic(bitmap); } else { upLoadPic(""); } } else { upLoadPic(""); } } catch (Exception e) { } } }).start(); }
4.工具類:
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import com.yingyou.demo.BuildConfig;
import java.io.File;
public class SelectPicUtil {
private static final String temp = Environment.getExternalStorageDirectory().getAbsolutePath() + "/temp.jpg";
public static final int GET_BY_ALBUM = 801;// 開啟相簿
public static final int GET_BY_CAMERA = 802;// 開啟相機
public static final int CROP = 803;// 裁剪圖片
public static void getByAlbum(Activity act) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
act.startActivityForResult(intent, GET_BY_ALBUM);
} else {
Intent intentFromGallery = new Intent();
intentFromGallery.setType("image/*");
intentFromGallery.setAction(Intent.ACTION_GET_CONTENT);
act.startActivityForResult(intentFromGallery, GET_BY_ALBUM);
}
}
public static void getByCamera(Activity act) {
getByCamera(act, temp, GET_BY_CAMERA);
}
// 給其他地方用
public static void getByCamera(Activity act, String path, int requestCode) {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri = getUri(act, new File(path));
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
takePictureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
act.startActivityForResult(takePictureIntent, requestCode);
} else {
ToastUtils.showText("請安裝sd卡");
}
}
public static Uri onActivityResult(Activity act, int requestCode,
int resultCode, Intent data) {
return onActivityResult(act, requestCode, resultCode, data, 0, 0, 0, 0, false);
}
public static Uri onActivityResult(Activity act, int requestCode,
int resultCode, Intent data, int w, int h, int aspectX, int aspectY) {
return onActivityResult(act, requestCode, resultCode, data, w, h, aspectX, aspectY, true);
}
public static Uri onActivityResult(Activity act, int requestCode,
int resultCode, Intent data, int outputX, int outputY, int aspectX, int aspectY, boolean isCut) {
if (resultCode == Activity.RESULT_OK) {
Uri uri = null;
switch (requestCode) {
case GET_BY_ALBUM:
if (isCut) {
if (null == data) return null;
String path = getImageAbsolutePath(act, data.getData());
if (TextUtils.isEmpty(path)) return null;
uri = getUri(act, new File(path));
} else {
return data.getData();
}
break;
case GET_BY_CAMERA:
if (isCut) {
uri = getUri(act, new File(temp));
} else {
return Uri.parse(temp);
}
break;
case CROP:
// return getUri(act, new File(temp));
return Uri.fromFile(new File(temp));
}
if (isCut && null != uri) {
crop(act, uri, outputX, outputY, aspectX, aspectY);
return null;
}
}
return null;
}
public static void crop(Activity act, Uri uri, int outputX, int outputY, int aspectX, int aspectY) {
if (outputX == 0 && outputY == 0) {
outputX = outputY = 480;
}
if (aspectX == 0 && aspectY == 0) {
aspectX = aspectY = 1;
}
Intent intent = new Intent("com.android.camera.action.CROP");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//對目標應用臨時授權該Uri所代表的檔案
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", aspectX);
intent.putExtra("aspectY", aspectY);
intent.putExtra("outputX", outputX);
intent.putExtra("outputY", outputY);
// intent.putExtra(MediaStore.EXTRA_OUTPUT, getUri(act, new File(temp)));
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(temp)));
intent.putExtra("outputFormat", "JPEG");
intent.putExtra("noFaceDetection", true);
intent.putExtra("return-data", false);
act.startActivityForResult(intent, CROP);
}
public static String getImageAbsolutePath(Activity context, Uri uri) {
if (null == uri)
return null;
final String scheme = uri.getScheme();
String data = null;
if (scheme == null)
data = uri.getPath();
else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
data = uri.getPath();
} else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}, null,
null, null);
if (null != cursor) {
if (cursor.moveToFirst()) {
int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
if (index > -1) {
data = cursor.getString(index);
}
}
cursor.close();
}
}
return data;
}
public static Uri getUri(Context context, File file) {
Uri fileUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// BuildConfig 是工程包路徑下的:如:com.yingyou.demo.BuildConfig
fileUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", file);
} else {
fileUri = Uri.fromFile(file);
}
return fileUri;
}
public static Bitmap getImageThumbnail(String imagePath, int width,
int height) {
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
// Options中有個屬性inJustDecodeBounds。我們可以充分利用它,來避免大圖片的溢位問題
options.inJustDecodeBounds = true;// 設定為true可以不載入到記憶體,直接獲取Bitmap寬高
// 獲取這個圖片的寬和高,注意此處的bitmap為null
bitmap = BitmapFactory.decodeFile(imagePath, options);
if (bitmap == null) {
// 計算縮放比
int h = options.outHeight;// 獲取Bitmap的實際高度
int w = options.outWidth;// 獲取Bitmap的實際寬度
int beWidth = w / width;
int beHeight = h / height;
int rate = 1;
if (beWidth < beHeight) {
rate = beWidth;
} else {
rate = beHeight;
}
if (rate <= 0) {// 圖片實際大小小於縮圖,不縮放
rate = 1;
}
options.inSampleSize = rate;// rate就是壓縮的比例
options.inJustDecodeBounds = false;
// 重新讀入圖片,讀取縮放後的bitmap,注意這次要把options.inJustDecodeBounds 設為 false
bitmap = BitmapFactory.decodeFile(imagePath, options);// 獲取壓縮後的圖片
}
return bitmap;
}
public static Bitmap getBitmapFormUri(Activity ac, Uri uri) {
InputStream input;
try {
input = ac.getContentResolver().openInputStream(uri);
BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
onlyBoundsOptions.inJustDecodeBounds = true;
onlyBoundsOptions.inDither = true;// optional
onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
input.close();
int originalWidth = onlyBoundsOptions.outWidth;
int originalHeight = onlyBoundsOptions.outHeight;
if ((originalWidth == -1) || (originalHeight == -1))
return null;
// 圖片解析度以480x800為標準
float hh = 800f;// 這裡設定高度為800f
float ww = 480f;// 這裡設定寬度為480f
// 縮放比。由於是固定比例縮放,只用高或者寬其中一個數據進行計算即可
int be = 1;// be=1表示不縮放
if (originalWidth > originalHeight && originalWidth > ww) {// 如果寬度大的話根據寬度固定大小縮放
be = (int) (originalWidth / ww);
} else if (originalWidth < originalHeight && originalHeight > hh) {// 如果高度高的話根據寬度固定大小縮放
be = (int) (originalHeight / hh);
}
if (be <= 0)
be = 1;
// 比例壓縮
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = be;// 設定縮放比例
bitmapOptions.inDither = true;// optional
bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
input = ac.getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(input, null,
bitmapOptions);
input.close();
return compressImage(bitmap);// 再進行質量壓縮
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}