I appreciate your help so that my code can open a PDF file with an Intent, this is the code i have been using:
String pathFile =
Environment.getExternalStorageDirectory().toString() +
FileUtils.PATH_SEPARATOR + "myFolder" +
FileUtils.PATH_SEPARATOR + "myFile.pdf";
File pdfFile = new File( pathFile );
if ( pdfFile.exists() ) {
Uri uri = Uri.fromFile( new File( pathFile ) );
Intent intent = new Intent( Intent.ACTION_VIEW );
intent.setDataAndType(uri, "application/pdf");
intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity( intent );
}
But I understand that now (from the Android-N version) the privileges are handled differently and so i hope you can help me by indicating what changes i have to make to get a PDF file opened using an Intent.
PS: In my AndroidManifest.xml I am looking for all permissions in the STORAGE:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
The answer was found at: http://android.2317887.n4.nabble.com/FileProvider-td394588.html
Terrific!.
Try this code.
Intent intent = new Intent(Intent.ACTION_VIEW);
try {
String newFilePath = filePath.replaceAll("%20", " ");
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
intent.setDataAndType(Uri.parse(newFilePath), "application/pdf");
} else {
Uri uri = Uri.parse(newFilePath);
File file = new File(uri.getPath());
if (file.exists()){
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
intent.setDataAndType(uri, "application/pdf");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
Related
I have a pdf in the app's internal storage, however, I cannot open the file in the system pdf app.
val pdfDirPath = File(context.filesDir, "pdfs")
val file: File = File(pdfDirPath, "title.pdf")
val uri = Uri.fromFile(file)
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(uri, "application/pdf");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
it shows the error "cannot display pdf"
Try this
Uri pdfUri = FileProvider.getUriForFile(getContext(), getContext().getApplicationContext().getPackageName() + ".provider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(pdfUri, "application/pdf");
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/Download/NSuresh.docx" ); uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", file); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.setDataAndType(uri, "application/msword"); startActivity(intent);
I am trying to add option to update app by downloading apk, its getting installed successfully. But the problem is, app got close after installation, without prompting Open/Done screen in Nougat and above devices.
I have tried using both ACTION_VIEW and ACTION_INSTALL_PACKAGE, but no luck. Also tried with startActivityForResult instead of startActivity, still no luck.
public void installAPK() {
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
File file = new File(path + "update.apk");
if (Build.VERSION.SDK_INT < 24) {
intent.setDataAndType(Uri.fromFile(new File(path + "update.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
} else {
Uri apkURI = FileProvider.getUriForFile(
activity,
activity.getApplicationContext()
.getPackageName() + ".provider", file);
intent.setDataAndType(apkURI, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
activity.startActivityForResult(intent, RC_INSTALL_APK);
}
Anything I missed to do?
Here's sample code of my application method to open new version
void OpenNewVersion(String location) {
Intent downloadIntent;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String PATH = Environment.getExternalStorageDirectory() + "/Download/";
File fileLocation = new File(PATH, "app-stock.apk");
Uri apkUri = FileProvider.getUriForFile(this, "Adapters.GenericFileProvider", fileLocation);
downloadIntent = new Intent(Intent.ACTION_VIEW);
downloadIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
downloadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
downloadIntent.setDataAndType(apkUri, "application/vnd.android.package-archive");
List<ResolveInfo> resInfoList = this.getPackageManager().queryIntentActivities(downloadIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
this.grantUriPermission(packageName, apkUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
} else {
File fileLocation = new File(this.getFilesDir(), "app-stock.apk");
downloadIntent = new Intent(Intent.ACTION_VIEW);
downloadIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
downloadIntent.setDataAndType(Uri.fromFile(fileLocation), "application/vnd.android.package-archive");
}
this.startActivity(downloadIntent);
}
I want to open specific path folder using Intent, I used code for File explorer and It working perfectly but in some device (samsung devices) if file explorer app are not available then It not opening folder of specific path.
I tried many solutions but It won't work for me.
Uri uri = Uri.fromFile(new File(new File(filePath).getParent()));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "resource/folder");
if (intent.resolveActivityInfo(getPackageManager(), 0) != null)
{
startActivity(intent);
}
else {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setDataAndType(uri, "file/*");
startActivity(Intent.createChooser(intent, "Open folder"));
}
Here is my solution for accessing "root/Downloads/Sam" in Kotlin
val rootPath = "content://com.android.externalstorage.documents/document/primary:"
val samPath = Uri.parse("$rootPath${Environment.DIRECTORY_DOWNLOADS}%2fSam") //"%2f" represents "/"
val REQUEST_CODE_PICK_FILE = 1
main_sam_button.setOnClickListener {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "text/plain" //specify file type
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, samPath) //set the default folder
startActivityForResult(intent, REQUEST_CODE_PICK_FILE)
}
Demo:
https://youtu.be/lUIPyC_8q_M
Helpful reading:
"content://com.android.externalstorage.documents/document/primary:":
What is com.android.externalstorage?
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.setDataAndType(
Uri.parse(
(Environment.getExternalStorageDirectory().path
+ java.io.File.separator) + "myFile" + java.io.File.separator
), "*/*"
)
intent.addFlags(
Intent.FLAG_GRANT_READ_URI_PERMISSION
and Intent.FLAG_GRANT_WRITE_URI_PERMISSION
and Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
and Intent.FLAG_GRANT_PREFIX_URI_PERMISSION
)
intent.addCategory(Intent.CATEGORY_OPENABLE)
startActivityForResult(intent, WRITE_ACCESS_CODE)
What i am trying is to share a file over bluetooth. I have tried below two methods to pass the file name to the ACTION_SEND intend. share activity is pop'ing up and when i touch the connected bluetooth device, i get a toast saying Bluetooth share: File Unknown file not sent. Both the method fails.
public void pushFileOverOpp(String filename) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setPackage("com.android.bluetooth");
intent.setType("audio/mp3");
File f = new File(Environment.getExternalStorageDirectory(), "images");
File sample = new File(f, "sample.mp3");
Uri u = Uri.parse(sample.toString());
intent.putExtra(Intent.EXTRA_STREAM, u);
mContext.startActivity(intent);
}
Error , Log-
OppService: URI : /storage/emulated/0/images/sample.mp3
OppService: HINT : null
OppService: FILENAME: null
OppService: MIMETYPE: audio/mp3
File f = new File(mContext.getFilesDir(), "images");
File sample = new File(f, "sample.mp3");
Uri u = FileProvider.getUriForFile(mContext,
BuildConfig.APPLICATION_ID + ".provider", sample);
intent.putExtra(Intent.EXTRA_STREAM, u);
Error, Log-
OppService: URI : content://com.example.com.test.provider/tester/images/sample.mp3
OppService: HINT : null
OppService: FILENAME: null
I have checked the android source code, This error comes when filename is null. Log also says filename is null. But i could not figure out the exact reason. Could someone Please help me out here, what is wrong with my code.
After some study i understood the problem. There were two issues-
xml tag for external storage(/sdcard/) directory was wrong in xml file.
I changed as below.
<root-path
name="root"
path="/" />
URI permission was not granted
mContext.grantUriPermission("com.android.bluetooth", u,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
After modifying with above lines of code, File share is working !
full working code-
public boolean pushFileOverOpp(String filename) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("*/*"); // supports all mime types
intent.setPackage("com.android.bluetooth"); //bluetooth package name, default opp
File folder = new File(Environment.getExternalStorageDirectory(), "images");
File file = new File(folder, filename);
if (!file.exists()) {
Logger.e("No such file " + filename + " exists!");
return false;
}
Uri u = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".provider", file);
intent.putExtra(Intent.EXTRA_STREAM, u);
mContext.grantUriPermission("com.android.bluetooth", u,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
Logger.d("Sharing file over bluetooth " + folder.toString());
mContext.startActivity(intent);
return true;
}
Thanks.
Please refer this code , it works and share the files using createChooser method.
ArrayList<Uri> arrayList2 = new ArrayList<>();
String MEDIA_PATH = new String(Environment.getExternalStorageDirectory() +
"/NewCallLogs/audio.mp3");
File files = new File(MEDIA_PATH);
Uri u = Uri.fromFile(files);
arrayList2.add(u);
Intent share = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
share.setData(Uri.parse("mailto:"));
share.setType("audio/mpeg");
share.putExtra(android.content.Intent.EXTRA_STREAM, arrayList2);
try {
startActivity(Intent.createChooser(share, "Share..."));
// getActivity().finish();
Log.i("Finished sharing.", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getActivity(), "nothing shared.", Toast.LENGTH_SHORT).show();
}
For sharing the file only in bluetooth
ArrayList<Uri> arrayList2 = new ArrayList<>();
String MEDIA_PATH = new String(Environment.getExternalStorageDirectory() +
"/NewCallLogs/audio.mp3" );
File files = new File(MEDIA_PATH);
Uri u = Uri.fromFile(files);
arrayList2.add(u);
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setData(Uri.parse("mailto:"));
share.setType("audio/mpeg");
share.setPackage("com.android.bluetooth");
share.putExtra(android.content.Intent.EXTRA_STREAM, arrayList2);
startActivity(share);