Android: get uri realpath return null
up vote
0
down vote
favorite
The code below uploads a file for an user. How can I fix it? I selected a file from my drive and data.getData()
is not null, but get real path does not work:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
Log.i("errorCheck","L: "+getPath(this, uri));
}
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
public static String getDataColumn(Context context, Uri uri, String selection,
String selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
Log.i("errorCheck", "KIT: "+isKitKat);
// DocumentProvider
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String selectionArgs = new String {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
But it always returns null.
My mobile android version : 4.3
Mobile : Galaxy S3
android uri realpath
|
show 3 more comments
up vote
0
down vote
favorite
The code below uploads a file for an user. How can I fix it? I selected a file from my drive and data.getData()
is not null, but get real path does not work:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
Log.i("errorCheck","L: "+getPath(this, uri));
}
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
public static String getDataColumn(Context context, Uri uri, String selection,
String selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
Log.i("errorCheck", "KIT: "+isKitKat);
// DocumentProvider
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String selectionArgs = new String {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
But it always returns null.
My mobile android version : 4.3
Mobile : Galaxy S3
android uri realpath
stackoverflow.com/questions/10386885/…
– teh_raab
Nov 10 at 12:06
See this and this and this.
– CommonsWare
Nov 10 at 12:08
@teh_raab this code return null
– soltan world
Nov 10 at 12:13
@CommonsWare i saw them, but none of them not work
– soltan world
Nov 10 at 12:14
1
Perhaps you should write a Stack Overflow question where you provide a Minimal, Complete, and Verifiable example showing what you tried with respect usingopenInputStream()
to work with aUri
, explaining how you have demonstrated that "none of them work" and asking for help. Or, if you only want to work with files, use a file chooser library.
– CommonsWare
Nov 10 at 12:18
|
show 3 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
The code below uploads a file for an user. How can I fix it? I selected a file from my drive and data.getData()
is not null, but get real path does not work:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
Log.i("errorCheck","L: "+getPath(this, uri));
}
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
public static String getDataColumn(Context context, Uri uri, String selection,
String selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
Log.i("errorCheck", "KIT: "+isKitKat);
// DocumentProvider
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String selectionArgs = new String {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
But it always returns null.
My mobile android version : 4.3
Mobile : Galaxy S3
android uri realpath
The code below uploads a file for an user. How can I fix it? I selected a file from my drive and data.getData()
is not null, but get real path does not work:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
Log.i("errorCheck","L: "+getPath(this, uri));
}
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
public static String getDataColumn(Context context, Uri uri, String selection,
String selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
Log.i("errorCheck", "KIT: "+isKitKat);
// DocumentProvider
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String selectionArgs = new String {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
But it always returns null.
My mobile android version : 4.3
Mobile : Galaxy S3
android uri realpath
android uri realpath
edited Nov 10 at 22:42
Daniel Kaparunakis
2,11191626
2,11191626
asked Nov 10 at 11:59
soltan world
63
63
stackoverflow.com/questions/10386885/…
– teh_raab
Nov 10 at 12:06
See this and this and this.
– CommonsWare
Nov 10 at 12:08
@teh_raab this code return null
– soltan world
Nov 10 at 12:13
@CommonsWare i saw them, but none of them not work
– soltan world
Nov 10 at 12:14
1
Perhaps you should write a Stack Overflow question where you provide a Minimal, Complete, and Verifiable example showing what you tried with respect usingopenInputStream()
to work with aUri
, explaining how you have demonstrated that "none of them work" and asking for help. Or, if you only want to work with files, use a file chooser library.
– CommonsWare
Nov 10 at 12:18
|
show 3 more comments
stackoverflow.com/questions/10386885/…
– teh_raab
Nov 10 at 12:06
See this and this and this.
– CommonsWare
Nov 10 at 12:08
@teh_raab this code return null
– soltan world
Nov 10 at 12:13
@CommonsWare i saw them, but none of them not work
– soltan world
Nov 10 at 12:14
1
Perhaps you should write a Stack Overflow question where you provide a Minimal, Complete, and Verifiable example showing what you tried with respect usingopenInputStream()
to work with aUri
, explaining how you have demonstrated that "none of them work" and asking for help. Or, if you only want to work with files, use a file chooser library.
– CommonsWare
Nov 10 at 12:18
stackoverflow.com/questions/10386885/…
– teh_raab
Nov 10 at 12:06
stackoverflow.com/questions/10386885/…
– teh_raab
Nov 10 at 12:06
See this and this and this.
– CommonsWare
Nov 10 at 12:08
See this and this and this.
– CommonsWare
Nov 10 at 12:08
@teh_raab this code return null
– soltan world
Nov 10 at 12:13
@teh_raab this code return null
– soltan world
Nov 10 at 12:13
@CommonsWare i saw them, but none of them not work
– soltan world
Nov 10 at 12:14
@CommonsWare i saw them, but none of them not work
– soltan world
Nov 10 at 12:14
1
1
Perhaps you should write a Stack Overflow question where you provide a Minimal, Complete, and Verifiable example showing what you tried with respect using
openInputStream()
to work with a Uri
, explaining how you have demonstrated that "none of them work" and asking for help. Or, if you only want to work with files, use a file chooser library.– CommonsWare
Nov 10 at 12:18
Perhaps you should write a Stack Overflow question where you provide a Minimal, Complete, and Verifiable example showing what you tried with respect using
openInputStream()
to work with a Uri
, explaining how you have demonstrated that "none of them work" and asking for help. Or, if you only want to work with files, use a file chooser library.– CommonsWare
Nov 10 at 12:18
|
show 3 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238713%2fandroid-get-uri-realpath-return-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
stackoverflow.com/questions/10386885/…
– teh_raab
Nov 10 at 12:06
See this and this and this.
– CommonsWare
Nov 10 at 12:08
@teh_raab this code return null
– soltan world
Nov 10 at 12:13
@CommonsWare i saw them, but none of them not work
– soltan world
Nov 10 at 12:14
1
Perhaps you should write a Stack Overflow question where you provide a Minimal, Complete, and Verifiable example showing what you tried with respect using
openInputStream()
to work with aUri
, explaining how you have demonstrated that "none of them work" and asking for help. Or, if you only want to work with files, use a file chooser library.– CommonsWare
Nov 10 at 12:18