Android Newly Taken Photo from custom camera does not appear in gallery (media store)

非 Y 不嫁゛ 提交于 2019-12-02 10:45:19

问题


  1. in my app, i initially load all the images from the gallery, i.e. (MediaStore.Images.Media.EXTERNAL_CONTENT_URI)

  2. user can take picture via my app, but i have a custom camera, i.e. i do not use default phone camera to take pictures, i have my own surface view, i deal with Camera object directly, and handle the camera picture. and i save this image in a custom folder.

  3. i want the newly taken picture to appear in my gallery once user returns to my app

problem: the newly taken picture does not appear in the cursor i loaded with MediaStore.Images.Media.EXTERNAL_CONTENT_URI

the problem is gone only when i reinstall the app and force a rescan.

how do i fix this? i want every picture that was immediately taken will appear in my cursor?

i have done some researhc, but don't tell me the contentvalues with the startactivity solution like below

 startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

this does not work, because again, i have my own camera, i am not using the default camera activity from the phone

i also tried

 getContentResolver().notifyChange(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null);

this does not work either

can someone please help?

Thanks


回答1:


You just need to send an broadcast "ACTION_MEDIA_SCANNER_SCAN_FILE" so that the mediascanner can scan for the image you saved.

 Intent mediaScanIntent = new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
 mediaScanIntent.setData(uri);
 sendBroadcast(mediaScanIntent);

just set the uri of the the newly created image. :)




回答2:


Yes, you are true, you need to provide the newly created file to the MediaStore.

MediaScannerConnection will help you.

MediaScannerConnection provides a way for applications to pass a newly created or downloaded media file to the media scanner service. The media scanner service will read metadata from the file and add the file to the media content provider. The MediaScannerConnectionClient provides an interface for the media scanner service to return the Uri for a newly scanned file to the client of the MediaScannerConnection class.




回答3:


This works for now:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){

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

}else{                      

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

}


来源:https://stackoverflow.com/questions/18374140/android-newly-taken-photo-from-custom-camera-does-not-appear-in-gallery-media-s

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