gpt4 book ai didi

java - 捕获后在 ImageView 中显示图像(android java)

转载 作者:行者123 更新时间:2023-12-01 04:46:22 25 4
gpt4 key购买 nike

我的应用程序必须制作一张图片,并在 ImageView 中显示它。我让它工作了,但突然它不再工作了。

这是我的按钮代码:

public void makeFoto(View v) {
Calendar cal = Calendar.getInstance();
File file = new File(Environment.getExternalStorageDirectory(), (subject.getText().toString() + cal.getTimeInMillis()+".jpg"));
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else {
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
capturedImageUri = Uri.fromFile(file);
Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
photoIntent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
startActivityForResult(photoIntent, CAMERA_PHOTO_REQUEST);
}

这是在我的 onActivityResult 中:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
switch(requestCode) {
case CAMERA_PHOTO_REQUEST:
Uri img = capturedImageUri;
imv.setImageURI(img);
case CHOOSE_PHOTO_REQUEST:
try {
Uri selectedImage = data.getData();
InputStream imageStream = getContentResolver().openInputStream(selectedImage);
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
imv.setImageBitmap(yourSelectedImage);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

如您所见,我有两个结果,一个用于拍照,一个用于从图库中选择图片。这是我得到的错误:

    04-03 18:16:29.207: E/AndroidRuntime(1478): FATAL EXCEPTION: main
04-03 18:16:29.207: E/AndroidRuntime(1478): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.example.icomplain/com.example.icomplain.Home}: java.lang.NullPointerException
04-03 18:16:29.207: E/AndroidRuntime(1478): at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
04-03 18:16:29.207: E/AndroidRuntime(1478): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3019)
04-03 18:16:29.207: E/AndroidRuntime(1478): at android.app.ActivityThread.access$1100(ActivityThread.java:122)
04-03 18:16:29.207: E/AndroidRuntime(1478): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1176)
04-03 18:16:29.207: E/AndroidRuntime(1478): at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 18:16:29.207: E/AndroidRuntime(1478): at android.os.Looper.loop(Looper.java:137)
04-03 18:16:29.207: E/AndroidRuntime(1478): at android.app.ActivityThread.main(ActivityThread.java:4340)
04-03 18:16:29.207: E/AndroidRuntime(1478): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 18:16:29.207: E/AndroidRuntime(1478): at java.lang.reflect.Method.invoke(Method.java:511)
04-03 18:16:29.207: E/AndroidRuntime(1478): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-03 18:16:29.207: E/AndroidRuntime(1478): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-03 18:16:29.207: E/AndroidRuntime(1478): at dalvik.system.NativeStart.main(Native Method)
04-03 18:16:29.207: E/AndroidRuntime(1478): Caused by: java.lang.NullPointerException
04-03 18:16:29.207: E/AndroidRuntime(1478): at com.example.icomplain.Home.onActivityResult(Home.java:154)
04-03 18:16:29.207: E/AndroidRuntime(1478): at android.app.Activity.dispatchActivityResult(Activity.java:4649)
04-03 18:16:29.207: E/AndroidRuntime(1478): at android.app.ActivityThread.deliverResults(ActivityThread.java:2972)
04-03 18:16:29.207: E/AndroidRuntime(1478): ... 11 more
04-03 18:16:31.478: I/Process(1478): Sending signal. PID: 1478 SIG: 9
04-03 18:17:36.507: D/dalvikvm(1539): GC_FOR_ALLOC freed 47K, 2% free 14131K/14339K, paused 42ms
04-03 18:17:36.507: I/dalvikvm-heap(1539): Grow heap (frag case) to 14.868MB for 1048592-byte allocation
04-03 18:17:36.597: D/dalvikvm(1539): GC_CONCURRENT freed 1K, 2% free 15154K/15431K, paused 4ms+3ms
04-03 18:17:36.667: D/dalvikvm(1539): GC_FOR_ALLOC freed 0K, 2% free 15154K/15431K, paused 26ms
04-03 18:17:36.677: I/dalvikvm-heap(1539): Grow heap (frag case) to 16.646MB for 1865972-byte allocation
04-03 18:17:36.727: D/dalvikvm(1539): GC_CONCURRENT freed 0K, 2% free 16976K/17287K, paused 4ms+3ms

日志中的图片可以查看here .

最佳答案

  1. 我猜您需要在 case CHOOSE_PHOTO_REQUEST: 之前 break;
  2. 它是可选的,但不幸的是,确定输出文件名完全取决于设备上安装的相机应用或用户选择的相机应用,即使它是在 MediaStore.EXTRA_OUTPUT 上指定的。因此,除了 capturedImageUri 之外,您还应该看到 data.getParcelableExtra(Intent.EXTRA_STREAM)data.getData()

关于java - 捕获后在 ImageView 中显示图像(android java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15792870/

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