gpt4 book ai didi

java - 从相册中选取照片后 Android 应用程序消失

转载 作者:行者123 更新时间:2023-11-29 19:42:35 26 4
gpt4 key购买 nike

当用户点击按钮时,应用会正常打开图库。然而,一旦用户选择了一张照片,应用程序就会消失(最小化),在 Debug模式下不会出现任何错误或警告。当我拍照时也会发生同样的情况。 从未达到我在 onActivityResult 开头的断点。 openGallery() 和 openCamera() 函数从 Fragment XML 调用。怎么了?

public static final int GALLERY = 0;
public static final int CAMERA = 1;

public void openGallery(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY);
}

public void openCamera(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == GALLERY)
onSelectFromGalleryResult(data);
else if (requestCode == CAMERA)
onCaptureImageResult(data);
}
}

private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private void onSelectFromGalleryResult(Intent data) {
Bitmap bm=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
}

权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

fragment XML

<ImageView
android:id="@+id/imageView1"
android:layout_width="30dp"
android:layout_height="30dp"
android:onClick="openCamera"
android:clickable="true"
android:src="@drawable/camera" />

<ImageView
android:id="@+id/imageView2"
android:layout_marginLeft="10dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:onClick="openGallery"
android:clickable="true"
android:src="@drawable/gallery" />

最佳答案

我通过将 AndroidManifest.xml 中的 android:noHistory="true" 更改为 "false" 解决了这个问题。

关于java - 从相册中选取照片后 Android 应用程序消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38386180/

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