gpt4 book ai didi

java - android:将捕获的照片0kb保存在文件夹中

转载 作者:行者123 更新时间:2023-12-01 18:43:16 25 4
gpt4 key购买 nike

拍照后,我的照片显示在 Activity 中,但是当我检查文件夹时,我的照片大小为 0kb,我不知道为什么会发生这种情况,当我再次拍照时,新照片没有保存在我想要的文件夹中,这是我的代码:

 {


File sdCard= Environment.getExternalStorageDirectory();
File path = new File (sdCard.getAbsolutePath() + "/android/data/spaj_foto");
path.mkdir();
File file= new File (path,"spaj_foto.png");
outputFileUri = Uri.fromFile(file);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri );
startActivityForResult(cameraIntent,TAKE_PHOTO_CODE );

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK) {
if (requestCode == TAKE_PHOTO_CODE) {
Bundle extras = data.getExtras();

Bitmap photo = (Bitmap) extras.get("data");
image_spaj.setImageBitmap(photo);
}
else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
}
}


}

编辑

这是我的日志猫:

    09-26 17:57:38.072: E/AndroidRuntime(12498): FATAL EXCEPTION: main
09-26 17:57:38.072: E/AndroidRuntime(12498): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {org.example.touch/org.example.touch.FormSpaj}: java.lang.NullPointerException
09-26 17:57:38.072: E/AndroidRuntime(12498): at android.app.ActivityThread.deliverResults(ActivityThread.java:3179)
09-26 17:57:38.072: E/AndroidRuntime(12498): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3222)
09-26 17:57:38.072: E/AndroidRuntime(12498): at android.app.ActivityThread.access$1100(ActivityThread.java:140)
09-26 17:57:38.072: E/AndroidRuntime(12498): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1276)
09-26 17:57:38.072: E/AndroidRuntime(12498): at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 17:57:38.072: E/AndroidRuntime(12498): at android.os.Looper.loop(Looper.java:137)
09-26 17:57:38.072: E/AndroidRuntime(12498): at android.app.ActivityThread.main(ActivityThread.java:4895)
09-26 17:57:38.072: E/AndroidRuntime(12498): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 17:57:38.072: E/AndroidRuntime(12498): at java.lang.reflect.Method.invoke(Method.java:511)
09-26 17:57:38.072: E/AndroidRuntime(12498): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
09-26 17:57:38.072: E/AndroidRuntime(12498): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
09-26 17:57:38.072: E/AndroidRuntime(12498): at dalvik.system.NativeStart.main(Native Method)
09-26 17:57:38.072: E/AndroidRuntime(12498): Caused by: java.lang.NullPointerException
09-26 17:57:38.072: E/AndroidRuntime(12498): at org.example.touch.FormSpaj.onActivityResult(FormSpaj.java:990)
09-26 17:57:38.072: E/AndroidRuntime(12498): at android.app.Activity.dispatchActivityResult(Activity.java:5347)
09-26 17:57:38.072: E/AndroidRuntime(12498): at android.app.ActivityThread.deliverResults(ActivityThread.java:3175)
09-26 17:57:38.072: E/AndroidRuntime(12498): ... 11 more

最佳答案

您已经创建了一个新文件。 (空白)

然后开始相机 Intent 。

但是您还没有做的是告诉相机 Intent 将图像保存在哪里。

因此,您将得到一个空白文件。

  File file= new File (path,"spaj_foto.png");


outputFileUri = Uri.fromFile(file);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri );
startActivityForResult(cameraIntent,TAKE_PHOTO_CODE );

请注意,您不需要创建该文件,android 相机 Intent 将为您创建该文件。您需要做的就是传递额外的内容,以便 Intent 知道保存在哪里。

(您仍然需要创建目录)

关于java - android:将捕获的照片0kb保存在文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19025913/

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