0
我想減少從相機和畫廊(1 + MB到150K兩個)拍攝的照片的大小。調整從+ 1mb調整圖像從相機意圖<= 150k Android
代碼拍照:
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卡上的照片。
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap –
爲什麼你有一個循環來搜索文件,而不是'F =新文件(f,「temp.jpg」);'直接? – njzk2
nota:PNG在android中是無損的。質量參數被忽略。使用JPG是你需要減小尺寸。 – njzk2