mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-02-04 21:13:54 +01:00
Move getFilePath to appropriate location
This commit is contained in:
parent
6cbb130bce
commit
a0ee35fa4c
@ -59,6 +59,7 @@ import nodomain.freeyourgadget.gadgetbridge.database.PeriodicExporter;
|
|||||||
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager;
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandPreferencesActivity;
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandPreferencesActivity;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec;
|
import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
|
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
|
||||||
@ -385,7 +386,7 @@ public class SettingsActivity extends AbstractSettingsActivity {
|
|||||||
}
|
}
|
||||||
Uri uri = Uri.parse(autoExportLocation);
|
Uri uri = Uri.parse(autoExportLocation);
|
||||||
try {
|
try {
|
||||||
String filePath = getFilePath(getApplicationContext(), uri);
|
String filePath = AndroidUtils.getFilePath(getApplicationContext(), uri);
|
||||||
if (filePath != null) {
|
if (filePath != null) {
|
||||||
return filePath;
|
return filePath;
|
||||||
}
|
}
|
||||||
@ -403,61 +404,6 @@ public class SettingsActivity extends AbstractSettingsActivity {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
As seen on stackoverflow https://stackoverflow.com/a/36714242/1207186
|
|
||||||
Try to find the file path of a document uri
|
|
||||||
*/
|
|
||||||
private String getFilePath(Context context, Uri uri) throws URISyntaxException {
|
|
||||||
String selection = null;
|
|
||||||
String[] selectionArgs = null;
|
|
||||||
// Uri is different in versions after KITKAT (Android 4.4), we need to
|
|
||||||
if (Build.VERSION.SDK_INT >= 19 && DocumentsContract.isDocumentUri(context.getApplicationContext(), uri)) {
|
|
||||||
if ("com.android.externalstorage.documents".equals(uri.getAuthority())) {
|
|
||||||
final String docId = DocumentsContract.getDocumentId(uri);
|
|
||||||
final String[] split = docId.split(":");
|
|
||||||
return Environment.getExternalStorageDirectory() + "/" + split[1];
|
|
||||||
} else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
|
|
||||||
final String id = DocumentsContract.getDocumentId(uri);
|
|
||||||
uri = ContentUris.withAppendedId(
|
|
||||||
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
|
|
||||||
} else if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
|
|
||||||
final String docId = DocumentsContract.getDocumentId(uri);
|
|
||||||
final String[] split = docId.split(":");
|
|
||||||
final String type = split[0];
|
|
||||||
if ("image".equals(type)) {
|
|
||||||
uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
|
|
||||||
} else if ("video".equals(type)) {
|
|
||||||
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
|
|
||||||
} else if ("audio".equals(type)) {
|
|
||||||
uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
|
|
||||||
}
|
|
||||||
selection = "_id=?";
|
|
||||||
selectionArgs = new String[]{
|
|
||||||
split[1]
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ("content".equalsIgnoreCase(uri.getScheme())) {
|
|
||||||
String[] projection = {
|
|
||||||
MediaStore.Images.Media.DATA
|
|
||||||
};
|
|
||||||
Cursor cursor = null;
|
|
||||||
try {
|
|
||||||
cursor = context.getContentResolver()
|
|
||||||
.query(uri, projection, selection, selectionArgs, null);
|
|
||||||
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
|
|
||||||
if (cursor.moveToFirst()) {
|
|
||||||
return cursor.getString(column_index);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
}
|
|
||||||
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
|
|
||||||
return uri.getPath();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* delayed execution so that the preferences are applied first
|
* delayed execution so that the preferences are applied first
|
||||||
*/
|
*/
|
||||||
|
@ -18,13 +18,21 @@ package nodomain.freeyourgadget.gadgetbridge.util;
|
|||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.ContentUris;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
|
import android.database.Cursor;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.os.Environment;
|
||||||
import android.os.ParcelUuid;
|
import android.os.ParcelUuid;
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
|
import android.provider.DocumentsContract;
|
||||||
|
import android.provider.MediaStore;
|
||||||
import android.support.v4.content.LocalBroadcastManager;
|
import android.support.v4.content.LocalBroadcastManager;
|
||||||
|
|
||||||
|
import java.net.URISyntaxException;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||||
@ -112,4 +120,62 @@ public class AndroidUtils {
|
|||||||
+ Integer.toHexString(Color.green(color))
|
+ Integer.toHexString(Color.green(color))
|
||||||
+ Integer.toHexString(Color.blue(color));
|
+ Integer.toHexString(Color.blue(color));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* As seen on stackoverflow https://stackoverflow.com/a/36714242/1207186
|
||||||
|
* Try to find the file path of a document uri
|
||||||
|
* @param context the application context
|
||||||
|
* @param uri the Uri for which the path should be resolved
|
||||||
|
* @return the path corresponding to the Uri as a String
|
||||||
|
* @throws URISyntaxException
|
||||||
|
*/
|
||||||
|
public static String getFilePath(Context context, Uri uri) throws URISyntaxException {
|
||||||
|
String selection = null;
|
||||||
|
String[] selectionArgs = null;
|
||||||
|
// Uri is different in versions after KITKAT (Android 4.4), we need to
|
||||||
|
if (Build.VERSION.SDK_INT >= 19 && DocumentsContract.isDocumentUri(context.getApplicationContext(), uri)) {
|
||||||
|
if ("com.android.externalstorage.documents".equals(uri.getAuthority())) {
|
||||||
|
final String docId = DocumentsContract.getDocumentId(uri);
|
||||||
|
final String[] split = docId.split(":");
|
||||||
|
return Environment.getExternalStorageDirectory() + "/" + split[1];
|
||||||
|
} else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
|
||||||
|
final String id = DocumentsContract.getDocumentId(uri);
|
||||||
|
uri = ContentUris.withAppendedId(
|
||||||
|
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
|
||||||
|
} else if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
|
||||||
|
final String docId = DocumentsContract.getDocumentId(uri);
|
||||||
|
final String[] split = docId.split(":");
|
||||||
|
final String type = split[0];
|
||||||
|
if ("image".equals(type)) {
|
||||||
|
uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
|
||||||
|
} else if ("video".equals(type)) {
|
||||||
|
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
|
||||||
|
} else if ("audio".equals(type)) {
|
||||||
|
uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
|
||||||
|
}
|
||||||
|
selection = "_id=?";
|
||||||
|
selectionArgs = new String[]{
|
||||||
|
split[1]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ("content".equalsIgnoreCase(uri.getScheme())) {
|
||||||
|
String[] projection = {
|
||||||
|
MediaStore.Images.Media.DATA
|
||||||
|
};
|
||||||
|
Cursor cursor = null;
|
||||||
|
try {
|
||||||
|
cursor = context.getContentResolver()
|
||||||
|
.query(uri, projection, selection, selectionArgs, null);
|
||||||
|
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
|
||||||
|
if (cursor.moveToFirst()) {
|
||||||
|
return cursor.getString(column_index);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
|
||||||
|
return uri.getPath();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user