zxd_72 2013-08-21 08:05 采纳率: 0%
浏览 3204

Android –压缩 bitmap 再保存到 SDCARD

我想实现的是:拍照片,然后压缩成 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();
        }
  • 写回答

1条回答 默认 最新

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-01-13 20:06
    关注

    您正在尝试使用相机拍照并将图像存储到SD卡中,然后使用Base64编码将图像发送到服务器。您提到了文件太大,内存耗尽和不确定这样做是否正确的问题。


    关于文件太大的问题,您可以在压缩图像之前对其进行重新缩放,以减小文件大小。关于内存耗尽的问题,您可以在使用完图像后将其释放,以释放内存。


    关于代码的正确性,我建议您确保您有足够的权限来访问SD卡并在其上创建文件。您还需要确保您在使用照片之前正确地检查了照片是否已捕获。


    我还建议您使用 try-catch 块来捕获并处理可能的IO异常。

    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集