我的第一个问题在这里!我有一段代码有问题,该代码使用额外的输出选项启动相机意图,然后在活动结果上尝试调整该图像的大小。正在发生的事情是在回调的 resize 函数中抛出了一个空指针异常。
原始的大图片仍然保存在文件系统中,因为我可以从文件系统中访问它。
原始相机 jpeg 大小为 2560x1920,使用的手机是 google nexus 手机。
我不清楚为什么调整大小不起作用,有人有任何见解吗?
这是一些代码:
也创建虚拟文件的 takePicture 函数:
public boolean takePicture( ) {
Log.e(TAG, "takePicture interface function");
String FileUri = Environment.getExternalStorageDirectory() + "/samples/";
File file = new File(FileUri,"picture"+ pictureNumber +".jpg");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Uri outputFileUri = Uri.fromFile(file);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
Log.e(TAG, "intent started");
return true;
}
onactivity结果回调函数:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// handle the camera request returns and handle back button in camera.
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_CANCELED ) {
Toast toast = Toast.makeText(this,"Canceled, no picture taken.", 1000);
toast.show();
return;
}
else if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK ) {
Log.e(TAG, "Camera intent return");
Bitmap scaledphoto = null;
int height = 300;
int width = 300;
Bitmap photo = BitmapFactory.decodeFile( APP_DATA_PATH + "/samples/picture" + pictureNumber + ".jpg" );
Log.e(TAG, "Picture fetched");
scaledphoto = Bitmap.createScaledBitmap(photo, height, width, true);
Log.e(TAG, "Picture scaled");
saveImageToFile(scaledphoto, "picture" + pictureNumber + ".jpg");
Log.e(TAG, "Scaled picture saved");
myWebView.loadUrl("javascript:pictureTaken(\""+ pictureLoc + "\")");
pictureNumber++;
这是 LogCat:
06-21 14:59:13.496: E/AndroidRuntime(6130): FATAL EXCEPTION: main
06-21 14:59:13.496: E/AndroidRuntime(6130): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=null} to activity {test.test/test.test.CameraIntentTestActivity}: java.lang.NullPointerException
06-21 14:59:13.496: E/AndroidRuntime(6130): at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
06-21 14:59:13.496: E/AndroidRuntime(6130): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
06-21 14:59:13.496: E/AndroidRuntime(6130): at android.app.ActivityThread.access$1100(ActivityThread.java:123)
06-21 14:59:13.496: E/AndroidRuntime(6130): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
06-21 14:59:13.496: E/AndroidRuntime(6130): at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 14:59:13.496: E/AndroidRuntime(6130): at android.os.Looper.loop(Looper.java:137)
06-21 14:59:13.496: E/AndroidRuntime(6130): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-21 14:59:13.496: E/AndroidRuntime(6130): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 14:59:13.496: E/AndroidRuntime(6130): at java.lang.reflect.Method.invoke(Method.java:511)
06-21 14:59:13.496: E/AndroidRuntime(6130): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-21 14:59:13.496: E/AndroidRuntime(6130): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-21 14:59:13.496: E/AndroidRuntime(6130): at dalvik.system.NativeStart.main(Native Method)
06-21 14:59:13.496: E/AndroidRuntime(6130): Caused by: java.lang.NullPointerException
06-21 14:59:13.496: E/AndroidRuntime(6130): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:432)