Android相机应用程序(图片仅在重启后显示)

Posted

技术标签:

【中文标题】Android相机应用程序(图片仅在重启后显示)【英文标题】:Android Camera App (picture only shows up after reboot) 【发布时间】:2014-07-24 20:16:14 【问题描述】:

我的相机应用出现问题。我试图让它拍一张照片,然后将它保存到 android 设备的文件夹中,它似乎正在这样做,但这些图片只有在我重新启动设备后才会显示。此外,我似乎总是以 0 的结果代码结束,所以我永远无法更新我的 imageView。为什么我总是得到那个结果代码?

/** Create a file Uri for saving an image */
private static Uri getOutputMediaFileUri()
      return Uri.fromFile(getOutputMediaFile());


/** Create a File for saving an image */
private static File getOutputMediaFile()

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "myAppPics");
    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists())
        if (! mediaStorageDir.mkdirs())
            Log.d("myAppPics", "failed to create directory");
            return null;
        
    
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    return mediaFile;



/** Opening App*/
public void open()
    intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE_SECURE);
    fileUri = getOutputMediaFileUri(); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
    startActivityForResult(intent, 0);  
 


@Override
/**when you get the activity result*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
   // TODO Auto-generated method stub
   super.onActivityResult(requestCode, resultCode, data);
   //if you want to keep the picture
   if (resultCode == RESULT_OK )
   
       //grab image data
       Bundle extras = data.getExtras();
       Bitmap bp = (Bitmap) extras.get("data");
       //make imageView hold that image now
       myImgV.setImageBitmap(bp);
       Log.d("MyCameraApp", "update image");
   
   //else just goes back to previously chosen image
   else
       Log.d("MyCameraApp", "don't want to update image "+resultCode);

【问题讨论】:

【参考方案1】:

要查看拍摄的照片,请使用刷新图库

 refreshGallery(String fileUrl, Context ctx) 
        Intent mediaScanIntent = new Intent(
                Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(fileUrl);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        ctx.sendBroadcast(mediaScanIntent);
    

【讨论】:

以上是关于Android相机应用程序(图片仅在重启后显示)的主要内容,如果未能解决你的问题,请参考以下文章

仅在单击按钮后请求相机权限

用相机拍照后应用程序重启(Titanium android)

Parse.com 推送通知延迟或仅在 Android 应用重启后

Android相机:拍照后获取图片Uri

Xamarin.Android Camera Permissions with ZXing.Net.Mobile 仅在应用重启后有效

Android 调用系统相机拍照后 添加文字水印