简体   繁体   English

上载base64会导致Android应用中出现内存不足错误

[英]Uploading base64 results in a outofmemory error in a Android app

I'm back with a problem. 我回来了一个问题。

I'm trying to create a Android application that allows the user to search a image from their gallery and upload it. 我正在尝试创建一个Android应用程序,允许用户从其图库中搜索图像并上传。 However on high resolution images the application will crash with a out of memory error. 但是,在高分辨率图像上,应用程序将因内存不足错误而崩溃。

The image is 1,7MB and a resolution of 1944x1944px. 图片为1,7MB,分辨率为1944x1944px。

After the users opens a picture from their gallery it will be loaded in a imageview, I already resized the picture to 150x150 pixels to prevent crashes there. 用户从其图库中打开图片后,它将被加载到imageview中,我已经将图片的尺寸调整为150x150像素,以防止崩溃。

Code: 码:

            Button buttonUploadImage = (Button) findViewById(R.id.uploadImage);
        buttonUploadImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view){
                dialog = ProgressDialog.show(MainActivity.this, "", 
                        "Uploading...", true);  

                    new Thread(){
                        @Override
                        public void run(){



                        Bitmap bitmap = BitmapFactory.decodeFile(picturePath);         
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);    bitmap.recycle();
                        byte [] byte_arr = stream.toByteArray();


                        String image_str = Base64.encodeToString(byte_arr, columnIndex2);
                        bitmap = null;
                        System.gc();


                        try{

                            HttpClient httpclient = new DefaultHttpClient();
                            HttpPost httppost = new HttpPost("http://example.com/android.php");

                            List<NameValuePair> pairs = new ArrayList<NameValuePair>();
                            pairs.add(new BasicNameValuePair("image", image_str));
                            httppost.setEntity(new UrlEncodedFormEntity(pairs));
                            HttpResponse response = httpclient.execute(httppost);
                            inputStream = response.getEntity().getContent();
                            //Json stuff here to handle the response and see if it succeeded or not.

Error message: (I hope this is allowed) Error message I already tried bitmap = null and System.gc() but both didn't help. 错误消息:(我希望这是允许的) 错误消息我已经尝试了位图= null和System.gc(),但是都没有帮助。

Ofcourse I can upload it in chuncks, but shouldn't Android be able to upload a 1.7MB image without any problems? 当然,我可以大块上传它,但是Android是否应该能够毫无问题地上传1.7MB图像?

Thanks already! 已经谢谢你了!

if you are finished with the bitmap, bitmap.recycle() is probably what you are looking for. 如果完成了位图, bitmap.recycle()可能就是您想要的。

check this for more info on managing bitmap memory. 检查此内容以获取有关管理位图内存的更多信息。

https://developer.android.com/training/displaying-bitmaps/manage-memory.html https://developer.android.com/training/displaying-bitmaps/manage-memory.html

Edit: I just noticed you are calling that (two logical statements on one line is quite hard to read). 编辑:我刚刚注意到您正在调用它(一行上的两个逻辑语句很难阅读)。

Second Edit: It seems the issue is that you are making several representations of the image other than the bitmap (a byte array, a string and a URLEncodedFormEntity), the System.gc() call is probably cleaning up the byte array. 第二次编辑:似乎问题在于,除了位图(字节数组,字符串和URLEncodedFormEntity)之外,您还对图像进行了几种表示,System.gc()调用可能正在清理字节数组。 A quick google provided an example that is using a buffer to send a file http://androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106 一个快速的Google提供了一个示例,该示例使用缓冲区发送文件http://androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM