我正在尝试从 imageView 获取缩放位图,因为我需要一个高度相等宽度的图像,以便稍后将它们划分为相等的较小位图。我正在使用以下代码:
public void breakImage(View v)
{
ImageView imageView = (ImageView) findViewById(R.id.imgView);
Drawable d = imageView.getBackground();
BitmapDrawable bitmapDrawable = ((BitmapDrawable) d);
Bitmap image = bitmapDrawable .getBitmap();
Bitmap scaledImage = Bitmap.createScaledBitmap(image, 240, 240, true);
int width = image.getWidth();
int height = image.getHeight();
}
但是程序在到达语句时卡住了
BitmapDrawable bitmapDrawable = ((BitmapDrawable) d);
有谁知道这段代码有什么问题?
提前致谢
这是LogCat:
01-03 04:47:57.078: D/dalvikvm(10411): GC_EXTERNAL_ALLOC freed 52K, 52% free 2634K/5379K, external 1605K/2108K, paused 22ms
01-03 04:47:58.507: D/AndroidRuntime(10411): Shutting down VM
01-03 04:47:58.507: W/dalvikvm(10411): threadid=1: thread exiting with uncaught exception (group=0x40015578)
01-03 04:47:58.531: E/AndroidRuntime(10411): FATAL EXCEPTION: main
01-03 04:47:58.531: E/AndroidRuntime(10411): java.lang.IllegalStateException: Could not execute method of the activity
01-03 04:47:58.531: E/AndroidRuntime(10411): at android.view.View$1.onClick(View.java:2154)
01-03 04:47:58.531: E/AndroidRuntime(10411): at android.view.View.performClick(View.java:2538)
01-03 04:47:58.531: E/AndroidRuntime(10411): at android.view.View$PerformClick.run(View.java:9152)
01-03 04:47:58.531: E/AndroidRuntime(10411): at android.os.Handler.handleCallback(Handler.java:587)
01-03 04:47:58.531: E/AndroidRuntime(10411): at android.os.Handler.dispatchMessage(Handler.java:92)
01-03 04:47:58.531: E/AndroidRuntime(10411): at android.os.Looper.loop(Looper.java:123)
01-03 04:47:58.531: E/AndroidRuntime(10411): at android.app.ActivityThread.main(ActivityThread.java:3687)
01-03 04:47:58.531: E/AndroidRuntime(10411): at java.lang.reflect.Method.invokeNative(Native Method)
01-03 04:47:58.531: E/AndroidRuntime(10411): at java.lang.reflect.Method.invoke(Method.java:507)
01-03 04:47:58.531: E/AndroidRuntime(10411): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-03 04:47:58.531: E/AndroidRuntime(10411): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-03 04:47:58.531: E/AndroidRuntime(10411): at dalvik.system.NativeStart.main(Native Method)
01-03 04:47:58.531: E/AndroidRuntime(10411): Caused by: java.lang.reflect.InvocationTargetException
01-03 04:47:58.531: E/AndroidRuntime(10411): at java.lang.reflect.Method.invokeNative(Native Method)
01-03 04:47:58.531: E/AndroidRuntime(10411): at java.lang.reflect.Method.invoke(Method.java:507)
01-03 04:47:58.531: E/AndroidRuntime(10411): at android.view.View$1.onClick(View.java:2149)
01-03 04:47:58.531: E/AndroidRuntime(10411): ... 11 more
01-03 04:47:58.531: E/AndroidRuntime(10411): Caused by: java.lang.NullPointerException
01-03 04:47:58.531: E/AndroidRuntime(10411): at com.example.imagegallerydemo.MainActivity.breakImage(MainActivity.java:105)
01-03 04:47:58.531: E/AndroidRuntime(10411): ... 14 more