如何在 Android 中捕获图像并将其显示在图库中?

发布于 2024-08-07 13:47:14 字数 565 浏览 10 评论 0原文

在画廊中出现之前我正在捕捉图像,但现在它们没有出现,我不明白为什么。这是我的代码:

        ContentValues values = new ContentValues();
    values.put(android.provider.MediaStore.Images.Media.IS_PRIVATE, false);
    String name = "ugc_" + String.valueOf(System.currentTimeMillis());
    values.put(android.provider.MediaStore.Images.Media.TITLE, name);
    imageURI = getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

我也尝试过: value.put(android.provider.MediaStore.Images.Media.IS_PRIVATE, 0); 以及以该值离开,但现在似乎没有任何作用。

I was capturing images before that were showing up in the gallery, but now they are not and I can't figure out why. Here is my code:

        ContentValues values = new ContentValues();
    values.put(android.provider.MediaStore.Images.Media.IS_PRIVATE, false);
    String name = "ugc_" + String.valueOf(System.currentTimeMillis());
    values.put(android.provider.MediaStore.Images.Media.TITLE, name);
    imageURI = getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

I've also tried:
values.put(android.provider.MediaStore.Images.Media.IS_PRIVATE, 0);
as well as leaving at that value, but nothing seems to work now.

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

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

发布评论

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

评论(1

浪荡不羁 2024-08-14 13:47:14

非常不漂亮,但这就是我正在做的。请注意,由于错误,保存的文件为 1/ 16 全分辨率(每个维度 1/4)。

String SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory() + File.separator + "tmpPhoto.jpg";
Intent takePictureFromCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureFromCameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new
            File(SD_CARD_TEMP_DIR)));
startActivityForResult(takePictureFromCameraIntent, TAKE_PICTURE_WITH_CAMERA);



public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //  Picture taken from camera
        if (requestCode == TAKE_PICTURE_WITH_CAMERA) {
            if (resultCode == Activity.RESULT_OK) {

                // http://code.google.com/p/android/issues/detail?id=1480
                //Toast.makeText(AddPhotos.this, "" + data, Toast.LENGTH_SHORT).show();

                // on activity return
                File f = new File(SD_CARD_TEMP_DIR);
                try {
                    Uri capturedImage =
                        Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(),
                                f.getAbsolutePath(), null, null));


                        Log.i("camera", "Selected image: " + capturedImage.toString());

                    f.delete();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }
            else {
                Log.i("Camera", "Result code was " + resultCode);

            }
        }   
     }

Very much not pretty, but this is how I'm doing it. Note that due to a bug, the file saved is 1/16 the full resolution (1/4 in each dimension).

String SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory() + File.separator + "tmpPhoto.jpg";
Intent takePictureFromCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureFromCameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new
            File(SD_CARD_TEMP_DIR)));
startActivityForResult(takePictureFromCameraIntent, TAKE_PICTURE_WITH_CAMERA);

.
.
.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //  Picture taken from camera
        if (requestCode == TAKE_PICTURE_WITH_CAMERA) {
            if (resultCode == Activity.RESULT_OK) {

                // http://code.google.com/p/android/issues/detail?id=1480
                //Toast.makeText(AddPhotos.this, "" + data, Toast.LENGTH_SHORT).show();

                // on activity return
                File f = new File(SD_CARD_TEMP_DIR);
                try {
                    Uri capturedImage =
                        Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(),
                                f.getAbsolutePath(), null, null));


                        Log.i("camera", "Selected image: " + capturedImage.toString());

                    f.delete();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }
            else {
                Log.i("Camera", "Result code was " + resultCode);

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