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 的句柄消息中

我的权限是

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

任何帮助深表感谢。谢谢

4

3 回答 3

6

这个问题是Image的副本,保存到sdcard,没有出现在Android的Gallery应用程序中(尤其是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)));
于 2012-12-10T14:47:25.287 回答
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)));

希望能帮助到你。

于 2014-03-14T12:49:49.083 回答
0

MediaScannerConnection 用于使用 KitKat 中的视频或图像更新图库,示例和代码如下所示:

保存到 SD 卡的图像不会出现在 Android 的图库应用中

Android MediaStore 插入视频

http://developer.android.com/reference/android/media/MediaScannerConnection.html

于 2016-01-01T13:45:58.347 回答