Reputation: 63
I'm trying to get the file path for a content URI. URL looks like this: content://com.android.providers.downloads.documents/document/31
The cursor object is not null but the cursor.getString(column_index) returns null.
Column index is always 0.
public static String getPath(Context context, Uri uri) throws URISyntaxException {
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()) {
// Method returns here with null value
return cursor.getString(column_index);
}
} catch (Exception e) {
// Eat it
}
}
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
EDIT: The content URI is returned from the file manager so it should represent an actual file.
public void filePicker(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}
Upvotes: 0
Views: 6767
Reputation: 853
This helper class can help, i found online in a github library
import android.content.ContentResolver
import android.content.Context
import android.net.Uri
import android.webkit.MimeTypeMap
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale.getDefault
/**
* This class will create a temporary file in the cache if need.
*
* When the uri already have `file://` schema we don't need to create a new file.
* The temporary file will always override a previous one, saving memory.
* Using the cache memory(context.cacheDir) we guarantee to not leak memory
*
* @param context used to access Android APIs, like content resolve, it is your activity/fragment.
* @param uri the URI to load the image from.
* @param uniqueName If true, make each image cropped have a different file name, this could cause
* memory issues, use wisely.
*
* @return string value of the File path.
*/
fun getFilePathFromUri(context: Context, uri: Uri, uniqueName: Boolean): String =
if (uri.path?.contains("file://") == true) uri.path!!
else getFileFromContentUri(context, uri, uniqueName).path
private fun getFileFromContentUri(context: Context, contentUri: Uri, uniqueName: Boolean): File {
// Preparing Temp file name
val fileExtension = getFileExtension(context, contentUri) ?: ""
val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", getDefault()).format(Date())
val fileName = ("temp_file_" + if (uniqueName) timeStamp else "") + ".$fileExtension"
// Creating Temp file
val tempFile = File(context.cacheDir, fileName)
tempFile.createNewFile()
// Initialize streams
var oStream: FileOutputStream? = null
var inputStream: InputStream? = null
try {
oStream = FileOutputStream(tempFile)
inputStream = context.contentResolver.openInputStream(contentUri)
inputStream?.let { copy(inputStream, oStream) }
oStream.flush()
} catch (e: Exception) {
e.printStackTrace()
} finally {
// Close streams
inputStream?.close()
oStream?.close()
}
return tempFile
}
private fun getFileExtension(context: Context, uri: Uri): String? =
if (uri.scheme == ContentResolver.SCHEME_CONTENT)
MimeTypeMap.getSingleton().getExtensionFromMimeType(context.contentResolver.getType(uri))
else uri.path?.let { MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(File(it)).toString()) }
@Throws(IOException::class)
private fun copy(source: InputStream, target: OutputStream) {
val buf = ByteArray(8192)
var length: Int
while (source.read(buf).also { length = it } > 0) {
target.write(buf, 0, length)
}
}
Upvotes: 1
Reputation: 1006554
I'm trying to get the file path for a content URI
There is no requirement that a content://
Uri
point to a file, let alone one that you can access. If you want to access the data in the file, use a ContentResolver
and openInputStream()
or openOutputStream()
.
Upvotes: 2