Open PDF from Cloud android - android

I want to open a PDF File uploaded in Google Drive on click of an Imageview.
As soon as I click, the PDF should get downloaded and ask the user to open a related PDF Viewer
Now the problem is, when I click, It asks the user to open in Drive or Chrome. But I want the options should be for PDF Applications like Adobe reader, polaris office etc.
Here is My Code :
u1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String un1 = "https://drive.google.com/open?id=0B_i97Zc2yxeSNFJMVXFGQmlXYzQ&authuser=0";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(un1));
startActivity(i);
}
});

Replace i.setData(Uri.parse(un1)); with i.setDataAndType(uri, "application/pdf");, where uri is a Uri pointing to your downloaded copy of the PDF file. For example, if you downloaded the PDF file to external storage, you could use Uri.fromFile() to get a Uri pointing to the same file as does a File object.

Related

Android open PDF from URI starting content

I am trying to open up PDF using intent that takes the following:
Here is the code that I have:
Intent intent = new Intent(Intent.ActionVIEW)
intent.setDataAndType(Uri.parse("CONTENTURI + FILENAME","application/pdf"
try { startActivity(intent)} catch excpetion and so on.
It pops up with whatever applications I have installed, from Adobe Reader, Google PDF reader, POLARIS(as I am using Galaxy Tab 3 for testing), and none of those work. Each say unsupported document.
Does download and show the right solution in this case, or any ideas? Thanks in advance!
Following code triggers the VIEW action for pdf files (open the default PDF viewer, choose between applications capable to view PDFs or get an error if you don't have any installed).
File file = new File("your pdf path here");
if (file.exists()) {
Uri pdfPath = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(pdfPath, "application/pdf");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
//if user doesn't have pdf reader instructing to download a pdf reader
}
}
NOTE: The PDF should not be saved local to application, or else the third party app won't have access. You should use media location.

How to open a pdf file given a url in android using third party application?

I have a number of urls that points to pdf files. I would like to view these in my application using the third party application. I have "Drive PDF Viewer". I am using the code below. When I press on a button on my app, that has a setOnCLickListener, the pdf file is downloaded, and instead of allowing me to choose the third party application that i would like to use to open my downloaded pdf, a web page is presented (yahoo website). However, if i go to my notification drawer and click on the downloaded file, only then does the option to select "Drive PDF Viewer" is presented. My question is, how do i go straight to choosing which third party pdf viewer i would like to use without having this webpage presented or having to access the notification drawer? I have no idea where this webpage comes from.
pdfButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
/*Display pdf here*/
Log.e("ArticleFragment", "pdf Button Pressed! url: " + pdfUrl);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdfUrl));
startActivity(browserIntent);
}
});
You should download the remote file yourself, storing it in a local cache directory, then use the same code but with the local URI.

Facebook Upload of photos fails from Android Chooser

In my app, I have an option to share photos using standard Android sharing mechanism. When I choose gmail or yahoo mail, the sharing works, the images are displayed in the body of an email and are sent to the destination. When I choose Facebook, the images are displayed inside the Facebook activity where you can add a message, but when I hit the Done button, the upload process starts, but always fails.
The title of the message box is: "Facebook Upload Failed" and the message itself is: "Your upload could not be completed because we could not access your photo(s)."
Here is the code snippet for sharing:
mShareIntent = new Intent();
mShareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
mShareIntent.setType("image/jpeg");
mShareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
ArrayList<Uri> uris = new ArrayList<Uri>();
for(int i = 0; i < mImageUrls.size(); i++) {
uris.add(Uri.parse("file://"+mShareFiles[i].getAbsolutePath()));
}
mShareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(mShareIntent, "Share Images To..."))
The photos are obviously accessible, since they are successfully processed by gmail and yahoo mail. And Facebook, itself, can access them, but not sent them.
As an experiment, I shared photos from the Android Gallery, in the Chooser, I chose Facebook, and the photos were successfully uploaded.
Does anyone have any idea what might be going on?
Thank You,
Gary
If the file is in your app's private directory (or even in its sd card directory in KitKat) then you might run into sharing issues. And FileProvider has its problems too.
The only reliable way I've found to share image files is to copy them to the external storage directory for pictures and sharing from there (you can create a .nomedia subdirectory to avoid them appearing in the Gallery app).
For example:
Bitmap bitmapToShare = ...
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
File pictureStorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File noMedia = new File(pictureStorage, ".nomedia");
if (!noMedia.exists())
noMedia.mkdirs();
File file = new File(noMedia, "shared_image.png");
saveBitmapAsFile(bitmapToShare, file);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
shareIntent.setType("image/png");
startActivity(shareIntent);
}

Pdf files as part of Android .apk

I have to build an Android application which shows a list of pdf files. These pdf files should be secured, in other words - the user of the app should not be able to get a copy of the pdf content by any means (copy/cut/print...etc). My questions now are
How should I ship the content of the pdf file along with .apk file.
If we send the content of the file in a diff format (raw/byte code), how should I convert the file back to pdf and where should I place the file on the installed machine such that it is secure.
FYI, I will be locking down current version of Adobe Redaer as the pdf viewer to view the pdf files as it doesn't allow copy/paste/print.
This is the first Android app that I am developing. So, I'm a kind of Android newbie. Any help would be greatly appreciated.
Thanks,
Navin
Probably you should store it in the res/raw folder
You can copy this(these) pdf file(s) into assets folder, thus your files will be the part of your application and can not be accessible outside, and you can also treat them as normal files (i.e. open with any pdf viewer in your case). Whenever you want to get access to context file you can call context.getAssets() .
Hope this information helps.
Update : Here is the code I am using to open a file in downloads directory, this code can open the pdf file in any compatible pdf reader if installed on phone, Tested with Adobe Reader. You will need a URI of the pdf file in assets folder to run the code.
String fileName = pdfUrl.substring(pdfUrl.lastIndexOf("/"));
Log.i("fileName", "" + fileName);
File file = new File("/sdcard/download" + fileName);
// File file = new
// File("/sdcard/download/airtel_latest000.mp3");
Intent intent;
if (file.exists()) {
Uri path = Uri.fromFile(file);
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
downloaded = true;
} else {
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(pdfUrl));
downloaded = false;
}
try {
startActivityForResult(intent, 5);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}

Is there a way to read local pdf file using google docs

I saw a way of reading online pdf files using google docs ...
Android - Load PDF / PDF Viewer
Is there a way we can use it to view local files stored in sd card
You can launch an intent that will allow the user to choose what app will open the PDF with the following code, which will work for any file and mimetype. If the user doesn't have an app that can open it, you can display an error or do whatever else you need to do.
Note that the file must be world-readable, so it must be marked as such if it is on Internal storage, or it must be in external storage.
private void openFile(File f, String mimeType)
{
Intent viewIntent = new Intent();
viewIntent.setAction(Intent.ACTION_VIEW);
viewIntent.setDataAndType(Uri.fromFile(file), mimeType);
// using the packagemanager to query is faster than trying startActivity
// and catching the activity not found exception, which causes a stack unwind.
List<ResolveInfo> resolved = getPackageManager().queryIntentActivities(viewIntent, 0);
if(resolved != null && resolved.size() > 0)
{
startActivity(viewIntent);
}
else
{
// notify the user they can't open it.
}
}

Categories

Resources