How open document pdf/docx with external app in Android - android

How open document pdf/docx with external app in Android

Try the below code :
File pdfFile = new File("File Path");
Intent openPdf = new Intent(Intent.ACTION_VIEW);
openPdf.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
fileUri = FileProvider.getUriForFile(viewContext,com.mydomain.fileprovider, pdfFile);
openPdf.setDataAndType(fileUri, "application/pdf");
openPdf.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(openPdf, "Select Application"));
Note : You need File Provider for granting URI permissions check this out

This will help you
//method to show file chooser
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Pdf"), PICK_PDF_REQUEST);
}
You will get the result in onActivity result
//handling the image chooser activity result
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PDF_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
}
}
For more info you can look into https://stackoverflow.com/a/11038348/11834065

Related

In Android 10, How to choose pdf file from android storage

I am using below code for choose pdf from Storage in Android 10.
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "Select Pdf"), PDF_REQ_CODE);
In startActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PDF_REQ_CODE && resultCode == RESULT_OK && data != null && data.getData() != null) {
uri = data.getData();
}
}
Uri returns like content://com.android.providers.media.documents/document/document:6865
But I want absolute path like storage/..../sample.pdf

Why it is not opening defined folder via intent in Android?

This code is redirecting to drive with open navigation but not opening actual given path
OLD Code
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/foldername/");
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setDataAndType(uri, "text/csv");
startActivity(intent);
The path value I am getting through uri is:
/storage/emulated/0/myfolder
NEW Code
Intent resultIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
resultIntent.addCategory(Intent.CATEGORY_OPENABLE);
Uri uri =
Uri.parse(Environment.getExternalStorageDirectory().getPath() +
"/myfolder/");
resultIntent.setType("*/*");
resultIntent.putExtra("android.provider.extra.INITIAL_URI", uri);
// show the entire internal storage tree
//resultIntent.putExtra("android.content.extra.SHOW_ADVANCED", true);
startActivity(resultIntent);
Let me know the issue where is the problem in this code.
private static final int READ_REQUEST_CODE = 10;
...
/**
* Fires an intent to spin up the "file chooser" UI and select an image.
*/
public void performFileSearch() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(uri, "text/*");
startActivityForResult(intent, READ_REQUEST_CODE);
}
After the user selects a document in the picker, onActivityResult()
gets called. The resultData parameter contains the URI that points to
the selected document. Extract the URI using getData(). When you have
it, you can use it to retrieve the document the user wants.
#Override
public void onActivityResult(int requestCode, int resultCode,
Intent resultData) {
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri uri = null;
if (resultData != null) {
uri = resultData.getData();
Log.i(TAG, "Uri: " + uri.toString());
showImage(uri);
}
}
}

Path of Image to be passed in Uri.parse to open an image using intent

I'm trying to open an image using intent.
The path of the image in my storage is /storage/emulated/0/nitp/download/logo.png.
My code is
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DEFAULT);
intent.setDataandType(Uri.parse("/storage/emulated/0/nitp/download/logo.png"),"image/*");
startActivity(intent);
I also tried putting file://storage/emulated/0/nitp/download/logo.png
and content://storage/emulated/0/nitp/download/logo.png
What is the path I should use?
Solved
Have to use file:///storage/emulated/0/nitp/downloads/logo.png
For Pick image from media Storage or SD Card:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
And For get the path of Image and set in ImageView:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
try {
//Getting the Bitmap from Gallery
imgpath= MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting the Bitmap to ImageView
imageViewProfileImage.setImageBitmap(imgpath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Hope this work.

Pick a file from an SD card using Intent

Does Android have a way to browse and pick any file from an SD card using intents?
Something like:
String uri = (Environment.getExternalStorageDirectory()).getAbsolutePath();
Intent i = new Intent(Intent.ACTION_PICK, Uri.parse(uri));
I am trying to send a file to other devices using Bluetooth. I can send if I give the full path of the file name in my code. I want my users to pick the file that should be send.
You can use the following code:
Intent mediaIntent = new Intent(Intent.ACTION_GET_CONTENT);
mediaIntent.setType("video/*"); // Set MIME type as per requirement
startActivityForResult(mediaIntent,REQUESTCODE_PICK_VIDEO);
Then you can get the path in onActivityResult:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUESTCODE_PICK_VIDEO
&& resultCode == Activity.RESULT_OK) {
Uri videoUri = data.getData();
Log.d("", "Video URI= " + videoUri);
}
}
For general browsing use this (e.g. Music file):
Intent intent = new Intent();
intent.setType("*/*");
if (Build.VERSION.SDK_INT < 19) {
intent.setAction(Intent.ACTION_GET_CONTENT);
intent = Intent.createChooser(intent, "Select file");
} else {
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
String[] mimetypes = { "audio/*", "video/*" };
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
}
startActivityForResult(intent, Constants.REQUEST_BROWSE);
And receive the browsed data here:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.REQUEST_BROWSE
&& resultCode == Activity.RESULT_OK && data != null) {
Uri uri = data.getData();
if (uri != null) {
// TODO: handle your case
}
}
}

Read/Write Image from local File

How do I read and write image to/from a local file in android?
I want to take an image from a file, save it in memory and then read it from file.
I am using this code to write image in file:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String fileName=GetFileName().replace('.', ' ')+".png";
File out = new File(Environment.getExternalStorageDirectory()+File.separator +fileName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
startActivityForResult(intent, REQUEST_IMAGE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
wb.loadUrl(sendUrl);
if(requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
String imgPath= data.getExtras().get("Uri").toString();
bm = BitmapFactory .decodeFile(imgPath);
}
}
How can I read the image back?
Try this---
Uri mUriSavedImage;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String fileName=GetFileName().replace('.', ' ')+".png";
File out = new File(Environment.getExternalStorageDirectory()+File.separator +fileName);
mUriSavedImage= Uri.fromFile(out);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mUriSavedImage);
startActivityForResult(intent, REQUEST_IMAGE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
wb.loadUrl(sendUrl);
if(requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
String imgPath= mUriSavedImage.getPath();
bm = BitmapFactory.decodeFile(imgPath);
}
}

Categories

Resources