Android - 在将位图保存到结果 Activity 中的 SDCARD 之前压缩位图

标签 android memory bitmap compression android-camera

我一直在为这件事绞尽脑汁,但不太确定该怎么做。我想做的是:拍照,将其压缩为 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();
    }
}

最佳答案

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

关于Android - 在将位图保存到结果 Activity 中的 SDCARD 之前压缩位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8659335/

相关文章:

Android Wear Watch Face - INVALID_ACCOUNT 尝试连接到 Google Services API 时

java - 无法定位符号 "__android_log_write"- Android 原生日志记录

java - 在 Java 中创建一个非常非常大的 map

android - picasso - 如何获得真实的图像尺寸?

java - getReseources() 位图数组出现 NullPointerException

java - 在Android中绘制大位图

android - 使用没有具体 Activity.class 的广播接收器进行集成测试

android - 动态 AndroidManifest.xml

linux - Python读取Linux内存过程报错(/proc/$pid/mem)

c - C程序的内存分配