【问题标题】:How to save a bitmap that I got from camera, to the sd card without losing it's original size and quality如何将我从相机获得的位图保存到 sd 卡而不丢失其原始大小和质量
【发布时间】:2011-05-19 06:40:24
【问题描述】:

如何在丢失原始大小或质量的情况下将位图图像保存到 sdcard,我目前正在使用此代码,但它始终保存的图像比相机捕获的图像的原始大小小得多。


private void SaveImage(Bitmap bitmap, String strType) {
        File sdImageMainDirectory = new File("/sdcard/");
        if (sdImageMainDirectory.exists()) {
            FileOutputStream fileOutputStream = null;
            Date dt = new Date();
            String nameFile = "myImage";
            int quality = 100;

        try {
            String strFullImageName;
            strFullImageName = UserID + "_" + nameFile + ".jpg";
            fileOutputStream = new FileOutputStream(
                    sdImageMainDirectory.toString() + "/"
                            + strFullImageName);
            BufferedOutputStream bos = new BufferedOutputStream(
                    fileOutputStream);
            bitmap.compress(CompressFormat.JPEG, quality, bos);
            bos.flush();
            bos.close();
            UploadImage(bitmap, strFullImageName, strType);
        } catch (FileNotFoundException e) {
            Utilities.LogError(e);
        } catch (IOException e) {
            Utilities.LogError(e);
        } catch (Exception eee) {
            Utilities.LogError(eee);
        }

    } else {
        Utilities.LogError("File not Found");
    }
}

找到解决办法,

点击 KPBird 提供的链接后,我发现问题在于相机活动如何返回捕获的位图对象。

为了保护内存,相机 Activity 返回已捕获照片的缩放图像,因此解决方案是永远不要使用此缩放图像!

只需向相机 Intent 传递一个额外的内容,提供您要保存图像的 url,瞧!捕获的照片会以原始比例保存在您想要的位置

代码:



private static final int CAMERA_PIC_REQUEST = 1337;

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

imageNameToUpload =  "myImage.jpeg";

cameraIntent.putExtra( android.provider.MediaStore.EXTRA_OUTPUT,        Uri.fromFile(new File(                  imagePathToUpload+imageNameToUpload)));
                                startActivityForResult(cameraIntent,
                                        CAMERA_PIC_REQUEST);

感谢 KPBird!

【问题讨论】:

    标签: android


    【解决方案1】:

    【讨论】:

    • 我试过了,这个链接,它可以添加一个额外的,相机现在正在按原样保存捕获的图像,但在我点击相机上的保存按钮后,整个设备冻结,奇怪,这是一个确定的错误吗?
    • 额外的添加是这样的:cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File("/sdcard/tmphassamok.jpeg")));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 2020-07-28
    相关资源
    最近更新 更多