从应用程序中选取照片后,Android应用程序消失
问题描述:
当用户单击按钮时,应用程序将正常打开图库。但是,一旦用户选择了一张照片,该应用就会在调试模式下消失(最小化),而不会出现任何错误或警告。我拍照时也是如此。 我在onActivityResult开始处的断点永远不会达到。 openGallery()和openCamera()函数是从Fragment XML中调用的。哪里不对?从应用程序中选取照片后,Android应用程序消失
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"/>
片段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"
。
请检查您是否在尝试对onSelectFromGalleryResult()进行激烈的操作。特别是如果您试图显示捕获的图像考虑调整位图的大小。查看此[链接](https://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap)以获取更多信息。 –
如果没有您的onSelectFromGalleryResult()方法,则无法分辨。请张贴它。 –
使用相机拍摄图像时它能正确工作吗? –