Android - Resize&发送之前裁剪图片而不保存

时间:2013-04-23 11:50:50

标签: android resize crop

请原谅我的英语不好,我是法国人!

在我的Android应用中,我需要调整图片并从图库裁剪图片,然后再将其发送到服务器没有保存它。

这里我的代码发送到服务器:

    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;

    String pathToOurFile = imagePath;
    String urlServer = "http://ip/serverApp/upload/transfert.php";
    Log.e("UploadImage", urlServer);
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";

    try
    {
        File file = new File(imagePath);
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] bytes = new byte[(int) file.length()];
        fileInputStream.read(bytes);
        fileInputStream.close();

        URL url = new URL(urlServer);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
        connection.setDoOutput(true);
        outputStream = new DataOutputStream( connection.getOutputStream() );
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
        outputStream.writeBytes(lineEnd);

        int bufferLength = 1024;
        for (int i = 0; i < bytes.length; i += bufferLength) {
            int progress = (int)((i / (float) bytes.length) * 100);
            publishProgress(progress);
            if (bytes.length - i >= bufferLength) {
                outputStream.write(bytes, i, bufferLength);
            } else {
                outputStream.write(bytes, i, bytes.length - i);
            }
        }

        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        outputStream.close();
        outputStream.flush();

        InputStream inputStream = connection.getInputStream();

        // read the response
        inputStream.close();

        int serverResponseCode = connection.getResponseCode();
        String serverResponseMessage = connection.getResponseMessage();


        Log.w("Upload image", "Response -> Code:"+serverResponseCode+" Message:"+serverResponseMessage);

        return serverResponseCode;
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }

现在我需要编码以调整大小并裁剪图片,以便拥有350px / 350px大小的图像。

你知道我怎么做吗?

非常感谢。

2 个答案:

答案 0 :(得分:2)

好的!!!! 正确的方法请遵循以下代码!

但是:很好,这是一个例子! - &GT; 您应该在主线上进行互联网请求

执行此代码时,函数exec();应该放入&#34; doInBackground()&#34;一个asyncTask<Object, Object, Object>();

startActivityForResult()的{​​{1}}和覆盖应该是一个活动类

告诉我它是否正确!!!!

onActivityResult()

答案 1 :(得分:0)

这个答案[基于另一个答案] [1],但我对该代码有一些问题,所以我发布了编辑和工作的代码。

我是这样做的:

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 来上传文件。