拍摄照片并把它放在图库 [英] taking a photo and placing it in the Gallery

查看:155
本文介绍了拍摄照片并把它放在图库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示自己的 SurfaceView 的子类,带有摄像头preVIEW,并有自己的拍摄按钮的应用程序。我使用 Camera.takePicture 来拍摄照片,并在 onPictureTaken 回调,直接养活图像数据转换成 MediaStore.Images.Media.insertImage 。 (这似乎比写的图像文件,然后将其添加到图库简单,但也许这是一个坏主意。)

和图片在图库中显示出来!然而,这是在画廊,这使得它很难找到的结束。我想它在画廊的开头展现出来,就像使用普通相机应用程序拍摄的照片。

据我所知道的,问题是,股票相机应用名称的文件, IMG_YYYYMMDD_ [时间] .JPG ,而我的照片最终成为 [unix时间戳] .JPG 。但我不知道怎么告诉 MediaStore 来解决这个问题。

下面是code:

 公共无效捕捉(){
    mCamera.takePicture(NULL,NULL,mPicture);
}最后PictureCallback mPicture =新PictureCallback(){
    公共无效onPictureTaken(字节[]数据,相机摄像头){
         MediaStore.Images.Media.insertImage(getContentResolver(),
                                             BitmapFactory.de codeByteArray的(数据,0,data.length)
                                             NULL,NULL);
      }
    };


解决方案

实际的解决办法是添加日期的元数据。最后的结果(其中仍含有定向缺陷)是

 最后PictureCallback mPicture =新PictureCallback(){    公共无效onPictureTaken(字节[]数据,相机摄像头){        //创建一个媒体文件名
        字符串title =IMG _+新的SimpleDateFormat(YYYYMMDD_HHMMSS)格式(新的Date());        字符串DCIM = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)的ToString();
        字符串DIRECTORY = DCIM +/镜头;
        字符串路径=目录+'/'+标题+.JPG;        FileOutputStream中走出= NULL;
        尝试{
            OUT =新的FileOutputStream(路径);
            out.write(数据);
        }赶上(例外五){
            Log.e(pictureTaken,无法写入数据,E);
        } {最后
            尝试{
                out.close();
            }赶上(例外五){
                Log.e(pictureTaken,无法写入后关闭文件,E);
            }
        }        // INSERT INTO MediaStore。
        ContentValues​​值=新ContentValues​​(5);
        values​​.put(ImageColumns.TITLE,职称);
        values​​.put(ImageColumns.DISPLAY_NAME,标题+.JPG);
        values​​.put(ImageColumns.DATE_TAKEN,System.currentTimeMillis的());
        values​​.put(ImageColumns.DATA,路径);
        //在度顺时针旋转。 0,90,180或270。
        values​​.put(ImageColumns.ORIENTATION,activity.getWindowManager()。getDefaultDisplay()
                .getRotation()+ 90);        URI URI = NULL;
        尝试{
            URI = activity.getContentResolver()插入(Images.Media.EXTERNAL_CONTENT_URI,价值观)。
        }赶上(的Throwable日){
            //当外部卷已安装就会发生这种情况,但
            // MediaScanner没有通知MediaProvider添加该卷。
            //图片仍然是安全和MediaScanner会发现它与
            //将其插入MediaProvider。唯一的问题是,用户
            //不能单击缩略图查看图片。
            Log.e(pictureTaken,无法写入MediaStore+个);
        }
      }
    };

I have an app which displays its own subclass of SurfaceView with a camera preview, and has its own capture button. I use Camera.takePicture to take the picture, and in the onPictureTaken callback, feed the image data directly into MediaStore.Images.Media.insertImage. (That seemed simpler than writing the image to file and then adding it to the Gallery, but maybe it's a bad idea.)

And the picture shows up in the Gallery! However, it's at the end of the Gallery, which makes it very hard to find. I'd like it to show up at the beginning of the gallery, just like a picture taken with the regular Camera app.

As far as I can tell, the problem is that the stock Camera app names the files as IMG_YYYYMMDD_[time].jpg, while my photos end up as [unix timestamp].jpg. But I don't know how to tell MediaStore to fix that.

Here is the code:

public void capture() {
    mCamera.takePicture(null, null, mPicture);
}

final PictureCallback mPicture = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
         MediaStore.Images.Media.insertImage(getContentResolver(),
                                             BitmapFactory.decodeByteArray(data, 0, data.length),
                                             null, null);
      }
    };

解决方案

The actual solution was to add date metadata. The final result (which still contains an orientation bug) is

final PictureCallback mPicture = new PictureCallback() {

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

        // Create a media file name
        String title = "IMG_"+ new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

        String DCIM = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString();
        String DIRECTORY = DCIM + "/Camera";
        String path = DIRECTORY + '/' + title + ".jpg";

        FileOutputStream out = null;
        try {
            out = new FileOutputStream(path);
            out.write(data);
        } catch (Exception e) {
            Log.e("pictureTaken", "Failed to write data", e);
        } finally {
            try {
                out.close();
            } catch (Exception e) {
                Log.e("pictureTaken", "Failed to close file after write", e);
            }
        }

        // Insert into MediaStore.
        ContentValues values = new ContentValues(5);
        values.put(ImageColumns.TITLE, title);
        values.put(ImageColumns.DISPLAY_NAME, title + ".jpg");
        values.put(ImageColumns.DATE_TAKEN, System.currentTimeMillis());
        values.put(ImageColumns.DATA, path);
        // Clockwise rotation in degrees. 0, 90, 180, or 270.
        values.put(ImageColumns.ORIENTATION, activity.getWindowManager().getDefaultDisplay()
                .getRotation() + 90);

        Uri uri = null;
        try {
            uri = activity.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
        } catch (Throwable th)  {
            // This can happen when the external volume is already mounted, but
            // MediaScanner has not notify MediaProvider to add that volume.
            // The picture is still safe and MediaScanner will find it and
            // insert it into MediaProvider. The only problem is that the user
            // cannot click the thumbnail to review the picture.
            Log.e("pictureTaken", "Failed to write MediaStore" + th);
        }
      }
    };

这篇关于拍摄照片并把它放在图库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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