Uploading High res images in android

不羁的心 提交于 2019-12-22 09:24:03

问题


Hi I wanted to upload high res images to my php server.Right now i am sending images to my server using encoded string.Problem here is i can not upload high res image it leads me to Out of memory. My image is around 1-2 Mb large in size. Here is a snippet how i upload image to server.

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;
    }

I am sending this encoded string in json object. Is there any better approach i can go for? If i go beyond 150X150 it leads me to OOM. I want to upload high res image file without loosing its quality.


回答1:


I have used MultipartEntity from Apache Commons library (httpmime.jar) in my POST request to upload picture file:

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

Then create simple HttpPost, set Entity to it and send it to server:

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

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


来源:https://stackoverflow.com/questions/18712197/uploading-high-res-images-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!