Android图片在上传之前调整大小(重新采样/缩减采样)

时间:2013-03-20 21:21:47

标签: android http bitmap resize

private void doFileUpload(){

    File file1 = new File(selectedPath1);

    String urlString = "http://example.com/upload_media_test.php";
    try
    {
         HttpClient client = new DefaultHttpClient();
         HttpPost post = new HttpPost(urlString);
         FileBody bin1 = new FileBody(file1);

         MultipartEntity reqEntity = new MultipartEntity();
         reqEntity.addPart("uploadedfile1", bin1);
         reqEntity.addPart("user", new StringBody("User"));
         post.setEntity(reqEntity);

         HttpResponse response = client.execute(post);
         resEntity = response.getEntity();
         final String response_str = EntityUtils.toString(resEntity);

         if (resEntity != null) {
             Log.i("RESPONSE",response_str);
             runOnUiThread(new Runnable(){
                    public void run() {
                         try {
                            res.setTextColor(Color.GREEN);
                            res.setText("n Response from server : n " + response_str);
                            Toast.makeText(getApplicationContext(),"Upload Complete. Check the server uploads directory.", Toast.LENGTH_LONG).show();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                       }
                });
         }
    }
    catch (Exception ex){
         Log.e("Debug", "error: " + ex.getMessage(), ex);
    }
  }

我从互联网上获取此代码。这是工作,但当我尝试上传比1Mbyte更大的文件,

我在文件大小时遇到​​错误。

我知道如何调整位图图像的大小,但我不知道上传调整大小的位图图像。

如何调整文件大小并上传?

提前致谢

2 个答案:

答案 0 :(得分:2)

您不仅需要调整位图大小,还需要将结果编码为.jpg图像。因此,您必须打开文件并将其转换为Bitmap,将Bitmap调整为较小的图像,将图像编码为byte []数组,然后以与上传文件相同的方式上传byte []数组file1

如果Bitmap很大,很明显,你将没有足够的堆内存来打开整个内容,所以你必须用BitmapFactory.Options.inSampleSize打开它。

首先,打开缩小尺寸的Bitmap:

Uri uri = getImageUri(selectedPath1);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4; // Example, there are also ways to calculate an optimal value.
InputStream in = in = contentResolver.openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(in, null, options);

接下来,编码为byte[]数组:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
byte[] bitmapdata = bos.toByteArray();

最后,使用reqEntity.addPart()直接添加byte[]数组,或写入较小的文件并添加文件,如当前示例所示。

答案 1 :(得分:0)

这个答案is based on another answer,但我对该代码有一些问题,所以我发布了编辑过的代码。

我是这样做的:

BitmapFactory.Options options = new BitmapFactory.Options();
options.outWidth = 50; //pixels
options.outHeight = 50; //pixels
InputStream in = context.getContentResolver().openInputStream(data.getData()); // here, you need to get your context.
Bitmap bitmap = BitmapFactory.decodeStream(in, null, options);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] bitmapdata = baos.toByteArray();

请注意, data 是您用于获取文件的Intent返回的数据。如果您已有文件路径,只需使用...

现在,当您创建HTTP实体时,请添加:

 FormBodyPart fbp = new FormBodyPart("image", new ByteArrayBody(baos.toByteArray(), "image/jpeg", "image"));
 entity.addPart(fbp);

另外,请注意您需要 MultipartEntity 来上传文件。