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

时间:2014-07-24 20:16:14

标签: android camera media

我的相机应用程序出现问题。我试图让它拍摄一张照片,然后将其保存到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 个答案:

答案 0 :(得分: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);
    }