0

我想调整 sd 卡上的图像大小并将其发送到 Web 服务,调用是异步的,ParcelFileDescriptor用于CountingInputStreamEntity显示进度条。我应该怎么做才能调整大小并以相同的方式发送!这是我当前的代码:

ParcelFileDescriptor fileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r");
InputStream in = context.getContentResolver().openInputStream(uri);
CountingInputStreamEntity entity = new CountingInputStreamEntity(in, fileDescriptor.getStatSize());
entity.setUploadListener(this);
entity.setContentType("application/octet-stream");
put.setEntity(entity);

提前致谢。

4

2 回答 2

2

可以使用BitmapFactory.Options重新调整图像大小

您需要创建一个新的小宽度和高度的图像,如下所示:

private Bitmap decodeFile(File f){
    Bitmap b = null;
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        FileInputStream fis = new FileInputStream(f);
        BitmapFactory.decodeStream(fis, null, o);
        fis.close();

        int scale = 1;
        if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
            scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        fis = new FileInputStream(f);
        b = BitmapFactory.decodeStream(fis, null, o2);
        fis.close();
    } catch (IOException e) {
    }
    return b;
}
于 2012-06-29T08:31:25.177 回答
0

要调整相机图像的大小,您可以在onActivitResult().

                 BitmapFactory.Options options = new BitmapFactory.Options();
                 options.inSampleSize = 8;

                 Bitmap receipt = BitmapFactory.decodeFile(photo.toString(),options);  

您可以在其中调整图像大小options.inSampleSize,然后按照您的样式将该图像传递给 Web 服务。

我希望它有帮助..

于 2012-06-29T08:29:35.513 回答