我目前正在开发一个Android应用程序,其中用户拍摄存储在设备上的外部存储器中的照片,然后我也试图让图库扫描文件以将新媒体添加到图库但是这似乎不会更新直到设备重启.
我使用的代码如下:
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Log.v("INFO", mCurrentPhotoPath.toString());
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
Run Code Online (Sandbox Code Playgroud)
使用我的应用程序拍照后,我可以使用设备上的文件管理器应用程序验证它是否存在,并且在将文件传递给Media Scanner之前记录它看起来是正确的文件路径
file:/storage/emulated/0/Pictures/JPEG_20140712_163043_1418054327.jpg
Run Code Online (Sandbox Code Playgroud)
谢谢亚伦
我正在开发一个基本的相机应用程序。我可以拍照,并且可以在我的应用程序中查看它。但我想在图库中异步查看我的照片。重新启动手机后,我可以在图库中看到我的照片。
示例代码。
public class PhotosActivity extends Fragment implements View.OnClickListener {
private final int REQUEST_CODE = 100;
private Button fotobutton;
private ImageView foto_image;
private static final String IMAGE_DIRECTORY_NAME = "OlkunMustafa";
public static final int MEDIA_TYPE_IMAGE = 1;
private Uri fileUri;
// Directory name to store captured images and videos
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.photos_activity,container,false);
fotobutton = (Button) rootView.findViewById(R.id.fotobutton);
foto_image = (ImageView) rootView.findViewById(R.id.foto_image);
fotobutton.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) { …Run Code Online (Sandbox Code Playgroud)