拍照图库没有显示所有图片Android摄像头后更新画廊画廊、摄像头、图库、图片

2023-09-07 00:05:24 作者:孤吟诗人

我使用的onClick通过按钮来拍摄图像。

I am using a onClick over a button to capture an image.

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被选中。

The image gets saved in the SD card. Then the user clicks on Send button and a layout opens, on Click of ImageView an Image Gallery opens and clicking on a particular image, the URI gets selected.

    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);
}

但我不能够查看图像抢购的画廊code,但可以通过FileBrowser看到的图像,当我在电脑打开SD卡,我可以看到的图像,然后在Android显示画廊图像也与在code。 让我知道了什么​​问题,以及如何解决这个问题,期待您的回复。 谢谢你。

But I am not able to view images snapped in Gallery code, but could see the images through FileBrowser and when I open the SD card in PC I can see the image and then the Android shows the image in Gallery also with in the code. Let me know what the issue is and how to solve it, looking forward to your reply. Thanks.

推荐答案

Android的扫描媒体文件和那些使用内容提供商时所显示 ...当你运行你的应用程序并采取新的相机在那个时候,你只是把新的图片,但是默认的库不知道这些新的图片因为他们没有在媒体内容提供商的名单。

Android scan media file and those are shown when using content provider...when your run your app and take new camera at that time you just took new pics but default gallery doesn't know about that new pics cause they are not in content provider's list of media.

我觉得后拍照,并呼吁画廊将展出从相机拍摄的所有最新的图片前加入这一行。

I think adding this line after taking picture and before calling gallery will show the all latest pics taken from camera.

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

这将扔在奇巧+一个SecurityException。尝试Vossi的方法,或使用MediaScannerConnection这里使用:stackoverflow - @absk

This will throw a SecurityException in KitKat+. Try Vossi's method, or use MediaScannerConnection as used here: stackoverflow – @absk