安卓自定义相机应用程序的照片在我重启手机之前无法出现在相册中

7
我一直在开发一个相机应用,当我按下“拍摄”按钮时,希望它能够拍照并将图片保存到SD卡中,以便在图库中查看。
然而,当前的问题是无法按照我的要求保存。目前,当我按下拍摄按钮时,它会拍照,但只有在我完全重新启动手机后,图片才会显示在图库中。
这个问题困扰了我数周,我大多数情况下都是按照Android提供的教程操作。
以下是我处理图片的主类代码。
 private PictureCallback mPicture = new PictureCallback() 
   {

    @Override
    public void onPictureTaken(byte[] data, Camera camera)
      {
        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null){
           Log.d(TAG, "Error creating media file, check storage permissions: ");
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {
          //  Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
        //    Log.d(TAG, "Error accessing file: " + e.getMessage());
        }
    }
};


private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyCameraApp");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    } else if(type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "VID_"+ timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

public void onClick(View v) {

    mCamera.takePicture(null, null, mPicture);
    Context context = getApplicationContext();
    CharSequence text = "Click Detected";
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show();

  }

当我点击捕捉时,我的日志记录显示以下内容:

级别:E 标签:相机 文本:在处理CAMERA_MSG_RAW_IMAGE的handlemessage中

我的权限是

uses-permission android:name="android.permission.CAMERA"
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 

任何帮助都非常感激。谢谢。
3个回答

6
这个问题是图片保存到SD卡后不显示在Android图库应用程序中的重复(特别是ShadowGod提供的答案)。
要修复你的代码,在`fos.close()`之后添加以下内容到`onPictureTaken`函数中。
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
    Environment.DIRECTORY_PICTURES), "MyCameraApp");
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
    Uri.parse("file://"+ mediaStorageDir)));

无法在KITKAT上运行。如果有任何解决方法,请帮忙提供一个解决方案? - Muhammad Riyaz

3
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

似乎在KITKAT上无法正常工作。它会抛出权限拒绝异常并使应用程序崩溃。因此,我已经采取了以下措施:

String path = mediaStorageDir.getPath() + File.separator
                + "IMG_Some_name.jpg";
CameraActivity.this.sendBroadcast(new Intent(
                         Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri
                        .parse("file://" + path)));

希望能帮到你。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接