【问题标题】:java.lang.outofmemoryerror bitmap size exceeds vm budget on bitmapjava.lang.outofmemoryerror 位图大小超出位图上的 vm 预算
【发布时间】:2011-11-16 01:50:53
【问题描述】:

在我的应用程序中,我正在显示来自厨房的图像,并且在选择图像时,我想将该图像上传到 Web 服务器。为了将图像上传到服务器,我正在使用以下代码,但出现错误 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+"\n"+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();
        }

【问题讨论】:

    标签: android


    【解决方案1】:

    错误是由于图像的大小,我在从图库中选择时使用此代码减小图像的大小。

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

    希望对你有帮助。

    【讨论】:

    • @abhi...感谢您的回复..但我仍然收到错误..1 个小问题...在获取位图后,即 **Bitmap bitmap = Bitmap Factory.decode File(file路径,o2); ** 在此之后你如何将其转换为 base 64 字符串...我为此 bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 使用以下代码数据 = baos.toByteArray(); str=Base64.encodeBytes(数据);
    • @abhi...谢谢我已经解决了这个问题...现在它工作正常...非常感谢您...
    • @Abhi 我使用了你的代码,它几乎解决了我的错误,但由于这种方法,我遇到了一个问题。这种方法正在旋转我的原始图像。而我不想要的。我想像在画廊中一样显示我的图像。有没有办法做到这一点?
    • 嗨@anddev 上面的函数中没有旋转图像的代码,它只会缩放图像。在你的最后检查
    • @Abhi 感谢您的快速回复,但我从最后使用不同的方法进行了检查,但仍有一些图像会旋转并显示到 imageview 中。所以问题不在于我的错误上面的代码。我对此感到抱歉。但是现在你能建议我解决我的问题的正确方法吗?谢谢
    【解决方案2】:

    你必须在加载图像时闯入样本,已经有一个问题和一个很好的答案,看看这个页面,这可能会对你有所帮助。

    Strange out of memory issue while loading an image to a Bitmap object

    【讨论】:

      猜你喜欢
      • 2012-08-19
      • 2011-04-26
      • 1970-01-01
      • 2010-12-29
      相关资源
      最近更新 更多