Here you can find the source of getPathFromUri(Context context, Uri uri)
Parameter | Description |
---|---|
context | a parameter |
uri | a parameter |
public static String getPathFromUri(Context context, Uri uri)
//package com.java2s; /**/*from www. j a v a 2 s. co m*/ * Copyright (c) 2013-2014, Rinc Liu (http://rincliu.com). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.MediaStore; public class Main { /** * @param context * @param uri * @return */ public static String getPathFromUri(Context context, Uri uri) { String path = null; String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null); if (cursor != null && cursor.getCount() > 0 && cursor.getColumnCount() > 0) { int column_index = -1; try { column_index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); } catch (IllegalArgumentException e) { e.printStackTrace(); cursor.close(); } if (column_index != -1) { cursor.moveToFirst(); path = cursor.getString(column_index); } cursor.close(); } return path; } }