Updating gallery after taking pictures Gallery not showing all the pictures Android Camera

家住魔仙堡 提交于 2019-12-18 07:07:15

问题


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

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

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.


回答1:


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

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




回答2:


Even if this is a little bit older! But I just had the same problem. Soni´s answer works but requires the Media Scanner every time to look for new files in the whole directory. You can also just register your new file that seems a bit more performant

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

f is the File you want to register.




回答3:


  1. simple answer is that first run the application, capture the image from camera of your application then simple see in gallery of your device.

  2. you can't see last capture image image by your application at that time one time restart your device and can see that your last capture image is in gallery of your device.

  3. one more thing, after your device restart then again open your own application and again capture one image from your application and see that your second capture image can see in gallery without any action in your device.



来源:https://stackoverflow.com/questions/8667623/updating-gallery-after-taking-pictures-gallery-not-showing-all-the-pictures-andr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!