Unable to open PDF file through intent programmatically in android - android

I have an app where on button click, access should be provided to browse and open the PDF files stored in the internal memory of the device.I tried various codes but i get an toast saying "Cannot display PDF". I don't get any errors in logs. I use android version 7.0 device. Attached my code below: please help:
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
I have included this in Oncreate:
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
This is how called the method:
linear_pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pdf.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));
openPdf(getContext(), Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
communication.dismiss();
}
});

Try this one...
public void openPdf(Context context, String path){
File file = new File(path);
if (file.exists()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
PackageManager pm = context.getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setType("application/pdf");
Intent openInChooser = Intent.createChooser(intent, "Choose");
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
if (resInfo.size() > 0) {
try {
context.startActivity(openInChooser);
} catch (Throwable throwable) {
Toast.makeText(context, "PDF apps are not installed", Toast.LENGTH_SHORT).show();
// PDF apps are not installed
}
} else {
Toast.makeText(context, "PDF apps are not installed", Toast.LENGTH_SHORT).show();
}
}
}
Call this method:
openPdf(getApplicationContext(), Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");

For opening one specific file:
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/your_directory/" + "your_file_name" + ".pdf");
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try{
startActivity(pdfIntent);
}catch(ActivityNotFoundException e){
Toast.makeText(getContext(), "No required app", Toast.LENGTH_SHORT).show();
}
Check directory, this one works.

For opening pdf file from local storage add prefix in url.
private void openPdf(File file) {
try {
String url = file.getAbsolutePath();
String u = "file:///" + url;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(u));
startActivity(i);
} catch (Exception e) {
e.printStackTrace();
}
}

Related

Android open XLSX file

I'm trying to open an XLSX file within my Android App.
I'm aware that the Intent type I have to fire is application/excel, but despite I've installed Google Sheets, my code says that no application can open my excel file.
This is the code I use to fire the Intent:
private void openXLS(){
File xls = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "prova.xlsx");
Uri path = Uri.fromFile(xls);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/excel");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "No Application available to view XLS", Toast.LENGTH_SHORT).show();
}
}
Note: prova.xlsx exists, and I'm able to reach it and open it.
SOLVED
Using the MIME type application/vnd.ms-excel, *.xls and *.xlsx files can be opened.
private void openXLS(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/vnd.ms-excel");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Application not found", Toast.LENGTH_SHORT).show();
}
}

add image to twitter share intent android

I'm trying to add an image to my twitter share intent. I save an image locally in one class and then in another I get the image and try to attach to my intent.
Here is my code
private void shareTwitter(){
try {
FileInputStream fis;
fis = getActivity().openFileInput("photo.jpg");
Bitmap shot = BitmapFactory.decodeStream(fis);
File file = new File(MapView.path, "snapshot.jpg");
if(file.exists()){
Log.i("FILE", "YES");
}else{
Log.i("FILE", "NO");
}
Uri uri = Uri.parse(file.getAbsolutePath());
//Uri uri = Uri.parse("android.resource://com.gobaby.app/drawable/back");
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("/*");
intent.setClassName("com.twitter.android", "com.twitter.android.PostActivity");
intent.putExtra(Intent.EXTRA_TEXT, "Thiws is a share message");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(intent);
} catch (final ActivityNotFoundException e) {
Toast.makeText(getActivity(), "You don't seem to have twitter installed on this device", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
At the moment there is no exception in my logcat my app just displays a toast saying image failed to load.
Please what an I doing wrong?
This is what you need
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file);
This might be helpful for somebody:
private void sendShareTwit() {
try {
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
String filename = "twitter_image.jpg";
File imageFile = new File(Environment.getExternalStorageDirectory(), filename);
tweetIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.twitter_share_text));
tweetIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
tweetIntent.setType("image/jpeg");
PackageManager pm = getActivity().getPackageManager();
List<ResolveInfo> lract = pm.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
boolean resolved = false;
for (ResolveInfo ri : lract) {
if (ri.activityInfo.name.contains("twitter")) {
tweetIntent.setClassName(ri.activityInfo.packageName,
ri.activityInfo.name);
resolved = true;
break;
}
}
startActivity(resolved ?
tweetIntent :
Intent.createChooser(tweetIntent, "Choose one"));
} catch (final ActivityNotFoundException e) {
Toast.makeText(getActivity(), "You don't seem to have twitter installed on this device", Toast.LENGTH_SHORT).show();
}
}
Here is solution:
private fun shareOnTwitter() {
val file = File(context!!.filesDir, FILENAME_SHARE_ON_TWITTER)
val uriForFile = FileProvider.getUriForFile(context!!, com.yourpackage.activity.YourActivity, file)
val intent = Intent(Intent.ACTION_SEND).apply {
type = "image/jpeg"
putExtra(Intent.EXTRA_STREAM, uriForFile)
}
startActivity(intent)
}

Unable to open pdf client on button click

Here is my button click code:
private View.OnClickListener onButton1=new View.OnClickListener() {
public void onClick(View v){
copyAssets();
File rootDir = Environment.getExternalStorageDirectory();
File file = new File(rootDir + "/sdcard/" +"save.pdf");
if(file.exists())
{
// do action here
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
intent.setType("application/pdf");
startActivity(intent);
}
else {
Toast.makeText(getApplicationContext(), "File does not exist", Toast.LENGTH_LONG).show();
}
}
};
copyAssets() is a function by which I copy the file from my assets folder to my SD-Card and it is working as I checked on my device, but I am unable to open it when I click the button. My file is named as save.pdf on the SD-Card
you can read pdf file from SDCard by giving the path as follows:
File pdfFile = new File(path);
if(pdfFile.exists())
{
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(pdfIntent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(uractivity.this, "File does not exist", Toast.LENGTH_LONG).show();
}
}

How PDf file can be read through android via coding

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button OpenPDF = (Button) findViewById(R.id.button);
OpenPDF.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
File pdfFile = new File("res/raw/comic.pdf");
if(pdfFile.exists())
{
//Uri path = Uri.fromFile(pdfFile);
Uri path = Uri.parse("android.resource://" + getPackageName() + "/ R.raw.comic.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
// Intent pdfIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://package.name/" + pdfFile));
// pdfIntent.setDataAndType(path, "/assets/arduino-comic-latest.pdf");
// pdfIntent.setType("application/pdf");
// pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(intent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(MainActivity.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
}
}
});
I have done this code but this is not find the path of the file.what I have to do??can Anyone help me
Thankyou
you cant do that directly, you have to use Native Library for reading PDF... library may be iText, jPedal, Mupdf, Pdfbox... etc. Google it

How to Read PDF in Android?

How to Read PDF in Android stored in SDCard ??
Here's some code showing how to open a pdf file to read:
private void openBook() {
File file = new File(mRealPath);
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, getString(R.string.application_type));
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(FirstTab.this,
getString(R.string.no_application_found),
Toast.LENGTH_SHORT).show();
}
}

Categories

Resources