安卓拍照并保存在SD卡之前调整其大小并保存、大小、SD

2023-09-04 06:58:06 作者:陌笑已徒然

我希望我的code保存前调整图像大小,但我不能找到它在谷歌任何东西。 你能帮助我吗?

I want my code to resize the image before saving but I can't find anything about it on Google. Could you help me please ?

这是在code(从Android电子文档):

This is the code (from Android doc) :

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
    File f = new File(mCurrentPhotoPath);

    picturePathForUpload = mCurrentPhotoPath;

    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

在那之后,我必须把它上传到服务器。

After that, I have to upload it to a server.

多谢了。

推荐答案

您可以保存位图图像下面code

You can save Bitmap image following code

Bitmap photo = (Bitmap) "your Bitmap image";
photo = Bitmap.createScaledBitmap(photo, 100, 100, false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

File f = new File(Environment.getExternalStorageDirectory()
        + File.separator + "Imagename.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();