Android呼叫系統自帶的檔案管理器進行檔案選擇

edagarli發表於2014-05-10

http://blog.csdn.net/zqchn/article/details/8770913的補充

FileUtils檔案

public class FileUtils {
    public static String getPath(Context context, Uri uri) {
 
        if ("content".equalsIgnoreCase(uri.getScheme())) {
            String[] projection = { "_data" };
            Cursor cursor = null;
 
            try {
                cursor = context.getContentResolver().query(uri, projection,null, null, null);
                int column_index = cursor.getColumnIndexOrThrow("_data");
                if (cursor.moveToFirst()) {
                    return cursor.getString(column_index);
                }
            } catch (Exception e) {
                // Eat it
            }
        }
 
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }
 
        return null;
    }
}

相關文章