简体   繁体   中英

Android app disappears after picking photo from Gallery

When the user clicks on a button, the app opens the Gallery normally. However, once the user picks a photo, the app disappears (minimizes) without any errors or warnings in debug mode. Same happens when I take a photo. The breakpoint I have at the beginning of onActivityResult is never reached . The openGallery() and openCamera() functions are called from the Fragment XML. What is wrong?

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();
            }
        }
    }

Permissions

<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"解决了此问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM