Java tutorial
//package com.java2s; /** * CommonUtils.java created on 2014-07-14. * * Copyright (C) 2014 P1nGu1n * * This file is part of Snapshare. * * Snapshare is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Snapshare is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * a gazillion times. If not, see <http://www.gnu.org/licenses/>. */ import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.provider.MediaStore; import java.io.File; public class Main { /** * Converts the content:// scheme to the file:// scheme * @param contentResolver Provides access to the content model * @param contentUri The URI to be converted using content:// scheme * @return The converted URI using file:// scheme */ public static Uri getFileUriFromContentUri(ContentResolver contentResolver, Uri contentUri) { String filePath = getPathFromContentUri(contentResolver, contentUri); if (filePath == null) { return null; } return Uri.fromFile(new File(filePath)); } /** * Converts the content:// scheme to the file path * @param contentResolver Provides access to the content model * @param contentUri The URI to be converted using content:// scheme * @return The converted file path */ public static String getPathFromContentUri(ContentResolver contentResolver, Uri contentUri) { String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = contentResolver.query(contentUri, projection, null, null, null); if (cursor != null && cursor.moveToFirst()) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); String filePath = cursor.getString(column_index); cursor.close(); return filePath; } else { return null; } } }