How to get file path of google drive document in Android - android

I am trying to access file from device. All works goods but when i browse and select file from google drive it gives null path. I have seen many question but didn't got the answer. One solution which i got below only gives me the file name.
public static String getGDriveDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_display_name";
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;
}
Is it possible to get that file? Because that file is on the drive and not on the device. Please help me how can i get that file.

Related

getContentResolver() value null for data but receiving name correctly

I'm not able to read data part from the selected image Uri, this happens on some specific device Lg nexus 5 api 6.0.1
Uri of a selected image
content://com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/200/ORIGINAL/NONE/2077196451
Code used to get data from the uri
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 index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
}catch (Exception e){
e.printStackTrace();
}
finally {
if (cursor != null)
cursor.close();
}
return null;
}
Not issue with the permissions handling, Required Permissions are given.

Content provider not working in one plus devices

I have two applications, one contains content provider and other app receives data using content resolver. If i add any data form provider that should be displayed from receiver in second app, this is the expected functionality.But after adding data once I remove first app from stack then second app displays null cursor,If I keep first app in stack, then second app displays correct value .(This issue only comes in one plus devices)
code snippet where cursor value coming null is,
Cursor c = getContentResolver().query(CONTENT_URI, null, null, null,
null);
may be it will help you
private String uriToFilename(Uri uri) {
String path = null;
if (Build.VERSION.SDK_INT < 11) {
path = getRealPathFromURI_BelowAPI11(this, uri);
} else if (Build.VERSION.SDK_INT < 19) {
path = getRealPathFromURI_API11to18(this, uri);
} else {
path = getRealPathFromURI_API19(this, uri);
}
return path;
}
BelowAPI11
public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) {
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index
= cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
API11to18
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
String[] proj = {MediaStore.Images.Media.DATA};
String result = null;
CursorLoader cursorLoader = new CursorLoader(
context,
contentUri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
if (cursor != null) {
int column_index =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
}
return result;
}
API19
public static String getRealPathFromURI_API19(Context context, Uri uri) {
Log.e("uri", uri.getPath());
String filePath = "";
if (DocumentsContract.isDocumentUri(context, uri)) {
String wholeID = DocumentsContract.getDocumentId(uri);
Log.e("wholeID", wholeID);
// Split at colon, use second item in the array
String[] splits = wholeID.split(":");
if (splits.length == 2) {
String id = splits[1];
String[] column = {MediaStore.Images.Media.DATA};
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{id}, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
}
} else {
filePath = uri.getPath();
}
return filePath;
}
Enable Don’t optimize inside Settings to resolve one plus issue.
Settings –> Battery –> Battery Optimization –> Your App –> Don’t optimize
enter image description here

Cursor always returning null on query

I'm using an Intent to select a file from my Android device. When shown the Chooser, I select the Astro file manager and then procede to select a file (a video in this case).
I get back a URI (content://com.metago.astro.filecontent/file/file/sdcard/Download/video_video.mp4) and I'm trying to process it with the following:
Cursor cursor = null;
final String column = null;
final String[] projection = null;
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
// Never enters here because cursor always null
L.p("Hi!");
}
} catch (Exception e) {
L.p(e.getMessage()); // Never hit
} finally {
if (cursor != null)
cursor.close();
}
however cursor always returns null . Any idea why this happens?

File can't be found in file DB

I am starting an intent to pick a file, I do pick a file and I get a URI, which I obviously can't use.
I am trying to convert this Uri to a legit file path and for that I use this method
public string GetPathToImage(global::Android.Net.Uri uri)
{
string path = null;
// The projection contains the columns we want to return in our query.
string[] projection = new[] { global::Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data };
using (global::Android.Database.ICursor cursor = ManagedQuery(uri, projection, null, null, null))
{
if (cursor != null)
{
int columnIndex = cursor.GetColumnIndexOrThrow(global::Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data);
cursor.MoveToFirst();
path = cursor.GetString(columnIndex);
}
}
return path;
}
Problem: this method returns a null string. But I need a working one.
Any suggestions?

Android Get Uri path to file from Google Drive

I have this code to upload a file to my app, and when the file is opened with a file manager, or dropbox, or anything else, the returned path is correct and I can access it, I am only having problems with Google Drive, it returns some path starting with "exposed_content" and I can't "decode" it any way, I have searched and didn't found a way, anyone have any idea?
if (resultCode == Activity.RESULT_OK) {
if ((data != null) && (data.getData() != null)) {
final Uri filePath;
if (data.getDataString().startsWith("content")) {
filePath = getRealPathFromURI(getApplicationContext(), data.getData());
} else {
filePath = data.getData();
}
// TODO bug with google drive
if (filePath.getLastPathSegment() != null) {
tvSelectedFile.setText("File selected: " + filePath.getLastPathSegment());
} else {
tvSelectedFile.setText("File can not be accessed, please try another way");
}
}
}
Use the attached code... from onActivity result you will get content uri ...pass this uri to the given method...
public static String getGDriveDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_display_name";
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;
}

Categories

Resources