从库中加载图像时出现异常

时间:2013-08-24 16:23:30

标签: android android-gallery

我正在开发一个手指画应用程序。我有以下代码从库中加载图像(以绘制它)。代码在我的智能手机中运行良好(android 4.2.2 - API 17)。但是当我在较低版本(2.3.3等)上运行它时,它只显示运行时错误并且应用程序已关闭。

   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();
        }

并且用于调用此

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

据我了解,此代码适用于所有API。但是较低版本存在一些问题。请帮帮我

编辑:

(我删除了DISPLAY代码,导致问题。仍然存在问题。这里是新的logcat。 单击LOAD按钮后的LOGCAT

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

2 个答案:

答案 0 :(得分:1)

日志说明了这一点:

  

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

所以,这是有问题的一行:

display.getSize(size);

仅支持API 13

相反,您可以使用getResources().getDisplayMetrics()

为了避免出现这样的错误,你应该使用Lint(看起来像一个带有“V”的复选框的按钮)。

答案 1 :(得分:0)

自API级别13以来,Display.getSize(Point)方法可用。