open directory (folder) in android - android

What is the code to open the folder, /storge/sdcar0/0students/ to be like a screen in the picture below
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ "/0students/");
intent.setDataAndType(uri, "*/*");
startActivity(Intent.createChooser(intent, "Open folder"));
I have used this code, but is not required
enter link description here

To open the directory 0students, try this.
Uri uri = Uri.parse(Environment.getExternalStorageDirectory() + "/0students/");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "resource/folder");
if (intent.resolveActivity(getPackageManager()) != null){
startActivity(intent);
}

Related

android open pdf from internal storage

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)

When I open .doc file giving me error like could not open this document this file isn't available offline

Uri uri = Uri.fromFile(file); Intent intent = new
Intent(Intent.ACTION_VIEW);
if (file.toString().contains(".doc") ||
file.toString().contains(".docx")) {
intent.setDataAndType(uri, "application/msword");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(intent); }

Unsupported attachment

private void sendPdf(String file) {
// Uri uri = Uri.fromFile(new File(file));
Uri uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", new File(String.valueOf(file)));
intent = new Intent(Intent.ACTION_SEND);
// intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION & Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// intent.putExtra(Intent.EXTRA_SUBJECT, "Transaction history");
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_STREAM, uri); // invitation body
Intent chooser = Intent.createChooser(intent, "Share with: ");
List<ResolveInfo> resInfoList = getActivity().getPackageManager().queryIntentActivities(chooser, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
getActivity().grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
startActivity(chooser);
}
I trying to share pdf using WhatsApp/Telegram but when I try to share the file, but it say unsupported attachment(Telegram). Can anyone help me?
EDIT: I got this issue only for Android 10. Anyone facing same problem?
I use this piece of code for my own application.
Intent mIntent = new Intent(Intent.ACTION_SEND);
File mFile = new File(mPath);
if(mFile.exists()) {
mIntent.setType("application/pdf");
mIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + mPath));
mIntent.putExtra(Intent.EXTRA_SUBJECT,
"Sharing File...");
mIntent.putExtra(Intent.EXTRA_TEXT, "Sharing File...");
startActivity(Intent.createChooser(mIntent, "Share My File"));
}
It worked for me, try it. Good luck
File outputFile = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_DOWNLOADS), "path_of_file.pdf");
Uri uri = Uri.fromFile(outputFile);
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType("application/pdf");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setPackage("org.telegram.messenger");
activity.startActivity(share);
Add android:requestLegacyExternalStorage="true" at manifest file.

How to open a folder create within application in android

I am generated a folder name "tesApplication" and create a file name "testFile.csc" in it and store some date into that file. Now I want to open folder dir using new intent.
I had try below code but did not work in my case any other way to achieve this.
Uri selectedUri = Uri.parse(fileNameString);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, "text/csv");
if (intent.resolveActivityInfo(getPackageManager(), 0) != null)
{
startActivity(intent);
}
else
{
// if you reach this place, it means there is no any file
// explorer app installed on your device
}
copy this in else
public void openFolder()
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ "/myFolder/");
intent.setDataAndType(uri, "text/csv");
startActivity(Intent.createChooser(intent, "Open folder"));
}

Open specific folder using Intent

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)

Categories

Resources