android - 从相机拍摄的图像中获取图像

发布于 2024-10-22 05:52:33 字数 230 浏览 2 评论 0原文

我很好奇如何从 Android 中的 Gallery/Camera 文件夹中获取图像。我正在查看文件管理器,但无法真正了解这些图像在文件系统中的位置。如果我进入文件管理器,我无法找到拍摄照片的确切位置。如果我转到图库应用程序,我会看到它们挂在“相机”文件夹中。

关于我想要完成的事情的一些信息:

获取所有现有照片,在某个时刻保存并将它们显示在我的活动中。我试图允许手机用户从现有照片中将头像分配到他们的个人资料中。

I am curious as to how to get images from the Gallery/Camera folder in Android. I am looking into the file manager and can't really get a grasp as to where the those images are located in the file system. If I go to File Manager I can't locate the exact location of my taken photos. If I go to Gallery app I see them hanging in the "Camera" folder.

A little about what I am trying to accomplish:

Get all existing photos, saved at some point and display them on my activity. I am trying to allow the phone user to assign an avatar to their profile from existing photos.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安稳善良 2024-10-29 05:52:33

这是代码,以防有人需要。这是在姜饼上测试的。

List<Image> existingPhotos = getCameraImages(this);

            photosGrid = (GridView)findViewById(R.id.gvExistingPhotos);
            LazyPhotosGridAdapter adapter = new LazyPhotosGridAdapter(this, existingPhotos);
            photosGrid.setAdapter(adapter);





public String getRealPathFromURI(Uri contentUri) {
            String[] proj = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }

        public static List<Image> getCameraImages(Context context) {
            final String[] projection = { MediaStore.Images.Media.DATA };
            final String selection = MediaStore.Images.Media.BUCKET_ID + " = ?";
            final String[] selectionArgs = { CAMERA_IMAGE_BUCKET_ID };
            final Cursor cursor = context.getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, 
                    projection, 
                    selection, 
                    selectionArgs, 
                    null);
            List<Image> result = new ArrayList<Image>(cursor.getCount());
            if (cursor.moveToFirst()) {
                final int dataColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                do {
                    final String data = cursor.getString(dataColumn);
                    result.add(new Image(data));
                } while (cursor.moveToNext());
            }
            cursor.close();
            return result;
        }

        public static final String CAMERA_IMAGE_BUCKET_NAME =
            Environment.getExternalStorageDirectory().toString()
            + "/DCIM/Camera";
        public static final String CAMERA_IMAGE_BUCKET_ID =
                getBucketId(CAMERA_IMAGE_BUCKET_NAME);

        /**
         * Matches code in MediaProvider.computeBucketValues. Should be a common
         * function.
         */
        public static String getBucketId(String path) {
            return String.valueOf(path.toLowerCase().hashCode());
        }

here is the code, in case somebody needs it. This is tested on gingerbread.

List<Image> existingPhotos = getCameraImages(this);

            photosGrid = (GridView)findViewById(R.id.gvExistingPhotos);
            LazyPhotosGridAdapter adapter = new LazyPhotosGridAdapter(this, existingPhotos);
            photosGrid.setAdapter(adapter);





public String getRealPathFromURI(Uri contentUri) {
            String[] proj = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }

        public static List<Image> getCameraImages(Context context) {
            final String[] projection = { MediaStore.Images.Media.DATA };
            final String selection = MediaStore.Images.Media.BUCKET_ID + " = ?";
            final String[] selectionArgs = { CAMERA_IMAGE_BUCKET_ID };
            final Cursor cursor = context.getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, 
                    projection, 
                    selection, 
                    selectionArgs, 
                    null);
            List<Image> result = new ArrayList<Image>(cursor.getCount());
            if (cursor.moveToFirst()) {
                final int dataColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                do {
                    final String data = cursor.getString(dataColumn);
                    result.add(new Image(data));
                } while (cursor.moveToNext());
            }
            cursor.close();
            return result;
        }

        public static final String CAMERA_IMAGE_BUCKET_NAME =
            Environment.getExternalStorageDirectory().toString()
            + "/DCIM/Camera";
        public static final String CAMERA_IMAGE_BUCKET_ID =
                getBucketId(CAMERA_IMAGE_BUCKET_NAME);

        /**
         * Matches code in MediaProvider.computeBucketValues. Should be a common
         * function.
         */
        public static String getBucketId(String path) {
            return String.valueOf(path.toLowerCase().hashCode());
        }
七七 2024-10-29 05:52:33

这个答案可能会有所帮助。这是一个常见问题,因此如果您在 StackOverflow 中搜索 Android 相机,可能会得到结果。

This answer might help. This is a common question, so you might get results if you searched StackOverflow for Android camera.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文