gpt4 book ai didi

java - 相机 Intent 回调后Android图像调整大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:07:00 25 4
gpt4 key购买 nike

我的第一个问题在这里!我有一段代码有问题,该代码使用额外的输出选项启动相机 Intent ,然后在 Activity 结果中尝试调整该图像的大小。发生的事情是在回调的调整大小函数中抛出空指针异常。

原始大图仍然保存在文件系统中,因为我可以从文件系统访问它。

原始相机 jpeg 大小为 2560x1920,使用的手机是 google nexus one。

我不清楚为什么调整大小不起作用,有人有任何见解吗?

这是一些代码:

同样创建虚拟文件的 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;
}

on activity结果回调函数:

    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)

最佳答案

改变

Bitmap photo = BitmapFactory.decodeFile( APP_DATA_PATH + "/samples/picture" + pictureNumber + ".jpg" );

  Bitmap photo = (Bitmap) data.getExtras().get("data");

关于java - 相机 Intent 回调后Android图像调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11137832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com