0

I am trying to open a huge image from the web, on the second item when i try to load the image... i get the below error.

@Override
protected void onPostExecute(String result) {
    progress.dismiss();

    try {
        JSONObject documentRoot = new JSONObject(result);
        String data = documentRoot.getString("Data");
        if (data.length() < 10) {
            Toast.makeText(Activity.this, "This is not a valid document", Toast.LENGTH_SHORT).show();
            finish();
        } else
            openPDF(data);
    } catch (Exception e) {
        Toast.makeText(Activity.this, "Document could not be opened", Toast.LENGTH_SHORT).show();
        finish();
    }
}

I am getting error in JSONObject documentRoot = new JSONObject(result); this line.

 06-14 19:54:22.218: E/XXX(16949): Uncaught exception is: 06-14
 19:54:22.218: E/XXX(16949): java.lang.OutOfMemoryError 06-14 19:54:22.218: E/XXX(16949):     at
 java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
 06-14 19:54:22.218: E/XXX(16949):     at
 java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:162)
 06-14 19:54:22.218: E/XXX(16949):     at
 java.lang.StringBuilder.append(StringBuilder.java:311) 06-14
 19:54:22.218: E/XXX(16949):     at
 org.json.JSONTokener.nextString(JSONTokener.java:224) 06-14
 19:54:22.218: E/XXX(16949):     at
 org.json.JSONTokener.nextValue(JSONTokener.java:107) 06-14
 19:54:22.218: E/XXX(16949):     at
 org.json.JSONTokener.readObject(JSONTokener.java:385) 06-14
 19:54:22.218: E/XXX(16949):     at
 org.json.JSONTokener.nextValue(JSONTokener.java:100) 06-14
 19:54:22.218: E/XXX(16949):     at
 org.json.JSONObject.<init>(JSONObject.java:154) 06-14 19:54:22.218:
 E/XXX(16949):     at org.json.JSONObject.<init>(JSONObject.java:171)
 06-14 19:54:22.218: E/XXX(16949):     at
 com.patient.Activity$DocumentsShowAsyncTask.onPostExecute(DocumentViewerActivity2.java:273)
 06-14 19:54:22.218: E/XXX(16949):     at
 com.patient.Activity$DocumentsShowAsyncTask.onPostExecute(DocumentViewerActivity2.java:1)
 06-14 19:54:22.218: E/XXX(16949):     at
 android.os.AsyncTask.finish(AsyncTask.java:631) 06-14 19:54:22.218:
 E/XXX(16949):     at
 android.os.AsyncTask.access$600(AsyncTask.java:177) 06-14
 19:54:22.218: E/XXX(16949):     at
 android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
 06-14 19:54:22.218: E/XXX(16949):     at
 android.os.Handler.dispatchMessage(Handler.java:99) 06-14
 19:54:22.218: E/XXX(16949):     at
 android.os.Looper.loop(Looper.java:137) 06-14 19:54:22.218:
 E/XXX(16949):     at
 android.app.ActivityThread.main(ActivityThread.java:4928) 06-14
 19:54:22.218: E/XXX(16949):     at
 java.lang.reflect.Method.invokeNative(Native Method) 06-14
 19:54:22.218: E/XXX(16949):     at
 java.lang.reflect.Method.invoke(Method.java:511) 06-14 19:54:22.218:
 E/XXX(16949):     at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
 06-14 19:54:22.218: E/XXX(16949):     at
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558) 06-14
 19:54:22.218: E/XXX(16949):     at
 dalvik.system.NativeStart.main(Native Method)
1
  • Have you base64 encoded the entire pdf into json? Commented Jun 14, 2013 at 16:05

1 Answer 1

1

It sounds like the data is just too large to hold in a String. How are you representing this PDF that it can be loaded from a String anyway? maybe it's a better idea to provide a URL in the JSON, and then you can use generic downloader code to get the PDF, without storing too much data in memory at a time.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there anyway without using an URL in the JSON?
You could do a standard HTTP Download, reading from an InputStream, chunk-by-chunk and converting to PDF or image in-line and probably write to a file. This would probably require you parse the JSON manually, however. Once the PDF is downloaded/converted, you can open it from the filesystem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.