ERROR/AndroidRuntime: java.lang.OutOfMemoryError
I am calling a web service to save an image in db. For that i picked an image from gallery and pass it to the web service. For me its working fine in Samsung galaxy tab.. But when i run it in HTC ( Android 2.2 Froyo ) an exception is thrown :
My code is :
protected void startGalleryPickActivity() {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
photoPickerInte开发者_JAVA百科nt.putExtra("crop", "true");
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
photoPickerIntent.putExtra("outputFormat",
Bitmap.CompressFormat.PNG.toString());
startActivityForResult(photoPickerIntent, REQ_CODE_PICK_IMAGE);
}
private Uri getTempUri() {
return Uri.fromFile(getTempFile());
}
private File getTempFile() {
if (isSDCARDMounted()) {
File f = new File(Environment.getExternalStorageDirectory(),
TEMP_PHOTO_FILE);
try {
f.createNewFile();
} catch (IOException e) {
}
return f;
} else {
return null;
}
}
private boolean isSDCARDMounted() {
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED))
return true;
return false;
}
-----------------------------------------------
yourSelectedImage = BitmapFactory.decodeFile(filePath);
uploadImage(yourSelectedImage);
public void uploadImage(final Bitmap yourSelectedImage) {
testWebService(yourSelectedImage);
}
public void testWebService(Bitmap bmp) {
MarshalBase64 marshal = new MarshalBase64();
ByteArrayOutputStream out = new ByteArrayOutputStream();
bmp.compress(CompressFormat.JPEG, 100, out);
byte[] raw = out.toByteArray();
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
OPERATION_NAME);
request.addProperty("image", raw);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
marshal.register(envelope);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
try
{
httpTransport.call(SOAP_ACTION, envelope);
Object response = envelope.getResponse();
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
Exception is :
03-24 09:52:07.364: ERROR/AndroidRuntime(920): java.lang.OutOfMemoryError
03-24 09:52:07.364: ERROR/AndroidRuntime(920): at java.lang.String.<init>(String.java:468)
03-24 09:52:07.364: ERROR/AndroidRuntime(920): at java.lang.AbstractStringBuilder.toString(AbstractStringBuilder.java:659)
03-24 09:52:07.364: ERROR/AndroidRuntime(920): at java.lang.StringBuffer.toString(StringBuffer.java:720)
03-24 09:52:07.364: ERROR/AndroidRuntime(920): at org.kobjects.base64.Base64.encode(Base64.java:33)
03-24 09:52:07.364: ERROR/AndroidRuntime(920): at org.ksoap2.serialization.MarshalBase64.writeInstance(MarshalBase64.java:39)
03-24 09:52:07.364: ERROR/AndroidRuntime(920): at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:653)
03-24 09:52:07.364: ERROR/AndroidRuntime(920): at org.ksoap2.serialization.SoapSerializationEnvelope.writeProperty(SoapSerializationEnvelope.java:645)
03-24 09:52:07.364: ERROR/AndroidRuntime(920): at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:614)
03-24 09:52:07.364: ERROR/AndroidRuntime(920): at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:598)
03-24 09:52:07.364: ERROR/AndroidRuntime(920): at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:655)
03-24 09:52:07.364: ERROR/AndroidRuntime(920): at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:579)
03-24 09:52:07.364: ERROR/AndroidRuntime(920): at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:192)
03-24 09:52:07.364: ERROR/AndroidRuntime(920): at org.ksoap2.transport.Transport.createRequestData(Transport.java:74)
03-24 09:52:07.364: ERROR/AndroidRuntime(920): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:58)
try to compress in PNG format instead of JPEG.
I added the below code
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
yourSelectedImage = BitmapFactory.decodeFile( filePath, options );
and in my webservice call method
MarshalBase64 marshal = new MarshalBase64();
ByteArrayOutputStream out = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 100, out);
byte[] raw = out.toByteArray();
精彩评论