1

我正在调用 Web 服务以将图像保存在数据库中。为此,我从图库中挑选了一张图片并将其传递给 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)
4

2 回答 2

2

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

于 2011-03-24T05:09:14.903 回答
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();
于 2011-03-24T05:47:38.293 回答