我正在尝试从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
答案 0 :(得分:1)
以下是您的日志中出现错误的原因:
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)
这是因为d
为空,d
为空,因为getBackground()
为空。
您确定要getBackground()
吗?这将返回ImageView
的背景,而不是使用android:src
或setImageBitmap
,setImageDrawable
,setImageResource
等设置的图像。要获得可绘制的图像,请使用{ {1}}方法。