0

我开发了一个将图像从设备库上传到服务器的 Android 应用程序。它在其他设备上运行良好,除了 Galaxy S3。我的 Galaxy S2 工作正常,虽然 S2 和 S3 具有相同的操作系统 Jelly Bean。

在 S3 中上传图片应用程序崩溃。不知道怎么了?这是上传图片的代码:

if (selectedImagePath.length() != 0) {

            // get data from page

            try
            {

                BitmapFactory.Options opts=new BitmapFactory.Options();
                opts.inSampleSize = 4;  

                Bitmap bitmapOrg = BitmapFactory.decodeFile(selectedImagePath,opts);

                System.gc();

                ByteArrayOutputStream bao = new ByteArrayOutputStream();


                // Resize the image
                double width = bitmapOrg.getWidth();
                double height = bitmapOrg.getHeight();
                double ratio = 300 / width;
                int newheight = (int) (ratio * height);

                // System.out.println("———-width" + width);
                // System.out.println("———-height" + height);

                bitmapOrg = Bitmap.createScaledBitmap(bitmapOrg,400,
                        newheight, true);  // 400 chilo age

                // Here you can define .PNG as well
                bitmapOrg.compress(Bitmap.CompressFormat.JPEG,75, bao);

                byte[] ba = bao.toByteArray();
                String ba1 = ImageConverter.encodeBytes(ba);

                // System.out.println("uploading image now ——–” + ba1);

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("image", ba1));

                nameValuePairs.add(new BasicNameValuePair("event_id",
                        Integer.toString(taskEntity.getEventId())));
                nameValuePairs.add(new BasicNameValuePair("title", title));
                nameValuePairs.add(new BasicNameValuePair("description",
                        description));
                nameValuePairs.add(new BasicNameValuePair("urgent", Integer
                        .toString(urgentAttention)));
                nameValuePairs.add(new BasicNameValuePair("sendtime",
                        convertedtime));

                try {

                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(
                            AppSettings.SERVER_ADDRESS+"upload_image.php");
                    httppost.setEntity(new UrlEncodedFormEntity(
                            nameValuePairs));

                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();

                    // print responce
                    outPut = EntityUtils.toString(entity);
                    Log.i("GET RESPONSE—-", outPut);

                    // is = entity.getContent();
                    Log.e("log_tag ******", "good connection");

                    bitmapOrg.recycle();


                } catch (Exception e) {
                    Log.e("log_tag ******",
                            "Error in http connection " + e.toString());
                }

            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

        }
4

1 回答 1

1

您没有说明您收到什么错误,但由于您的问题是 Galaxy S3 特有的,我猜这是 OutOfMemoryError。S3 的应用程序堆相对于其屏幕尺寸而言往往较小,因此位图存储有点紧张。

尝试使用:

opts.inPurgeable = true;
opts.inInputShareable = true;

inPurgeable 将在不同的堆上分配位图,并允许其数据在内存压力下被丢弃,而无需等待垃圾收集器。inInputShareable 将允许从指定的输入重新解码位图,以防在您完成之前将其清除。尽可能多地使用这两个选项是一种很好的做法。请记住,使用 inInputShareable 无法更改或丢弃正在解码的数据,因为位图可能再次需要它。

于 2013-03-20T18:55:29.997 回答