0

我正在学习如何在android中旋转位图,但是在获取位图的宽度和高度时遇到问题..如何解决?

这是我的代码;

// Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            bitmap = BitmapFactory.decodeFile(filePath, o2);

            int width = bitmap.getWidth();
            int height = bitmap.getHeight();

            if(degree!=0){



        Matrix matrix = new Matrix();
            matrix.postRotate(degree);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

        }

我得到错误int width = bitmap.getWidth(); int height = bitmap.getHeight();

这是我的日志猫

 10-25 11:02:51.689: W/dalvikvm(22013): threadid=1: thread exiting with uncaught exception (group=0x40bd7438)
10-25 11:02:51.699: E/AndroidRuntime(22013): FATAL EXCEPTION: main
10-25 11:02:51.699: E/AndroidRuntime(22013): java.lang.NullPointerException
10-25 11:02:51.699: E/AndroidRuntime(22013):    at com.mozaik.tangsel.uploadimage.UploadActivity.decodeFile(UploadActivity.java:580)
10-25 11:02:51.699: E/AndroidRuntime(22013):    at com.mozaik.tangsel.uploadimage.UploadActivity$5.onClick(UploadActivity.java:216)
10-25 11:02:51.699: E/AndroidRuntime(22013):    at android.view.View.performClick(View.java:4084)
10-25 11:02:51.699: E/AndroidRuntime(22013):    at android.view.View$PerformClick.run(View.java:16987)
10-25 11:02:51.699: E/AndroidRuntime(22013):    at android.os.Handler.handleCallback(Handler.java:615)
10-25 11:02:51.699: E/AndroidRuntime(22013):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 11:02:51.699: E/AndroidRuntime(22013):    at android.os.Looper.loop(Looper.java:137)
10-25 11:02:51.699: E/AndroidRuntime(22013):    at android.app.ActivityThread.main(ActivityThread.java:4794)
10-25 11:02:51.699: E/AndroidRuntime(22013):    at java.lang.reflect.Method.invokeNative(Native Method)
10-25 11:02:51.699: E/AndroidRuntime(22013):    at java.lang.reflect.Method.invoke(Method.java:511)
10-25 11:02:51.699: E/AndroidRuntime(22013):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
10-25 11:02:51.699: E/AndroidRuntime(22013):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
10-25 11:02:51.699: E/AndroidRuntime(22013):    at dalvik.system.NativeStart.main(Native Method)

谢谢,对不起我的英语

4

2 回答 2

1

我认为你必须在设置 insamplesize 之前设置你的选项 inJustDecodeBounds

BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(selectedImagePath, options);
                int imageHeight = options.outHeight;
                int imageWidth = options.outWidth;
                String imageType = options.outMimeType;
                if(imageWidth > imageHeight){
                    options.inSampleSize = calculateInSampleSize(options,512,256);//<------the size you need if landscape
                }else{
                    options.inSampleSize = calculateInSampleSize(options,256,512);//<------the size you need if portrait
                }
                options.inJustDecodeBounds = false;
                bitmap = BitmapFactory.decodeFile(selectedImagePath,options);
于 2013-10-25T04:01:54.060 回答
1

问题是您filePath没有指向正确的文件。也就是说,在 处没有图像filePath。检查 的值filePath是否正确,这应该得到解决。

于 2013-10-25T04:09:49.393 回答