Android examples for android.provider:MediaStore
Gets media file using file path and file name if exists
//package com.java2s; import java.io.File; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; public class Main { /**/*from ww w . j ava 2 s.co m*/ * Gets media file using file path and file name if exists * @param filepath * @param filename * @return */ public static File getOutMediaFile(String filepath, String filename) { File mediaStorageDir = new File( Environment.getExternalStorageDirectory(), filepath); File mediaFile = null; if (mediaStorageDir.exists()) mediaFile = new File(mediaStorageDir.getPath() + File.separator + filename); return mediaFile; } /** * Gets media file from Uri. * @param uri * @return */ public static File getOutMediaFile(Context context, Uri uri) { File mediaFile = null; mediaFile = new File(getRealPathFromUri(context, uri)); return mediaFile; } /** * Gets real path from the URI * @param context * @param contentUri * @return */ public static String getRealPathFromUri(Context context, Uri contentUri) { Cursor cursor = null; try { String[] proj = { MediaStore.Images.Media.DATA }; cursor = context.getContentResolver().query(contentUri, proj, null, null, null); if (cursor == null) return null; if (cursor.getColumnIndex(MediaStore.Images.Media.DATA) != -1) { int columnIndex = cursor .getColumnIndex(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(columnIndex); } return null; } finally { if (cursor != null) cursor.close(); } } }