我开发了一个将图像从设备库上传到服务器的 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();
}
}