简体   繁体   English

Android Camera App(图片仅在重启后显示)

[英]Android Camera App (picture only shows up after reboot)

I'm having trouble with my camera app. 我的相机应用出现问题。 I'm trying to make it snap a picture and then save it to a folder of the android device and it seems to be doing that, but the pictures only show up after I reboot the device. 我正在尝试使其抓拍图片,然后将其保存到android设备的文件夹中,似乎正在这样做,但是这些图片仅在我重启设备后才会显示。 Furthermore, I seem to always end up with the result code of 0, so I never get to update my imageView. 此外,我似乎总是以结果代码0结束,因此我永远也无法更新imageView。 Why do I always get that result code? 为什么我总是得到那个结果代码?

/** 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);
}

To see the picture taken refresh the gallery using 要查看拍摄的照片,请使用刷新画廊

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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