Android - 在将位图保存到活动中的 SDCARD 之前压缩位图以获取结果

发布于 2024-12-23 08:16:16 字数 2510 浏览 1 评论 0原文

我一直在为此绞尽脑汁,不知道该怎么办。我想做的是:拍摄一张照片,将其压缩为png(保留原始尺寸),然后将其保存到sdCard。我需要这样做的原因是因为我必须再次重新压缩它,然后对其进行 Base64 编码,以便我可以将其发送到服务器。问题是 1. 文件太大 2. 我的内存不足,3. 不确定我是否正确执行此操作。

感谢您的帮助

这是我的代码:

@Override
public void onClick(View button) {
    switch (button.getId()) {
        case R.id.cameraButton:
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(new File("/sdcard/test.png")));
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
            break;
        case R.id.galleryButton:
            sendToDatabase();
            break;
    }
}

// Camera on activity for result - save it as a bmp and place in imageview
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST) {
        // do something
    }

    if (resultCode == Activity.RESULT_OK) {
        Log.d(TAG, "result ok");

        picture = BitmapFactory.decodeFile("/sdcard/test.png");

        // Create string to place it in sd card
        String extStorageDirectory = Environment
                .getExternalStorageDirectory().toString();
        //create output stream
        OutputStream outputStream = null;
        //create file
        File file = new File(extStorageDirectory, "test.png");
        try {
            outputStream = new FileOutputStream(file);
            picture.compress(Bitmap.CompressFormat.PNG, 80, outputStream);
            //picture.recycle();
            outputStream.flush();
            outputStream.close();
        } catch (IOException e){
            Log.d(TAG, "ERROR");
        }
        imageView.setImageBitmap(picture);
    }
}

public void sendToDatabase() {
    InputStream inputStream = null;

    //get the picture from location
    picture = BitmapFactory.decodeFile("/sdcard/test.png");

    // CONVERT:
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    Boolean didItWork = picture.compress(Bitmap.CompressFormat.PNG, 50, outStream);
    picture.recycle();
    if (didItWork = true) {
        Log.d(TAG, "compression worked");
    }
    Log.d(TAG, "AFTER. Height: " + picture.getHeight() + " Width: "
        + picture.getWidth());
    final byte[] ba = outStream.toByteArray();
    try {
        outStream.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

I've been busting my head on this, and not so sure what to do. What I am trying to do is: Take a picture, compress it to png (keeping the original dimensions), and then save it to sdCard. The reason why I need to do this is because I have to recompress it again, then Base64 encode it so i can send it to a server. The problem is 1. the file is too big 2. i am running out of memory and 3. not sure if I am doing this correctly.

Thanks for your help

Here is my code:

@Override
public void onClick(View button) {
    switch (button.getId()) {
        case R.id.cameraButton:
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(new File("/sdcard/test.png")));
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
            break;
        case R.id.galleryButton:
            sendToDatabase();
            break;
    }
}

// Camera on activity for result - save it as a bmp and place in imageview
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST) {
        // do something
    }

    if (resultCode == Activity.RESULT_OK) {
        Log.d(TAG, "result ok");

        picture = BitmapFactory.decodeFile("/sdcard/test.png");

        // Create string to place it in sd card
        String extStorageDirectory = Environment
                .getExternalStorageDirectory().toString();
        //create output stream
        OutputStream outputStream = null;
        //create file
        File file = new File(extStorageDirectory, "test.png");
        try {
            outputStream = new FileOutputStream(file);
            picture.compress(Bitmap.CompressFormat.PNG, 80, outputStream);
            //picture.recycle();
            outputStream.flush();
            outputStream.close();
        } catch (IOException e){
            Log.d(TAG, "ERROR");
        }
        imageView.setImageBitmap(picture);
    }
}

public void sendToDatabase() {
    InputStream inputStream = null;

    //get the picture from location
    picture = BitmapFactory.decodeFile("/sdcard/test.png");

    // CONVERT:
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    Boolean didItWork = picture.compress(Bitmap.CompressFormat.PNG, 50, outStream);
    picture.recycle();
    if (didItWork = true) {
        Log.d(TAG, "compression worked");
    }
    Log.d(TAG, "AFTER. Height: " + picture.getHeight() + " Width: "
        + picture.getWidth());
    final byte[] ba = outStream.toByteArray();
    try {
        outStream.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撕心裂肺的伤痛 2024-12-30 08:16:16

当您执行 picture.compress(Bitmap.compressFormat.PNG, 50, outStream); 时压缩不会像无损的 PNG 一样工作,会忽略质量设置。所以参数 50 在这种情况下不起作用。所以我建议您将 compressformat.png 更改为 compressformat.jpeg。

When you do picture.compress(Bitmap.CompressFormat.PNG, 50, outStream); the compression wont work as PNG which is lossless, will ignore the quality setting. So the parameter 50 wont work in this case. So i suggest you to change CompressFormat.PNG to CompressFormat.JPEG.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文