简体   繁体   English

Android-将从相机拾取的图像放入照片库

[英]Android - Get an image picked from a camera into a photo gallery

I am new to Android. 我是Android新手。 This is my first question. 这是我的第一个问题。 I am testing on 4.4.4, KitKat. 我正在KitKat 4.4.4上进行测试。

I am using the Android Developers "Taking Photos Simply" code in my app. 我在我的应用中使用Android开发人员的“简单拍摄照片”代码。 ( Taking Photos Simply Tutorial ) I want the photos taken by the user to be available to my app, but also available later in the gallery. 简单地拍摄照片教程 )我希望用户拍摄的照片可用于我的应用程序,但以后也可以在图库中使用。

If I specify 如果我指定

File storageDir =  getExternalFilesDir (null);

Then various cameras work (camera, opencamera, DSLR camera), but after I leave my app the images do not appear in the gallery. 然后可以使用各种相机(相机,opencamera,DSLR相机),但是离开我的应用程序后,图像不会出现在图库中。

If I specify 如果我指定

File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);

Then the behavior is the same, except for one camera app: DSLR Camera. 然后,行为是相同的,除了一个相机应用程序:DSLR相机。 The photo does appear in that app's camera directory which is visible using the gallery later. 该照片确实出现在该应用程序的相机目录中,稍后可使用图库查看该目录。

I want this behavior for my app. 我希望我的应用有这种行为。 I want the photos that the user takes to appear in a directory that they can later see using the gallery app. 我希望用户拍摄的照片出现在目录中,以便以后使用图库应用程序查看。 I don't want to create duplicate images, however. 但是,我不想创建重复的图像。

I also want to save modified images later on to the same directory. 我也想稍后将修改后的图像保存到同一目录中。

Thanks for any help 谢谢你的帮助

but after I leave my app the images do not appear in the gallery. 但是当我离开我的应用程序后,图像不会出现在图库中。

It may eventually. 最终可能。 Gallery apps usually use the MediaStore , and your file may eventually be added to it. Gallery应用程序通常使用MediaStore ,并且您的文件最终可能会添加到其中。 Use MediaScannerConnection and scanFile() to expedite this process. 使用MediaScannerConnectionscanFile()可以加快此过程。

You need to request the media scanner to scan a file and add it to the media database.Use this code. 您需要请求媒体扫描仪扫描文件并将其添加到媒体数据库。使用此代码。

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

and see ACTION_MEDIA_SCANNER_SCAN_FILE in android developer site.Here mMediaUri is the Uri of your file. 并在android开发人员网站中查看ACTION_MEDIA_SCANNER_SCAN_FILE 。此处mMediaUri是文件的Uri。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM