Android自定义相机照片未显示在图库中 [英] Android custom camera photos not shown in gallery

查看:92
本文介绍了Android自定义相机照片未显示在图库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建自定义的android相机布局.

I'm creating custom android camera layout.

我使用了 http://developer.android中的信息. com/guide/topics/media/camera.html#basics .除了一件事情,一切都很好.

I've used information from http://developer.android.com/guide/topics/media/camera.html#basics. Everithing works fine except one thing.

该照片未显示在手机的图库中.方法是:

The photo is not shown in the phone's gallery. The metod is:

private Camera.PictureCallback cameraCallback = new Camera.PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getOutputMediaFile();
        if (pictureFile == null){
            Log.d(Values.APPLICATION_TAG + ACTIVITY_TAG, "Error creating media file, check storage permissions: ");
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.flush();
            fos.close();
            createPhotoAndShowPreview(Uri.fromFile(pictureFile));
        } catch (FileNotFoundException e) {
            Log.d(Values.APPLICATION_TAG + ACTIVITY_TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(Values.APPLICATION_TAG + ACTIVITY_TAG, "Error accessing file: " + e.getMessage());
        }
    }
};

private static File getOutputMediaFile(){

    File galleryDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "MyCameraApp"); 
    if (!galleryDir.exists()) {
        if (!galleryDir.mkdir())
        {
            Log.d(Values.APPLICATION_TAG, "failed to create directory");
            return null;
        }
    }

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    mediaFile = new File(galleryDir.getPath() + File.separator +
            "gfranq_"+ timeStamp + ".jpg");

    return mediaFile;
}

方法createPhotoAndShowPreview(Uri.fromFile(pictureFile));工作正常,文件在运行时存在并且可以使用,但是当我退出应用程序时,图库中没有文件.我使用文件管理器来检查文件是否已删除,但未删除,退出应用程序后该文件存在,但系统无法识别它是映像.

Method createPhotoAndShowPreview(Uri.fromFile(pictureFile)); works fine, the file exists during runtime and I can use it, but when I exit the app there is no file in the gallery. I used a file manager to check if the file is deleted, but its not deleted, it exists after exiting the application, but the system doesn't recognize that it is an image.

更新:将手机连接到PC后,图库中出现了照片.但是直到我连接手机并断开手机与PC的连接后,新照片仍不会出现在图库中.重新启动手机具有相同的效果.

Update: The photos appered in the gallery after I connected my phone to my pc. But new photos still don't appear in the gallery untill I connect and disconnect my phone to pc. rebooting the phone has the same effect.

推荐答案

您需要发送广播:

context.sendBroadcast(new Intent(Intent.Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(imageFile)));

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imageFile)));

这篇关于Android自定义相机照片未显示在图库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆