1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-29 11:10:35 +02:00

Use a file provider to implement sharing

This commit is contained in:
Erik Johansson
2018-02-12 23:13:18 +01:00
parent f2d7b355a8
commit b9f4c7de8e
4 changed files with 30 additions and 16 deletions

View File

@@ -34,6 +34,16 @@
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
</application>
</manifest>

View File

@@ -29,6 +29,7 @@ import android.os.Environment;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.view.Gravity;
@@ -49,6 +50,7 @@ import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import com.health.openscale.BuildConfig;
import com.health.openscale.R;
import com.health.openscale.core.OpenScale;
import com.health.openscale.core.datatypes.ScaleMeasurement;
@@ -169,9 +171,7 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
}
return true;
case R.id.shareData:
if (PermissionHelper.requestWritePermission(getActivity())) {
shareTable();
}
shareTable();
return true;
default:
return false;
@@ -368,26 +368,25 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
private void shareTable() {
final ScaleUser selectedScaleUser = OpenScale.getInstance(getContext()).getSelectedScaleUser();
String exportFilename = prefs.getString("exportFilename" + selectedScaleUser.getId(), "openScale_data_" + selectedScaleUser.getUserName() + ".csv");
String fullPath = Environment.getExternalStorageDirectory().getPath() + "/tmp/" + exportFilename;
if (!OpenScale.getInstance(getContext()).exportData(fullPath)) {
File shareFile = new File(getContext().getCacheDir(),
String.format("openScale %s.csv", selectedScaleUser.getUserName()));
if (!OpenScale.getInstance(getContext()).exportData(shareFile.getPath())) {
return;
}
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
File shareFile = new File(fullPath);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("text/csv");
if(shareFile.exists()) {
intentShareFile.setType("text/comma-separated-values");
intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+fullPath));
final Uri uri = FileProvider.getUriForFile(
getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", shareFile);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intentShareFile.putExtra(Intent.EXTRA_SUBJECT, "openScale export csv file");
intentShareFile.putExtra(Intent.EXTRA_TEXT, exportFilename);
intent.putExtra(Intent.EXTRA_SUBJECT,
getResources().getString(R.string.label_share_subject, selectedScaleUser.getUserName()));
startActivity(Intent.createChooser(intentShareFile, getResources().getString(R.string.label_share)));
}
startActivity(Intent.createChooser(intent, getResources().getString(R.string.label_share)));
}
@Override

View File

@@ -25,6 +25,7 @@
<string name="label_delete">Delete</string>
<string name="label_add_user">Add user</string>
<string name="label_share">Share</string>
<string name="label_share_subject">openScale CSV data export (%s)</string>
<string name="label_id">Id</string>
<string name="label_weight">Weight</string>

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path name="cache" path="/" />
</paths>