手机拍摄的android 照片压缩上传上传到微信是不是被压缩了?

Android拍照调用系统相册仿微信封装总结,治疗各种崩溃,图片横竖问题压缩等问题。
项目下载地址:
最早使用android调用系统拍照然后遇到很多空指针等问题以及各种android 不同版本Intent取data有时候会空指针之类的api兼容问题,像使用红米note在开了很多应用后,再启动拍照系统,会发生拍照崩溃图片丢失等问题,用微信控件有时拍照有极小概率拍照无效等等奇怪的问题,其原因是因为Activity被回收了,变量变成null,
还有三星手机可能会遇到变量空针
需要在AndroidManifest.xml的Activity里加入
android:configChanges=&mcc|mnc|keyboard|keyboardHidden|navigation|orientation|screenSize|fontScale&
如何在Fragment上调用拍照,图片如何压缩
经过一段时间优化,修复了一些坑。我觉得目前的代码比较可靠,总结一下封装后分享出来。
import android.content.I
import android.os.B
import android.support.v7.app.AppCompatA
import android.view.V
import android.widget.ImageV
import android.widget.TextV
import android.widget.T
import java.io.F
import java.text.DecimalF
import java.util.L
import me.lxz.photopicker.camera.PhotoPickM
import me.lxz.photopicker.tools.SimpleImageL
public class SimpleDemoActivity extends AppCompatActivity {
PhotoPickManger pickM
private ImageV
private TextV
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**图片加载器*/
SimpleImageLoader.init(this.getApplicationContext());
btn = findViewById(R.id.btn);
img = (ImageView) findViewById(R.id.img);
tv=(TextView)findViewById(R.id.tv);
pickManger = new PhotoPickManger(&pick&,this, savedInstanceState,new PhotoPickManger.OnPhotoPickFinsh() {
public void onPhotoPick(List list) {
tv.setText(&&);
Toast.makeText(getApplicationContext(), &path:& + list.get(0).getPath() + & length:& + list.get(0).length(), Toast.LENGTH_SHORT).show();
tv.append(&path:& + list.get(0).getPath());
tv.append(&\nlength:& + new DecimalFormat(&#.##&).format((1.0d*list.get(0).length()/))+&MB&);
/**是否图片压缩*/
processImg();
/**是否在*/
pickManger.setCut(false);
pickManger.flushBundle();
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pickManger.clearCache();
pickManger.start(PhotoPickManger.Mode.SYSTEM_CAMERA);
/**图片压缩*/
private void processImg() {
pickManger.doProcessedPhotos(new PhotoPickManger.OnProcessedPhotos() {
public void onProcessed(List list) {
SimpleImageLoader.displayImage(list.get(0), img);
tv.append(&\nprogress length:& + new DecimalFormat(&#.##&).format((1.0d*list.get(0).length()/))+&MB&);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
pickManger.onActivityResult(requestCode, resultCode, data);
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
pickManger.onSaveInstanceState(savedInstanceState);
核心代码:
package me.lxz.photopicker.
import android.app.A
import android.content.I
import android.database.C
import android.graphics.B
import android.graphics.BitmapF
import android.graphics.C
import android.graphics.M
import android.graphics.P
import android.net.U
import android.os.B
import android.os.E
import android.os.H
import android.provider.MediaS
import android.util.L
import android.widget.T
import java.io.F
import java.io.FileInputS
import java.io.FileNotFoundE
import java.io.FileOutputS
import java.io.InputS
import java.text.SimpleDateF
import java.util.ArrayL
import java.util.D
import java.util.I
import java.util.L
import me.iwf.photopicker.PhotoPickerA
import me.iwf.photopicker.utils.PhotoPickerI
import me.lxz.photopicker.tools.PictureU
* 图片选择器
public final class PhotoPickManger {
public interface OnProcessedPhotos {
void onProcessed(List list);
public interface OnPhotoPickFinsh {
public void onPhotoPick(List list);
public enum Mode {
* 系统相机
*/SYSTEM_CAMERA,
* 系统图库
*/SYSTEM_IMGCAPTRUE,
* 类似微信图库
*/AS_WEIXIN_IMGCAPTRUE
* 用于区别哪一个图片选择器
private static String currentPickMangerN
private boolean isDebugToast =
* 用于区别其他图片选择器
public final String SAVE_STATIC_NAME = &save_currentPickMangerName&;
* 字段保存所已选择的图片
public final String SAVE_SELECTED_PHOTOS = &save_selected_photos&;
* 字段保存所拍照已选择的图片
public final String SAVE_CACHE_CAMERA = &save_cache_camera&;
* 字段保存所拍照是否选择裁剪
public final String SAVE_CACHE_IS_CUT = &save_cache_is_cut&;
* 字段保存待处理图片
public final String SAVE_CACHE_CUT_QUEUE = &save_cache_cut_queue&;
* 是否裁剪 只对系统相机和系统相册有效
private boolean isCut =
* 是否缩略
private boolean isOptimize =
* 设置返回最大图片数 对系统相机和相册调用无效
private int returnFileCount = 1;
private OnPhotoPickFinsh onPhotoPickF
private File tempF
private Handler handler = new Handler();
* 已经选择的拍照图片
public ArrayList selectsPhotos = new ArrayList&&();
* 用于缓存
* 系统拍照标示code
private final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
* 系统相册标示code
private final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
* 系统照片返回标示code
private final int PHOTO_REQUEST_CUT = 3;// 结果
* 仿微信相册返回code
public final static int AS_WEIXIN_REQUEST_CODE = 4;
* 如果多图待剪切则保存到该队列里
public ArrayList willCutOfFileQueue = new ArrayList&&();
* 图片缓存地址
public String cacheFilePath = &/img/&;
* 默认裁剪大小
public int defaultCutSize = 150;
* 至少大于多少的图片进行处理
public int needProcessFileLength = (int) 0.5 * 1024 * 1024;
* 构造方法
* @param name
为图片选择器默认一个别名 用来区别那一个选择器被选择了
* @param activity
* @param bundle
当系统内存不足时,重建时取出变量
* @param onPhotoPickFinsh 图片选择成功时回调
public PhotoPickManger(String name, Activity activity, Bundle bundle, OnPhotoPickFinsh onPhotoPickFinsh) {
this.onPhotoPickFinsh = onPhotoPickF
this.name =
this.activity =
this.bundle =
if (bundle != null) {
isCut = bundle.getBoolean(SAVE_CACHE_IS_CUT + &_& + name);
currentPickMangerName = bundle.getString(SAVE_STATIC_NAME);
willCutOfFileQueue = (ArrayList) bundle.getSerializable(SAVE_CACHE_CUT_QUEUE + &name&);
* 处理掉重建时的缓存
public void flushBundle() {
if (bundle != null) {
if (isDebugToast) {
Toast.makeText(activity, &bundle is refresh&, Toast.LENGTH_LONG).show();
selectsPhotos = (ArrayList) bundle.getSerializable(SAVE_SELECTED_PHOTOS + &_& + name);
if (selectsPhotos == null) {
selectsPhotos = new ArrayList&&();
tempFile = (File) bundle.getSerializable(SAVE_CACHE_CAMERA + &_& + name);
if (tempFile != null) {
if (tempFile.exists()) {
if (tempFile.length() & 0) {
if (!isCut) {
selectsPhotos.add(tempFile);
startPhotoZoom(Uri.fromFile(tempFile), defaultCutSize);
tempFile =
tempFile.delete();
tempFile =
bundle.remove(SAVE_CACHE_CAMERA + &_& + name);
if (!selectsPhotos.isEmpty()) {
if (onPhotoPickFinsh != null) onPhotoPickFinsh.onPhotoPick(selectsPhotos);
* 保存变量
public void onSaveInstanceState(Bundle savedInstanceState) {
this.bundle = savedInstanceS
if (selectsPhotos != null && !selectsPhotos.isEmpty()) {
savedInstanceState.putSerializable(SAVE_SELECTED_PHOTOS + &_& + name, selectsPhotos);
if (tempFile != null) {
savedInstanceState.putSerializable(SAVE_CACHE_CAMERA + &_& + name, tempFile);
savedInstanceState.putBoolean(SAVE_CACHE_IS_CUT + &_& + name, isCut);
savedInstanceState.putSerializable(SAVE_CACHE_CUT_QUEUE + &_& + name, willCutOfFileQueue);
savedInstanceState.putSerializable(SAVE_STATIC_NAME, currentPickMangerName);
* 如果是单一拍照,在拍照前应该清理缓存
public void clearCache() {
getSelectsPhotos().clear();
tempFile =
if (bundle != null) {
bundle.remove(SAVE_SELECTED_PHOTOS + &_& + name);
bundle.remove(SAVE_CACHE_CAMERA + &_& + name);
* 生成一个临时的缓存文件
private File getFile() {
File dir = new File(Environment.getExternalStorageDirectory().getPath()
+ cacheFilePath);
if (!dir.exists()) {
dir.mkdirs();
File file = new File(Environment.getExternalStorageDirectory()
.getPath() + cacheFilePath, getPhotoFileName());
// 使用系统当前日期加以调整作为照片的名称
private String getPhotoFileName() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat(
&'IMG'_yyyyMMdd_HHmmss&);
return dateFormat.format(date) + &.jpg&;
* // 调用系统的拍照功能
private void startCamearPicCut() {
tempFile = getFile();
Log.d(&test&, &start:& + tempFile.exists() + & & + tempFile.length());
// this.isCutOut =
// 调用系统的拍照功能
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(&camerasensortype&, 2);// 调用前置摄像头
intent.putExtra(&autofocus&, true);// 自动对焦
intent.putExtra(&fullScreen&, false);// 全屏
intent.putExtra(&showActionIcons&, false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
activity.startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
* 调用系统的相册
private void startImageCaptrue() {
tempFile = getFile();
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
&image/*&);
activity.startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
* 调用仿微信图库
private void startAsWeixinImageCaptrue() {
PhotoPickerIntent intent = new PhotoPickerIntent(activity);
intent.setPhotoCount(returnFileCount);
activity.startActivityForResult(intent, AS_WEIXIN_REQUEST_CODE);
public void start(Mode mode) {
currentPickMangerName = this.
switch (mode) {
case SYSTEM_CAMERA:
startCamearPicCut();
case SYSTEM_IMGCAPTRUE:
startImageCaptrue();
case AS_WEIXIN_IMGCAPTRUE:
startAsWeixinImageCaptrue();
* 回调onActivityResult事件
@SuppressWarnings(&unused&)
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
if (currentPickMangerName == null) {
if (bundle != null) {
currentPickMangerName = bundle.getString(SAVE_STATIC_NAME);
if (!currentPickMangerName.equals(name))
if (!currentPickMangerName.equals(name))
switch (requestCode) {
case PHOTO_REQUEST_TAKEPHOTO:
if (isCut) {
startPhotoZoom(Uri.fromFile(tempFile), defaultCutSize); // 裁剪
if (tempFile.length() == 0) {
tempFile.delete();
finish(tempFile);
case PHOTO_REQUEST_GALLERY:
if (isCut) {
if (data != null) {
startPhotoZoom(data.getData(), defaultCutSize);
File file =
String path = getRealPathFromURI(data.getData());
file = new File(path);
if (file == null)
if (file.length() == 0) {
file.delete();
finish(file);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
case PHOTO_REQUEST_CUT:
if (data != null) {
setCutPicToView(data);
flushCutPhotos();
case AS_WEIXIN_REQUEST_CODE:
List photos =
if (requestCode == AS_WEIXIN_REQUEST_CODE) {
if (data != null) {
photos = data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS);
if (!isCut) {
List list = new ArrayList();
if (photos != null && !photos.isEmpty()) {
for (String str : photos) {
list.add(new File(str));
finish(list);
if (photos != null && !photos.isEmpty()) {
/**如果只有一张照片可以直接裁剪 否则*/
if (photos.size() == 1) {
for (String str : photos) {
tempFile = getFile();
copyFile(str, tempFile.getAbsolutePath());
startPhotoZoom(Uri.fromFile(tempFile), defaultCutSize); // 裁剪
for (String str : photos) {
willCutOfFileQueue.add(new File(str));
tempFile = getFile();
copyFile(willCutOfFileQueue.get(0).getAbsolutePath(), tempFile.getAbsolutePath());
startPhotoZoom(Uri.fromFile(tempFile), defaultCutSize); // 裁剪
willCutOfFileQueue.remove(0);
} catch (Exception e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
* 如果多图处理裁剪队列
private void flushCutPhotos() {
if (willCutOfFileQueue != null && !willCutOfFileQueue.isEmpty()) {
tempFile = getFile();
copyFile(willCutOfFileQueue.get(0).getAbsolutePath(), tempFile.getAbsolutePath());
willCutOfFileQueue.remove(0);
startPhotoZoom(Uri.fromFile(tempFile), defaultCutSize); // 裁剪
* 文件复制
private void copyFile(String oldPath, String newPath) {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) {
//文件存在时
InputStream inStream = new FileInputStream(oldPath);
//读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum +=
//字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
inStream.close();
} catch (Exception e) {
e.printStackTrace();
* 对当前已选图片进行压缩
public void doProcessedPhotos(final OnProcessedPhotos on) {
if (getSelectsPhotos() != null && !getSelectsPhotos().isEmpty()) {
new Thread(new Runnable() {
public void run() {
for (Iterator it = getSelectsPhotos().iterator(); it.hasNext(); ) {
File file = it.next();
if (file.length() & needProcessFileLength) {
final Bitmap bm = PictureUtil.getSmallBitmap(file.getAbsolutePath(), 720, 1200);
FileOutputStream fos = new FileOutputStream(file);
bm.pressFormat.JPEG, 95, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
handler.post(new Runnable() {
public void run() {
on.onProcessed(getSelectsPhotos());
}).start();
* 图片选择完成并回调
private void finish(final File file) {
finish(createFiles(file));
* 图片选择完成并回调
private void finish(final List files) {
new Thread(new Runnable() {
public void run() {
for (Iterator it = files.iterator(); it.hasNext(); ) {
File file = it.next();
if (!file.exists() || file.length() == 0) {
it.remove();
changFile(file.getPath());
handler.post(new Runnable() {
public void run() {
selectsPhotos.addAll(files);
if (!files.isEmpty()) {
if (onPhotoPickFinsh != null) onPhotoPickFinsh.onPhotoPick(files);
tempFile =
flushCutPhotos();
}).start();
* 三星手机将横向图片转换为竖向
public void changFile(String file) {
BitmapFactory.Options options = new BitmapFactory.Options();
* 最关键在此,把options.inJustDecodeBounds =
* 这里再decodeFile(),返回的bitmap为空,但此时调用options.outHeight时,已经包含了图片的高了
options.inJustDecodeBounds =
BitmapFactory.decodeFile(file, options);
int width = options.outW
int height = options.outH
if (width & height) {
Bitmap bit = bitmapFromFile(file, width, height);
bit = adjustPhotoRotation(bit, 1);
pressFormat.PNG,100,new FileOutputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
* 旋转图片
public Bitmap adjustPhotoRotation(Bitmap bm, int count) {
int orientationDegree = 90;
Matrix m = new Matrix();
for (int i = 0; i & i++) {
m.setRotate(orientationDegree, bm.getWidth(),
bm.getHeight());
float targetX, targetY;
if (orientationDegree == 90) {
targetX = bm.getHeight();
targetY = 0;
targetX = bm.getHeight();
targetY = bm.getWidth();
final float[] values = new float[9];
m.getValues(values);
float x1 = values[Matrix.MTRANS_X];
float y1 = values[Matrix.MTRANS_Y];
m.postTranslate(targetX - x1, targetY - y1);
Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(),
Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
Canvas canvas = new Canvas(bm1);
canvas.drawBitmap(bm, m, paint);
// ?????bitmap????
bm.recycle();
return bm1;
private List createFiles(File file) {
List list = new ArrayList&&();
list.add(file);
* 根据Uri获得File文件路径
public String getRealPathFromURI(Uri contentUri) {
String res =
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = activity.getContentResolver().query(contentUri, proj,
null, null, null);
if (cursor.moveToFirst()) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
cursor.close();
* 启动裁剪
private void startPhotoZoom(Uri uri, int size) {
Log.d(&test&, uri.toString());
Intent intent = new Intent(&com.android.camera.action.CROP&);
intent.setDataAndType(uri, &image/*&);
// crop为true是设置在开启的intent中设置显示的view可以剪裁
intent.putExtra(&crop&, &true&);
// ectX aspectY 是宽高的比例
intent.putExtra(&aspectX&, 1);
intent.putExtra(&aspectY&, 1);
// outputX,outputY 是剪裁图片的宽高
intent.putExtra(&outputX&, size);
intent.putExtra(&outputY&, size);
intent.putExtra(&return-data&, true);
intent.putExtra(&noFaceDetection&, true);
activity.startActivityForResult(intent, PHOTO_REQUEST_CUT);
// 将进行剪裁后的图片显示到UI界面上
private void setCutPicToView(Intent picdata) {
Bundle bundle = picdata.getExtras();
if (bundle != null) {
Bitmap photo = bundle.getParcelable(&data&);
if (photo != null) {
FileOutputStream fOut =
fOut = new FileOutputStream(tempFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
pressFormat.JPEG, 100, fOut);
finish(tempFile);
* 获取一个指定大小的bitmap
* @param reqWidth
* @param reqHeight 目标高度
public Bitmap bitmapFromFile(String pathName, int reqWidth,
int reqHeight) {
if (reqHeight == 0 || reqWidth == 0) {
return BitmapFactory.decodeFile(pathName);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds =
BitmapFactory.decodeFile(pathName, options);
options = calculateInSampleSize(options, reqWidth,
reqHeight);
return BitmapFactory.decodeFile(pathName, options);
* 图片压缩处理(使用Options的方法)
* 说明 使用方法:
* 首先你要将Options的inJustDecodeBounds属性设置为true,BitmapFactory.decode一次图片 。
* 然后将Options连同期望的宽度和高度一起传递到到本方法中。
* 之后再使用本方法的返回值做参数调用BitmapFactory.decode创建图片。
* 说明 BitmapFactory创建bitmap会尝试为已经构建的bitmap分配内存
* ,这时就会很容易导致OOM出现。为此每一种创建方法都提供了一个可选的Options参数
* ,将这个参数的inJustDecodeBounds属性设置为true就可以让解析方法禁止为bitmap分配内存
* ,返回值也不再是一个Bitmap对象, 而是null。虽然Bitmap是null了,但是Options的outWidth、
* outHeight和outMimeType属性都会被赋值。
* @param reqWidth
目标宽度,这里的宽高只是阀值,实际显示的图片将小于等于这个值
* @param reqHeight 目标高度,这里的宽高只是阀值,实际显示的图片将小于等于这个值
public BitmapFactory.Options calculateInSampleSize(
final BitmapFactory.Options options, final int reqWidth,
final int reqHeight) {
// 源图片的高度和宽度
final int height = options.outH
final int width = options.outW
int inSampleSize = 1;
if (height & reqHeight || width & reqWidth) {
// 计算出实际宽高和目标宽高的比率
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width
/ (float) reqWidth);
// 选择宽和高中最小的比率作为inSampleSize的值,这样可以保证最终图片的宽和高
// 一定都会大于等于目标的宽和高。
inSampleSize = heightRatio & widthRatio ? heightRatio
// 设置压缩比例
options.inSampleSize = inSampleS
options.inJustDecodeBounds =
public Activity getActivity() {
public PhotoPickManger setActivity(Activity activity) {
this.activity =
public boolean isCut() {
return isC
public PhotoPickManger setIsCut(boolean isCut) {
this.isCut = isC
public boolean isOptimize() {
return isO
public PhotoPickManger setIsOptimize(boolean isOptimize) {
this.isOptimize = isO
public int getReturnFileCount() {
return returnFileC
public PhotoPickManger setReturnFileCount(int returnFileCount) {
this.returnFileCount = returnFileC
public OnPhotoPickFinsh getOnPhotoPickFinsh() {
return onPhotoPickF
public PhotoPickManger setOnPhotoPickFinsh(OnPhotoPickFinsh onPhotoPickFinsh) {
this.onPhotoPickFinsh = onPhotoPickF
public boolean isDebugToast() {
return isDebugT
public PhotoPickManger setDebugToast(boolean isDebugToast) {
this.isDebugToast = isDebugT
public void setCut(boolean isCut) {
this.isCut = isC
public Bundle getBundle() {
public PhotoPickManger setBundle(Bundle bundle) {
this.bundle =
public File getTempFile() {
return tempF
public ArrayList getSelectsPhotos() {
return selectsP
public int getNeedProcessFileLength() {
return needProcessFileL
public PhotoPickManger setNeedProcessFileLength(int needProcessFileLength) {
this.needProcessFileLength = needProcessFileL
public static String getCurrentPickMangerName() {
return currentPickMangerN
public static void setCurrentPickMangerName(String currentPickMangerName) {
PhotoPickManger.currentPickMangerName = currentPickMangerN
public String getCacheFilePath() {
return cacheFileP
public PhotoPickManger setCacheFilePath(String cacheFilePath) {
this.cacheFilePath = cacheFileP
public String getName() {
public PhotoPickManger setName(String name) {
this.name =
public PhotoPickManger setTempFile(File tempFile) {
this.tempFile = tempF
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'}

我要回帖

更多关于 怎么把拍摄的录像压缩 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信