从Gallery中挑选照片后,Android应用程序消失了

时间:2016-07-15 00:33:35

标签: java android android-gallery onactivityresult

当用户点击按钮时,该应用会正常打开图库。但是,一旦用户选择照片,应用程序将在调试模式下消失(最小化)而不会出现任何错误或警告。我拍照时也是如此。 我在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"/>

片段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" />

1 个答案:

答案 0 :(得分:2)

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