【发布时间】:2015-11-10 15:20:14
【问题描述】:
我正在尝试调整捕获图像的大小,使其尺寸小于 1Mb。这是我迄今为止在 OnActivityResult 中尝试过的,但失败了。我从Android take photo and resize it before saving on sd card得到教程
ImageFitScreen.java
try {
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = false;
bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
bitmapOptions.inDither = true;
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
Global.img = bitmap;
b.setImageBitmap(bitmap);
String path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Phoenix" + File.separator + "default";
//p = path;
f.delete();
OutputStream outFile = null;
File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".png");
try {
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outFile);
//pic=file;
outFile.flush();
outFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
Claims.java
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if ((name != null && name.trim().length() > 0) && (result != null && result.trim().length() > 0)) {
// Toast.makeText(getActivity().getApplicationContext(), fk+"", Toast.LENGTH_LONG).show();
byte[] data=getBitmapAsByteArray(getActivity(),Global.img);// this is a function
Toast.makeText(getActivity().getApplicationContext(), data+"", Toast.LENGTH_LONG).show();
if(data==null)
{
Toast.makeText(getActivity(), "null", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getActivity(), " not null", Toast.LENGTH_LONG).show();
SB.insertStaffBenefit(name, data, description, result, fk);
}
});
return claims;
}
public static byte[] getBitmapAsByteArray(final Context context,Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
Toast.makeText(context, outputStream.size()/1024+"KB", Toast.LENGTH_LONG).show();
return outputStream.toByteArray();
}
在Claims.java中仍然显示大于1Mb的大小
有人可以帮助我吗?谢谢
【问题讨论】:
-
请完整而准确地解释“失败”是什么意思。例如,你在崩溃吗?如果是这样,请使用 LogCat 检查与崩溃相关的 Java 堆栈跟踪:stackoverflow.com/questions/23353173/…
-
@CommonsWare 我很抱歉。我已经编辑了我的帖子。
-
这是正确的实现方式吗?还是我在错误的地方实施?
标签: java android size android-camera