How can i share my pdf with dropbox and Google drive? - android

I have create a pdf and now want to upload it on Google drive and Dropbox but not able to upload. i have used the following code:
Uri pdfUri = Uri.parse("file://" + File.separator+ "sdcard" + File.separator + "A****" File.separator + "A****.pdf");
Intent shareIntent = ShareCompat.IntentBuilder.from(TripHome.this).setText("Share PDF doc").setType("application/pdf").setStream(pdfUri).getIntent().setPackage("com.google.android.apps.docs");
startActivity(shareIntent);

Take a look at user2459928's the answer here: Android Launch a Google Drive application from another application not uploaded file
Additionally, I would highly suggest wrapping the startActivity() in a try catch block just in case the user does not have the app that you are trying to launch installed on their device.
Uri pdfUri = Uri.fromFile(new File("path/to/file"));
try {
Intent shareIntent = ShareCompat.IntentBuilder.from((Activity) context)
.setText("Share PDF file")
.setType("application/pdf")
.setStream(pdfUri)
.getIntent()
.setPackage("com.google.android.apps.docs");
context.startActivity(shareIntent);
}catch (Exception e){
e.printStackTrace();
// show a Toast or messag saying "activity not found" or "application not installed on this device"
}

Related

Stray file path added to email on intent using FileProvider attachment

I've got an intent that uses FileProvider to read from internal file storage for my app to send a file via email (or similar apps). The code works great everywhere apparently except for Gmail, which strangely adds a version of the provider path itself to the list of addressees of the email.
This is the code generating the intent:
public static Intent getSendIntent(Uri uri) {
final Intent i = new Intent(Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.setDataAndType(uri, "message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[] { "my#email.com" });
i.putExtra(Intent.EXTRA_SUBJECT, "Export");
i.putExtra(Intent.EXTRA_TEXT, "See the attached...");
i.putExtra(Intent.EXTRA_STREAM, uri);
return i;
}
This is the code to start the activity:
File file = new File(getFilesDir(), filename);
Uri uri = FileProvider.getUriForFile(MainActivity.this, "com.example.myapplication.provider", file);
Intent i = Utils.getSendIntent(uri);
try {
startActivity(Intent.createChooser(i, "Send file..."));
} catch (Exception e) {
Log.d(TAG, "failed to start send activity: " + e.getMessage());
Toast.makeText(MainActivity.this, "No suitable activity found for export.", Toast.LENGTH_SHORT).show();
}
Works well in Slack, Evernote, etc. but in Gmail in addition to the email address I've provided, another addressee in this kind of format is added:
//com.example.myapplication.provider/my_files/filefile.csv
which prevents the email from sending until it's manually removed from the message. Everything else about the message is as expected.
Any clue how to prevent this?
The following Captain Obvious solution resolved the problem.
Replace:
i.setDataAndType(uri, "message/rfc822");
with:
i.setType("message/rfc822");
since, as #CommonsWare's question inferred, the file content isn't itself in rfc822 format.

How can I put my program in the action list in android

When you want to share a file like the music file, if you have telegram and viber in your device you can choose one of these to share the music file. How can I do this? How can I add my program in the action list? What are the principles?
Android uses Intents for operations to be performed. Read the documentation for more information.
For example to share an mp3 file you can start an Intent like this:
File recordingFile = new File(recording.getFilePath());
Uri fileUri = Uri.fromFile(recordingFile);
Intent i = new Intent(Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.setType("audio/mp3");
i.putExtra(Intent.EXTRA_STREAM, fileUri);
try {
startActivity(Intent.createChooser(i, "Share " + recording.getName()));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "There are no app installed to share your audio file.", Toast.LENGTH_SHORT).show();
}

File sharing using Intent.ACTION_SEND gives access denied on BBM

I am trying to share an audio file saved by my application. The file is added to the media store so all apps can access it.
Intent mediaScannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri fileContentUri = Uri.fromFile(finalFile);
mediaScannerIntent.setData(fileContentUri);
this.sendBroadcast(mediaScannerIntent);
I use Intent.ACTION_SEND to share the files to other apps:
public void shareRecording(View view) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.setType("audio/mp3");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///" + recording.getFilePath()));
try {
startActivity(Intent.createChooser(i, "Share " + recording.getName()));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "There are no app installed to share your audio file.", Toast.LENGTH_SHORT).show();
}
}
All working good with all apps like GMail, Yahoo mail, Whatsapp, ...
except BBM, it gives access denied.
What do I need to do to make it work on BBM ?
Thanks for any help.
UPDATE:
I used
Uri fileUri = Uri.parse("content://" + recording.getFilePath());
instead of
Uri fileUri = Uri.parse("file:///" + recording.getFilePath());
and it worked on BBM but not othe apps
So what is the difference between havin the URI parsed with: "file:///" and "content://" ?
And how can I used this to get the share working for all apps ?
The solution is to use the actual file to initialize the Uri object.
File recordingFile = new File(recording.getFilePath());
Uri fileUri = Uri.fromFile(recordingFile);
This worked with all the apps that can share a file.

Try to share picture on google+ but i got "could not attach all media items to the post"

I want to share a picture on google, it should be easy but it is not :-(
File rootSdDirectory = Environment.getExternalStorageDirectory();
File pictureFile = new File(rootSdDirectory, "attachment.jpg");
Log.e(TAG , "pictureFile.absolutePath = " + pictureFile.getAbsolutePath());
Log.e(TAG , "pictureFile.length = " + pictureFile.length());
Uri pictureUri = Uri.fromFile(pictureFile);
Intent shareIntent = ShareCompat.IntentBuilder.from(getActivity())
.setText("Hello from Google+!").setType("image/jpeg")
.setStream(pictureUri).getIntent()
.setPackage("com.google.android.apps.plus");
startActivity(shareIntent);
The intent is started, the text is there, but not the picture. And a toast from android system appears saying : "could not attach all media items to the post"
When i search this error message on google i only found this :
http://code.google.com/p/android/issues/detail?id=31442
Is anyone has a solution ?
Thanks
The Google+ app only supports content:// URIs. You will need to use the MediaStore API for this purpose. An example could be found here.

How to read a pdf in android

I want to read a PDF file in android.
I placed my PDF files in the assets folder.
How can i read the PDF file from there?
PDF Reader Link
I have checked the above link but it does not work for me.
It gives me an error saying that the Activity was not found.
And I also want to open a PDF file in WebView. So is it possible to read PDF in WebView?
u can download PDFbox API to read PDF in android
try this link: http://pdfbox.apache.org/
You need to have a application which can support that mimetype and open it.
In your device/emulator that app is not installed so it is throwing error
Another great solution to read pdf using externally installed application like Adobe Reader.
private void openPDF(final String pathToPDF) {
File file = new File(pathToPDF);
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/pdf");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Application not found", Toast.LENGTH_SHORT).show();
}
}
Google cached displays formats like pdf, ppt, docx, etc. And you don't need to install any application at all.
Search the link in google and go to Cached.
private void openPDF(final String path) {
File file = new File(path);
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
} else {
uri = Uri.fromFile(file);
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Application not found", Toast.LENGTH_SHORT).show();
}
}

Categories

Resources