Search code examples
androidandroid-gallery

Exception while Loading Image from gallery


I am developing a Finger painting application. I have following code for loading an image from gallery (to paint over it). The code is working fine in my smartphone (android 4.2.2 - API 17). but when i run it on lower versions (2.3.3 etc), It just shows a run time error and app is closed.

   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);


    if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
        try {
            // We need to recyle unused bitmaps
            if (bitmap != null) {
                bitmap.recycle();
            }
            InputStream stream = getContentResolver().openInputStream(
                    data.getData());
            myBitmap = BitmapFactory.decodeStream(stream);
            stream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

and for calling this

Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(intent, REQUEST_CODE);

In my understanding, this code should work for all the APIs. But there is some problem with lower versions. please help me out

EDIT:

(I have deleted DISPLAY code, that was causing problem. still the problem persists. here is new logcat. LOGCAT after clicking LOAD button

08-25 18:55:51.079: D/ActionBarSherlock(524): [callbackOptionsItemSelected] item: Load
08-25 18:55:51.089: D/ActionBarSherlock(524): [callbackOptionsItemSelected] returning true
08-25 18:55:51.141: D/PhoneWindow(524): couldn't save which view has focus because the focused view com.example.colordraw.CanvasActivity$Canvass@4050a590 has no id.
08-25 18:55:51.159: D/ActionBarSherlock(524): [dispatchPause]
08-25 18:55:51.899: D/ActionBarSherlock(524): [dispatchStop]
08-25 18:55:53.259: D/ActionBarSherlock(524): [dispatchPostResume]
08-25 18:55:53.379: D/AndroidRuntime(524): Shutting down VM
08-25 18:55:53.379: W/dalvikvm(524): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-25 18:55:53.400: E/AndroidRuntime(524): FATAL EXCEPTION: main
08-25 18:55:53.400: E/AndroidRuntime(524): java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
08-25 18:55:53.400: E/AndroidRuntime(524):  at android.graphics.Canvas.<init>(Canvas.java:83)
08-25 18:55:53.400: E/AndroidRuntime(524):  at com.example.colordraw.CanvasActivity$Canvass.onDraw(CanvasActivity.java:780)
08-25 18:55:53.400: E/AndroidRuntime(524):  at android.view.View.draw(View.java:6880)
08-25 18:55:53.400: E/AndroidRuntime(524):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
08-25 18:55:53.400: E/AndroidRuntime(524):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-25 18:55:53.400: E/AndroidRuntime(524):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-25 18:55:53.400: E/AndroidRuntime(524):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-25 18:55:53.400: E/AndroidRuntime(524):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-25 18:55:53.400: E/AndroidRuntime(524):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-25 18:55:53.400: E/AndroidRuntime(524):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-25 18:55:53.400: E/AndroidRuntime(524):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-25 18:55:53.400: E/AndroidRuntime(524):  at android.view.View.draw(View.java:6883)
08-25 18:55:53.400: E/AndroidRuntime(524):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
08-25 18:55:53.400: E/AndroidRuntime(524):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
08-25 18:55:53.400: E/AndroidRuntime(524):  at android.view.ViewRoot.draw(ViewRoot.java:1522)
08-25 18:55:53.400: E/AndroidRuntime(524):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
08-25 18:55:53.400: E/AndroidRuntime(524):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
08-25 18:55:53.400: E/AndroidRuntime(524):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 18:55:53.400: E/AndroidRuntime(524):  at android.os.Looper.loop(Looper.java:123)
08-25 18:55:53.400: E/AndroidRuntime(524):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-25 18:55:53.400: E/AndroidRuntime(524):  at java.lang.reflect.Method.invokeNative(Native Method)
08-25 18:55:53.400: E/AndroidRuntime(524):  at java.lang.reflect.Method.invoke(Method.java:507)
08-25 18:55:53.400: E/AndroidRuntime(524):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-25 18:55:53.400: E/AndroidRuntime(524):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-25 18:55:53.400: E/AndroidRuntime(524):  at dalvik.system.NativeStart.main(Native Method)
08-25 18:55:56.699: I/Process(524): Sending signal. PID: 524 SIG: 9

Solution

  • the logs says this:

    java.lang.NoSuchMethodError: android.view.Display.getSize

    so, this is the problematic line:

    display.getSize(size);
    

    it's supported only from API 13.

    instead , you can use getResources().getDisplayMetrics().

    in order to avoid having such bugs, you should use Lint (the button that looks like a checkbox with a "V" in it).