捕获的图像不会出现在图库中

时间:2014-03-03 18:42:10

标签: android

我使用相机意图拍摄图像。但是当我在画廊登记时,它没有显示出来。

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

Intent intent_cam = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                    
File imagesFolder = new File(Environment.getExternalStorageDirectory() + File.separator + "App Photos");

Intent mediaScan = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

if (!imagesFolder.exists()) {       

imagesFolder.mkdirs();          
File image = new File(imagesFolder, "App_" + timeStamp + ".png");
Uri uriSavedImage = Uri.fromFile(image);         
intent_cam.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);    
mediaScan.setData(uriSavedImage);
getActivity().sendBroadcast(mediaScan);
startActivityForResult(intent_cam, 0);           
}

else if (imagesFolder.exists()) {

File image = new File(imagesFolder, "App_" + timeStamp + ".png");
Uri uriSavedImage = Uri.fromFile(image);
intent_cam.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
mediaScan.setData(uriSavedImage);
getActivity().sendBroadcast(mediaScan);
startActivityForResult(intent_cam, 0);    

}

清单:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

sendBroadcast在API 19中不再有效吗?(4.4)? Android How to use MediaScannerConnection scanFile

2 个答案:

答案 0 :(得分:1)

您在捕获图片之前调用扫描请求。回想一下,startActivityForResult()是异步的。将sendBroadcast()来电置于onActivityResult(),当相机活动将控制权交还给您时,会将其触发。

答案 1 :(得分:0)

这适用于所有Android版本:

MediaScannerConnection.scanFile(this,new String [] {file.getPath()},new String [] {“image / jpeg”},null);