![](/img/trans.png)
[英]Get onActivityResult when returning from startActivityForResult in Adapter
[英]startActivityForResult returning without calling onActivityResult
我正在嘗試捕獲照片並將其保存在SD卡中。 圖像uri生成,當我調用startActivityForResult時,它返回而未調用onActivityResult。
private Uri dispatchTakePictureIntent() {
Uri uri = null;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
Log.d("dispatchTakePictureIntent", "inside try");
} catch (IOException ex) {
// Error occurred while creating the File
Log.d("dispatchTakePictureIntent", "inside catch");
}
// Continue only if the File was successfully created
if (photoFile != null) {
Log.d("dispatchTakePictureIntent", "inside secondif");
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
Log.d("dispatchTakePictureIntent","after putExtra");
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
uri = Uri.fromFile(photoFile);
Log.d("dispatchTakePictureIntent", uri.toString());
return uri;
}
}
return uri;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("onActivityResult", "outside if");
ImageView recipeImageView = (ImageView)this.findViewById(R.id.dishImage);
Log.d("onActivityResult", "outside if");
Log.d("requestcode", requestCode+"");
Log.d("REQUEST_IMAGE_CAPTURE", REQUEST_IMAGE_CAPTURE+"");
Log.d("resultcode", resultCode+"");
Log.d("REESULT_OK", RESULT_OK+"");
Log.d("onActivityResult", "inside if");
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Log.d("onActivityResult", "inside if");
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
recipeImageView.setImageBitmap(imageBitmap);
}
Log.d("onActivityResult", "completed if");
}
String mCurrentPhotoPath;
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
Log.d("createImageFile", Environment.DIRECTORY_PICTURES);
Log.d("createImageFile", storageDir.toString());
Log.d("createImageFile", imageFileName);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
this.getCacheDir() /* directory */
//storageDir
);
Log.d("createImageFile", image.toString());
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
Log.d("createImageFile", mCurrentPhotoPath);
return image;
}
但是,當我僅調用縮略圖而不將其保存在SD卡中時,將調用onActivityResult並顯示縮略圖
private void dispatchTakePictureIntentThumb() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
當我檢查日志消息時。
12-23 23:16:15.589:D / createImageFile(2189):/ data / data / com.cookstory / cache / JPEG_20131223_231615_1148844370.jpg
12-23 23:16:15.599:D / createImageFile(2189):文件:/data/data/com.cookstory/cache/JPEG_20131223_231615_1148844370.jpg
12-23 23:16:15.599:D / dispatchTakePictureIntent(2189):內部嘗試
12-23 23:16:15.599:D / dispatchTakePictureIntent(2189):在secondif內部
12-23 23:16:15.599:D / dispatchTakePictureIntent(2189):在putExtra之后
12-23 23:16:15.609:E / SoundPool(374):錯誤加載/system/media/audio/ui/KeypressInvalid.ogg
12-23 23:16:15.619:W / AudioService(374):聲音池無法加載文件:/system/media/audio/ui/KeypressInvalid.ogg
12-23 23:16:15.619:W / AudioService(374):onLoadSoundEffects(),加載樣本時出現錯誤-1
12-23 23:16:15.629:I / ActivityManager(374):從pid 2189開始u0 {act = android.media.action.IMAGE_CAPTURE cmp = com.android.camera / .Camera(有其他功能)}
12-23 23:16:15.689:D / gralloc(51):在創建緩沖區的進程中注冊一個緩沖區。 這可能會導致內存排序問題。
12-23 23:16:15.689:E / libEGL(51):稱為未實現的OpenGL ES API
12-23 23:16:15.689:E / libEGL(51):稱為未實現的OpenGL ES API
12-23 23:16:15.689:E / libEGL(51):稱為未實現的OpenGL ES API
12-23 23:16:15.689:E / libEGL(51):稱為未實現的OpenGL ES API
12-23 23:16:15.689:E / SurfaceFlinger(51):glCheckFramebufferStatusOES錯誤1613735025
12-23 23:16:15.689:E / SurfaceFlinger(51):截屏時出現GL_FRAMEBUFFER_COMPLETE_OES錯誤
12-23 23:16:15.689:E / libEGL(51):稱為未實現的OpenGL ES API
12-23 23:16:15.689:E / libEGL(51):稱為未實現的OpenGL ES API
12-23 23:16:15.689:W / WindowManager(374):屏幕截圖失敗,將(328x546)的屏幕截圖捕獲到第21040層
12-23 23:16:15.909:D / dispatchTakePictureIntent(2189):file:///data/data/com.cookstory/cache/JPEG_20131223_231615_1148844370.jpg
在瀏覽其他帖子時,我確保以下內容都可以。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
從功能上講,它可以打開相機並拍照,但是當我們嘗試保存時,它仍處於相同狀態。
我遇到了這個問題。 您是否在活動的屬性中使用android:launchMode =“ singleInstance”?
這是將圖片保存在外部目錄中的功能,
public static String saveImageInExternalCacheDir(Context context, Bitmap bitmap, String myfileName) {
String fileName = myfileName.replace(' ', '_') + getCurrentDate().toString().replace(' ', '_').replace(":", "_");
String filePath = (context.getExternalCacheDir()).toString() + "/" + fileName + ".jpg";
try {
FileOutputStream fos = new FileOutputStream(new File(filePath));
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
return filePath;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.