start directory of a file explorer Intent for Android - android

What is the correct way to start a File Explorer by Intent in a specified directory?
Following code snippet works fine, except that it starts in the wrong directory.
The desired starting point would be at "selectedUri"
val selectedUri = Uri.parse(externalStorage.toString() + "DCIM/Camera/")
val intent= Intent(Intent.ACTION_GET_CONTENT).apply{
addCategory(Intent.CATEGORY_OPENABLE)
type = "image/*"
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}

you forgot to set the intent data
val selectedUri = Uri.parse(externalStorage.toString() + "DCIM/Camera/")
val intent= Intent(Intent.ACTION_GET_CONTENT).apply{
addCategory(Intent.CATEGORY_OPENABLE)
data = selectedUri
type = "image/*"
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}

Related

how can i share a plain text and another file like excel file through one single intent

I tried this but it only sends excel file not text
I tried to send text and file in two intents and it works but in one intent no
val file = File(
Environment.getExternalStorageDirectory() ,
"newfolder/$name.xlsx"
val uri = FileProvider.getUriForFile(context , "com.example.android.fogara2" , file)
val sendIntent : Intent = Intent(Intent.ACTION_SEND).apply {
type="text/plain"
`package` = "com.whatsapp"
putExtra(Intent.EXTRA_TEXT,"sometext")
putExtra(Intent.EXTRA_STREAM , uri)
type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
context.startActivity(sendIntent)

How to open a uri in file manager in Android

I'm trying to make a button in my app when user click on it, it'll open a specific URI in file manager but the best I could've done is that the button opens recent tab in default file manager.
Please, if it's possible, suggest me a code which opens a chooser for user to choose between his file manager applications and when user chose, that file manager opens in specific URI that I defined in my code.
Here is my code:
val intent = Intent(Intent.ACTION_GET_CONTENT)
val uri = Uri.parse(
//my path
)
intent.data = uri
intent.type = "*/*"
startActivity(Intent.createChooser(intent, "Open folder"))
Also one of the users suggested me to use INITIAL_URI I've did it like this but didn't work :
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
val uri = Uri.parse(
//my path
)
intent.data = uri
intent.type = "*/*"
intent.putExtra("android.provider.extra.INITIAL_URI", uri)
intent.putExtra("android.content.extra.SHOW_ADVANCED", true)
startActivity(Intent.createChooser(intent, "Open folder"))
suggest me a code which opens a chooser for user to choose between his file manager applications and when user chose, that file manager opens in specific URI that I defined in my code
That has never been a pattern in Android app development. There is no standard Intent action for what you seek that is likely to be implemented by much of anything, let alone a significant number of file manager apps.
fun openNewTabWindow(urls: String, context : Context) {
val uris = Uri.parse(urls)
val intents = Intent(Intent.ACTION_VIEW, uris)
val myV = Bundle()
myV.putBoolean("new_window", true)
intents.putExtras(myV)
context.startActivity(intents)
}

Android Samsung: how to intent samsung file explorer

I would like to get a nicer file explorer as the one which is chosen automatically by Intent. On my phone there is also the file explorer from Samsung. However it never shows up.
Here my code snippet in Kotlin
val intent = Intent(Intent.ACTION_GET_CONTENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
data = selectedUri
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, REQUEST_CODE);
For Samsung Users
val intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
intent.addCategory(Intent.CATEGORY_DEFAULT);
For more refernce check http://developer.android.com/guide/topics/providers/document-provider.html

Android doesn't find Intent for opening a PNG File

So i try to open a PNG File on the system using the Gallery, but for some reason I always get an ActivityNotFoundException saying the following :
No Activity found to handle Intent { act=android.intent.action.VIEW dat=context://storage/emulated/0/Android/data/com.myapp/files/MyApp/1533800534172image001.png typ=image/* }
My code looks like this :
val intent = Intent()
intent.action = Intent.ACTION_VIEW
intent.setDataAndType(uri,"image/*")
context.startActivity(intent)
I think the problem is on your variable uri.
it should be like:
uri = Uri.parse("file://" + "/sdcard/path-to-your-file.png")

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