Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

android - java.lang.outofmemoryerror bitmap size exceeds vm budget on bitmap

In my app I'm displaying images from galley and on selection of image i want to upload that image to web server.For uploading image to server I'm using following code but I'm getting error at bitmapImage = BitmapFactory.decodeFile(path,opt);

private void uploadImage(String selectedImagePath) {
        String str = null;
        byte[] data = null;
        String  Responce= null;
        Bitmap bitmap2 = null;
        try {
            File file=new File(selectedImagePath);
            //FileInputStream fileInputStream = new FileInputStream(new File(imagePath2) );
            FileInputStream fileInputStream=new FileInputStream(selectedImagePath);
            Log.i("Image path 2",""+selectedImagePath+"
"+fileInputStream);
            name=file.getName();
            name=name.replace(".jpg","");
            name=name.concat(sDate).concat(".jpg");
            Log.e("debug",""+name);

            //else
            //{
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inTempStorage = new byte[16*1024];

            //bitmapImage = BitmapFactory.decodeFile(path,opt);

            bitmap2=BitmapFactory.decodeFileDescriptor(fd, outPadding, opts)
            Log.i("Bitmap",""+bitmap.toString());
            BitmapFactory.decodeStream(fileInputStream);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap2.compress(Bitmap.CompressFormat.PNG, 100, baos);
            data = baos.toByteArray();
            str=Base64.encodeBytes(data);
            //}

            //String image=str.concat(sDate);
            ArrayList<NameValuePair> nameValuePairs = new
            ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("image",str));
            nameValuePairs.add(new BasicNameValuePair("imagename", name));
            Log.e("debug",""+nameValuePairs.toString());

            HttpClient client=new DefaultHttpClient();
            HttpPost post=new HttpPost("http://ufindfish.b4live.com/uploadTipImage.php");

            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse httpResponse=client.execute(post);
            HttpEntity entity=httpResponse.getEntity();
            InputStream inputStream=entity.getContent();

            StringBuffer builder=new StringBuffer();
            int ch;
            while( ( ch = inputStream.read() ) != -1 )
            {
                builder.append((char)ch);
            }
            String s=builder.toString();
            Log.i("Response",""+s);



        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(bitmap2!=null)
        {
            bitmap2.recycle();
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Error is due to the size of the image, i used this code to decrease the size of image when select from gallery.

public Bitmap setImageToImageView(String filePath) 
    { 
    // Decode image size 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(filePath, o); 

    // The new size we want to scale to 
    final int REQUIRED_SIZE = 1024; 

    // Find the correct scale value. It should be the power of 2. 
    int width_tmp = o.outWidth, height_tmp = o.outHeight; 
    int scale = 1; 
    while (true) 
    { 
    if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) 
    break; 
    width_tmp /= 2; 
    height_tmp /= 2; 
    scale *= 2; 
    } 

    // Decode with inSampleSize 
    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize = scale; 
    Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2); 
    return bitmap;

    }

i hope this may helps you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...