我开发了一款Android应用,可将图片从设备库上传到服务器。它工作正常其他设备期望Galaxy S3。我的Galaxy S2工作正常,虽然S2和S3有相同的os 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();
}
}
答案 0 :(得分:1)
您没有指出您收到的错误,但由于您的问题是针对Galaxy S3的,因此我猜测它是OutOfMemoryError。 S3相对于其屏幕尺寸往往具有较小的app堆,因此位图存储有点紧张。
尝试使用:
opts.inPurgeable = true;
opts.inInputShareable = true;
inPurgeable将在不同的堆上分配位图,并允许在内存压力下丢弃其数据,而无需等待垃圾收集器。 inInputShareable将允许从指定的输入重新解码位图,以防它在完成之前被清除。尽可能经常使用这两个选项是一种很好的做法。请记住,使用inInputShareable,无法更改或丢弃正在解码的数据,因为Bitmap可能需要再次使用它。