【问题标题】:Updating gallery after taking pictures Gallery not showing all the pictures Android Camera拍照后更新画廊画廊不显示所有照片Android相机
【发布时间】:2015-08-25 13:15:10
【问题描述】:

我在按钮上使用 onClick 来捕获图像。

PictureCallback myPictureCallback_JPG = new PictureCallback() {

    @Override
    public void onPictureTaken(byte[] arg0, Camera arg1) {

        FileOutputStream outStream = null;
        try {
            // Write to SD Card
            outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis())); 
            outStream.write(arg0);
            outStream.close();
            Toast.makeText(Photo.this, "Image Saved to SD Card", Toast.LENGTH_SHORT).show();
            System.out.println();
        } catch (FileNotFoundException e) { 
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        camera.startPreview();
    }};

图像保存在 SD 卡中。 然后用户单击发送按钮并打开一个布局,单击 ImageView 将打开一个图像库并单击特定图像,URI 被选中。

    imageAttachPhoto = (ImageView)findViewById(R.id.imageViewPhotoOne);
    imageAttachPhoto.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
        }
    });
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            imageAttachPhoto.setImageURI(selectedImageUri);
        }
    }
}

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

但我无法查看在图库代码中捕捉的图像,但可以通过 FileBrowser 查看图像,当我在 PC 中打开 SD 卡时,我可以看到图像,然后 Android 也会在图库中显示图像代码。 让我知道问题是什么以及如何解决它,期待您的回复。 谢谢。

【问题讨论】:

    标签: android android-camera


    【解决方案1】:

    即使这有点旧!但我也遇到了同样的问题。 Soni 的答案有效,但每次都需要媒体扫描仪在整个目录中查找新文件。您也可以只注册看起来性能更高的新文件

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+f.getAbsolutePath())));
    

    f 是你要注册的文件。

    【讨论】:

    • 这很棒。顺便说一句,您也可以直接使用 Uri.fromFile(f) 而不是 Uri.parse() 。看起来更好一点,做同样的事情。 :)
    • 正是我想要的,比扫描整个目录要快得多。我正在使用 fromFile 方法,它运行良好。
    • Uri.fromFile(file) 更好
    • 已弃用,调用者应迁移到直接将项目插入 MediaStore stackoverflow.com/questions/57726896/…
    【解决方案2】:

    Android 扫描媒体文件,并且在使用 content provider 时显示...不在内容提供商的媒体列表中。

    我认为在拍照后和调用图库之前添加此行将显示从相机拍摄的所有最新照片。

        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
                     Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
    

    这将在 KitKat+ 中引发 SecurityException。尝试 Vossi 的方法,或使用此处使用的 MediaScannerConnection:stackoverflow – @absk

    【讨论】:

    • 加上一个绝对完美的...让它工作...但每当我想以纵向模式拍摄图像时,方向将旋转 90 度。关于如何正确设置的任何想法。谢谢。
    • hmm ..没有得到你确切的原因是因为你正在调用默认意图..无论如何..如果没有通过互联网找到解决方案,那么在 SO 上提出另一个问题,人们会帮助你。
    • 这将在 KitKat+ 中引发 SecurityException。尝试 Vossi 的方法,或使用此处使用的 MediaScannerConnection:stackoverflow.com/questions/18624235/…
    • @absk 感谢您的评论。跟上。!!
    • java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=11300, uid=10512
    【解决方案3】:
    1. 简单的答案是首先运行应用程序,从应用程序的摄像头捕获图像,然后简单地在设备的图库中查看。

    2. 您当时无法看到您的应用程序最后一次捕获的图像,重新启动您的设备,并且可以看到您的最后一个捕获的图像在您设备的图库中。

    3. 还有一件事,在您的设备重新启动后,再次打开您自己的应用程序并再次从您的应用程序中捕获一张图像,然后您可以看到您的第二张捕获的图像可以在图库中看到,而无需在您的设备中进行任何操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      相关资源
      最近更新 更多