【发布时间】:2014-01-30 12:52:41
【问题描述】:
我正在尝试减小从相机和画廊拍摄的照片的大小(1+mb 到 150k。两者)。
-
拍照代码:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //intent.putExtra("crop", "true"); intent.putExtra("outputX", 50); intent.putExtra("outputY", 50); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 128); intent.putExtra("outputY", 128); intent.putExtra("scaleUpIfNeeded", true); intent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("return-data", false); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination)); startActivityForResult(intent, PICTURE_RESULT); -
打算从图库中选择
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setType("image/*"); //intent.putExtra("crop", "true"); intent.putExtra("outputX", 50); intent.putExtra("outputY", 50); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("scale", true); intent.putExtra("scaleUpIfNeeded", true); intent.putExtra("return-data", true); intent.putExtra(MediaStore.EXTRA_OUTPUT,destination); intent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString()); startActivityForResult(Intent.createChooser(intent, "Choisir Photo"),SELECT_FILE);
我的活动结果如下
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == PICTURE_RESULT) {
if (requestCode == REQUEST_CAMERA) {
Editer.PHOTO_FROM=11;
File f = new File(Environment.getExternalStorageDirectory()
.toString());
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
//Uri.fromFile(createFile());
Constant.filePath=f.getAbsolutePath();
String fname = "user_image_golf.jpg";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16 * 1024];
options.inSampleSize = 4;
options.outWidth = 100;
options.outHeight = 100;
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "user_image_golf.jpg"); // == /
myDir.mkdirs();
destination = new
File(Environment.getExternalStorageDirectory(),"user_image_golf.jpg");
Bitmap bitmap = BitmapFactory.decodeFile(destination.toString(), options);
Bitmap map = Bitmap.createScaledBitmap(bitmap, 100, 100, true);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
map.compress(Bitmap.CompressFormat.PNG, 50, bao);
//byte[] ba = bao.toByteArray();
File file = new File (myDir, f.getAbsolutePath());
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
map.compress(Bitmap.CompressFormat.JPEG, 20, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("//imagedata=Base64.encodeBytes(ba);");
///imagedata=Base64.encodeBytes(ba);
//rotateImage(Uri.fromFile(destination).toString());
}
else if (requestCode == SELECT_FILE) {
Uri selectedImageUri = data.getData();
Constant.filePath =selectedImageUri.toString();
Editer.PHOTO_FROM=2;
System.out.println("getAbsolutePath() "+selectedImageUri);
System.out.println("selectedImageUri-getAbsolutePath()
"+getPath(selectedImageUri,m));
}
}
m.finish();
}
如果我从图库中选择照片或从相机拍摄照片,则相机中的新照片或图库中最新选择的图像应覆盖保存在 sd 卡上的照片。
【问题讨论】:
-
为什么会有循环搜索文件而不是直接搜索
f = new File(f, "temp.jpg");? -
注意:PNG 在 android 中是无损的。质量参数被忽略。使用JPG是你需要减小尺寸。
标签: android android-intent android-camera