ERROR / AndroidRuntime:java.lang.OutOfMemoryError

时间:2011-03-24 04:33:23

标签: android web-services ksoap2

我正在调用一个Web服务来保存db中的图像。为此,我从图库中选择了一个图像并将其传递给Web服务。对我而言,它在三星Galaxy标签中运行良好..但是当我在HTC(Android 2.2 Froyo)中运行它时会抛出异常:

我的代码是:

protected void startGalleryPickActivity() {

    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    photoPickerIntent.setType("image/*");
    photoPickerIntent.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();

    }

    }

例外情况是:

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)

2 个答案:

答案 0 :(得分:2)

尝试以PNG格式而不是JPEG格式进行压缩。

答案 1 :(得分:1)

我添加了以下代码

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

        yourSelectedImage = BitmapFactory.decodeFile( filePath, options );

和我的网络服务电话方法

MarshalBase64 marshal = new MarshalBase64();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    bmp.compress(CompressFormat.PNG, 100, out);
    byte[] raw = out.toByteArray();