1
當用戶單擊按鈕時,應用程序將正常打開圖庫。但是,一旦用戶選擇了一張照片,該應用就會在調試模式下消失(最小化),而不會出現任何錯誤或警告。我拍照時也是如此。 我在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" />
請檢查您是否在嘗試對onSelectFromGalleryResult()進行激烈的操作。特別是如果您試圖顯示捕獲的圖像考慮調整位圖的大小。查看此[鏈接](https://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap)以獲取更多信息。 –
如果沒有您的onSelectFromGalleryResult()方法,則無法分辨。請張貼它。 –
使用相機拍攝圖像時它能正確工作嗎? –