Exporting UART configuration fixed

+ file:// URIs can no longer be exported outside the app
This commit is contained in:
Aleksander Nowakowski
2017-11-30 17:59:38 +01:00
parent 4ea75cf9db
commit 7b88d8300d
2 changed files with 33 additions and 2 deletions

View File

@@ -752,7 +752,7 @@ public class UARTActivity extends BleProfileServiceReadyActivity<UARTService.UAR
// Notify user about the file
final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "text/xml");
intent.setDataAndType(FileHelper.getContentUri(this, file), "text/xml");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
final PendingIntent pendingIntent = PendingIntent.getActivity(this, 420, intent, 0);
final Notification notification = new NotificationCompat.Builder(this, ToolboxApplication.FILE_SAVED_CHANNEL).setContentIntent(pendingIntent).setContentTitle(fileName).setContentText(getText(R.string.uart_configuration_export_succeeded))

View File

@@ -22,10 +22,15 @@
package no.nordicsemi.android.nrftoolbox.utility;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.widget.Toast;
import java.io.File;
@@ -190,7 +195,7 @@ public class FileHelper {
/**
* Copies the file from res/raw with given id to given destination file. If dest does not exist it will be created.
*
* @param context activity context
* @param context activity context
* @param rawResId the resource id
* @param dest destination file
*/
@@ -212,4 +217,30 @@ public class FileHelper {
DebugLogger.e(TAG, "Error while copying HEX file " + e.toString());
}
}
public static Uri getContentUri(final Context context, final File file) {
final String filePath = file.getAbsolutePath();
final Uri uri = MediaStore.Files.getContentUri("external");
final Cursor cursor = context.getContentResolver().query(
uri,
new String[]{BaseColumns._ID},
MediaStore.Files.FileColumns.DATA + "=? ",
new String[]{filePath}, null);
try {
if (cursor != null && cursor.moveToFirst()) {
final int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
return Uri.withAppendedPath(uri, String.valueOf(id));
} else {
if (file.exists()) {
final ContentValues values = new ContentValues();
values.put(MediaStore.Files.FileColumns.DATA, filePath);
return context.getContentResolver().insert(uri, values);
} else {
return null;
}
}
} finally {
cursor.close();
}
}
}