在android中上传高分辨率图像

时间:2013-09-10 06:41:35

标签: android android-image android-networking uploading

您好我想将高分辨率图像上传到我的php服务器。现在我正在使用编码的字符串将图像发送到我的服务器。这里的问题是我无法上传高分辨率图像它导致我内存不足。我的图像大小约为1-2 Mb。以下是我将图片上传到服务器的片段。

String encodedPagerpath="";
                if(!FILE_PAGER_PATH.equals("") && FILE_PAGER_PATH.length()!=0)
                {
                    byte [] b=imageTobyteArray(FILE_PAGER_PATH,150,150);
                    if(b!=null)
                    {
                        encodedPagerpath=Base64.encodeToString(b, Base64.DEFAULT);
                        if(encodedPagerpath==null || encodedPagerpath.equals("")){
                            encodedPagerpath="";
                        }
                    }
                }

//Convert image into byte array
    static  byte[] imageTobyteArray(String path,int width,int height)
    {   byte[] b = null ;
    if(!path.equals("") || path.length()!=0)
    {


        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inDither=true;//optional
        options.inPreferredConfig=Bitmap.Config.RGB_565;//optional

        Bitmap bm = BitmapFactory.decodeFile(path,options);

        options.inSampleSize = calculateInSampleSize(options, width, height);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        bm=BitmapFactory.decodeFile(path,options);


        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        bm.compress(Bitmap.CompressFormat.JPEG, 70, baos); //bm is the bitmap object   
        b= baos.toByteArray();
    }
    return b;
    }

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            // Calculate ratios of height and width to requested height and width
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }

        return inSampleSize;
    }

我在json对象中发送此编码字符串。有什么更好的方法我可以去吗?如果我超过150X150,它会把我带到OOM。我想上传高分辨率图像文件而不会失去其质量。

1 个答案:

答案 0 :(得分:1)

我在我的POST请求中使用了来自Apache Commons库(httpmime.jar)的MultipartEntity来上传图片文件:

public static HttpEntity createMultipartBody(File file) throws IOException{
    MultipartEntity entity = new MultipartEntity();
    entity.addPart(key, new FileBody(file));
    return entity;
} 

然后创建简单的HttpPost,将Entity设置为它并将其发送到服务器:

HttpPost httpPostRequest = new HttpPost(SOME_URL);
httpPostRequest.setEntity(createMultipartBody(SOME_FILE_TO_UPLOAD));

HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPostRequest);