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










share|improve this question























  • 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 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















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










share|improve this question























  • 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 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













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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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

















  • 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 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
















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


















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線