pilgr / Paper

Paper is a fast NoSQL-like storage for Java/Kotlin objects on Android with automatic schema migration support.
Apache License 2.0
2.34k stars 234 forks source link

Cannot share .pt file #131

Closed semmet95 closed 6 years ago

semmet95 commented 6 years ago

Sorry for posting this here, this is not an issue with the paper library but I'd really appreciate it if I someone helps me out here.

I am using the Paper library to store the object of a Java class. I'm trying to share the .pt file created using the path returned by the getPath function. I'm using an email client to share the file. Here is my code

AndroidManifest.xml <provider android:name="android.support.v4.content.FileProvider" android:authorities="amit.apps.aurora_raw3.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/paths" /> </provider>

paths.xml `<?xml version="1.0" encoding="utf-8"?>

` Here is the file path /data/data/amit.apps.aurora_raw3/files/io.paperdb/playlistobject.pt And following is the intent used to share the file Intent playlistintent=new Intent(android.content.Intent.ACTION_SEND); ``` String filePath=Paper.book().getPath("playlistobject"); Uri uri=Uri.parse(filePath); playlistintent.setType("plain/text"); playlistintent.putExtra(Intent.EXTRA_STREAM, uri); Intent backupIntent=new Intent(playlistintent); playlistintent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail"); List resInfoList = playlistFragment.getContext().getPackageManager() .queryIntentActivities(playlistintent, PackageManager.MATCH_DEFAULT_ONLY); for (ResolveInfo resolveInfo : resInfoList) { String packageName = resolveInfo.activityInfo.packageName; playlistFragment.getContext().grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); } try { playlistFragment.startActivity(playlistintent); } catch(ActivityNotFoundException ex) { playlistFragment.startActivity(Intent.createChooser(backupIntent, "Choose an email client")); } ``` I'm preferring Gmail to share the file but if it's not available I'm creating a chooser to let the user select another email client. The problem is that Gmail is giving the error "Couldn't send the attachment" even though it shows the playlistobject.pt file attached before sending the email while composing the message. When I tried the same with Yahoo mail I'm getting "attachment too large" message and it's not even shown. I tried many things but nothing worked.
semmet95 commented 6 years ago

Finally got it to work. What I did was copy the .pt file into the app's cache directory and get the uri using FileProvider. I used the code provided here http://stephendnicholas.com/posts/android-attaching-files-from-internal-cache-to-gmail Modified it a bit as it was intended specifically for using gmail to share file and modified the paths.xml file

`<?xml version="1.0" encoding="utf-8"?>

`