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)
}
Related
My app can share internally stored files with the androidx.core.content.FileProvider.
The intent and the chooser are created with the following snippet:
val shareIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(
Intent.EXTRA_STREAM,
FileProvider.getUriForFile(this, authority, file, displayName),
)
type = document.mimeType.mediaType
}
startActivity(Intent.createChooser(shareIntent, getString(R.string.share_title)))
I explicitly pass the displayName to the FileProvider, but in the app chooser, I see the file's local name (see image).
Is there a way to show the correct display name in the chooser?
I have a broadcast receiver for android that is supposed to open an intent to a pdf i am downloading but when it opens up the intent it opens a blank pdf, when i debug it it's giving me content://downloads/all_downloads/431 as the location instead of the file, the 431 is the dowload number and increases each time i download the pdf, my receiver looks like this.
override fun onReceive(context: Context?, intent: Intent) {
val action = intent.action
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
val manager = context!!.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val completeDownloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
var uri: Uri? = manager.getUriForDownloadedFile(completeDownloadId)
Log.d("BROADCASTEND", uri.toString())
val intent = Intent(Intent.ACTION_VIEW)
intent.data = uri
MApplication.currentActivity.startActivity(intent)
}
}
does anyone know why it opens a blank pdf and references the number instead of the file? i can see that it is downloading the pdf.
Since Android introduced major changes in storage framework recently much of the documentation talks about permissions and scoped storage. But I couldn't find details on how to process Uri of a file, for it to be readable by other apps.
The intent action to view/read a file by other apps fail. I don't understand what's the problem here;
Does it have to do with difference between java.io.File and java.nio.File?
The Uri has missing permissions or the Uri is not well formatted.
The Android storage samples (FileManager) has this bug as well. It lists all the files in a directory successfully but can't open a selected image, or a document. I've reported this issue but no help so far.
Following snippet is from FileManager (storage-samples)
fun openFile(activity: AppCompatActivity, selectedItem: File) {
// Get URI and MIME type of file
val uri = Uri.fromFile(selectedItem).normalizeScheme()
val mime: String = getMimeType(uri.toString())
// Open file with user selected app
val intent = Intent()
intent.action = Intent.ACTION_VIEW
intent.data = uri
intent.type = mime
return activity.startActivity(intent)
}
After the hints from the comments, I found the answer in developer docs.
Caution: If you want to set both the URI and MIME type, don't call setData() and setType() because they each nullify the value of the other. Always use setDataAndType() to set both URI and MIME type.
The reason behind openFile didn't throw FileUriExposedException in android-storage-samples is that after setting intent.type, the Uri gets nullified and when I changed it to setDataAndType() I got the exception. The final snippet looks like
fun openFile(activity: AppCompatActivity, selectedItem: File) {
// Get URI and MIME type of file
val uri = FileProvider.getUriForFile(activity.applicationContext, AUTHORITY, selectedItem)
// val uri = Uri.fromFile(selectedItem).normalizeScheme()
val mime: String = getMimeType(uri.toString())
// Open file with user selected app
val intent = Intent()
intent.action = Intent.ACTION_VIEW
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
// intent.data = uri
// intent.type = mime
intent.setDataAndType(uri, mime)
return activity.startActivity(intent)
}
I think they forgot to update the samples over time, let me create a pull request to commit this change over there as well.
I am making a camera app which takes both front camera and back camera images/video and I do not want individual thumbnails on the Camera preview for each file.
I want to open "/storage/emulated/0/DCIM/Camera" folder using the Files app and further open the photo/video which is not possible with ACTION_GET_CONTENT as it selects the image and exits the Files app as tried here -
val intent = Intent(Intent.ACTION_GET_CONTENT)
val uri: Uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).path.toString() + "/Camera")
intent.setDataAndType(uri, "*/*")
startActivity(Intent.createChooser(intent, "Open folder"))
I tried ACTION_VIEW too, but it is not specific to one folder and opens the gallery showing all media as tried here -
val intent = Intent()
intent.action = Intent.ACTION_VIEW
intent.type = "image/*"
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
"image/*" shows images and videos too in the gallery for me which is good. When "*/*" is used we can use the Files app too but it opens the downloads folder.
One solution I found works only with ES Explorer as tried here -
val uri: Uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).path.toString() + "/Camera")
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(uri, "resource/folder")
startActivity(intent)
This is due to "resource/folder" not supported leading to a crash. Changing "resource/folder" to "*/*" makes Files app open the downloads folder and Photos app to hang.
It seems gallery can do it via buckets, but it too is not universal.
I am not asking for much, just to display my Camera folder from where I can open and view any photo/video.
It was not possible if the user had not once selected a file using ACTION_GET_CONTENT was the opinion.
But now... try this code:
String scheme = "content://com.android.externalstorage.documents/document/primary%3APictures";
// String scheme = "content://com.android.externalstorage.documents/document/primary%3ADCIM%2FCamera";
Uri uri = Uri.parse(scheme);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uri); // Added in API level 26
startActivityForResult(intent, 12345);
Toast.makeText(context, "Picker opened in:\n\n" + uri.toString(), Toast.LENGTH_LONG).show();
It works here and i'm pretty amazed.
To open the Files app starting with a certain folder you can use below code but only for Android 11 devices sadly.
//String scheme = "content://com.android.externalstorage.documents/document/primary%3APictures";
//String scheme = "content://com.android.externalstorage.documents/document/primary%3ADownload";
//String scheme = "content://com.android.externalstorage.documents/document/10F9-2E19%3ADownload";
String scheme = "content://com.android.externalstorage.documents/document/primary%3ADCIM%2FCamera";
Uri uri = Uri.parse(scheme);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "vnd.android.document/root");
startActivity(intent );
Toast.makeText(context, "Files app opening in:\n\n" + uri.toString(), Toast.LENGTH_LONG).show();
Instagram allows a single image or video to be uploaded programmatically from an Android app via Android Intents. I have been able to do this successfully. What I want to know is it possible for Instagram to handle multiple images using Intents? Not much to no information on this unfortunately. The following is my last attempt which opens Instagram briefly then closes with a toast message saying "Unable to load image".
Have tried both Intent.ACTION_SEND and Intent.ACTION_SEND_MULTIPLE
val fileUris = ArrayList<Uri>()
val newFile = File("/data/user/0/com.myapp.android/files/media/961087022.jpg")
val contentUri = getUriForFile(this, "com.myapp.fileprovider", newFile)
grantUriPermission("com.instagram.android", contentUri, FLAG_GRANT_READ_URI_PERMISSION)
fileUris.add(contentUri)
val newFile2 = File("/data/user/0/com.myapp.android/files/media/961146948.jpg")
val contentUri2 = getUriForFile(this, "com.myapp.fileprovider", newFile2)
grantUriPermission("com.instagram.android", contentUri2, FLAG_GRANT_READ_URI_PERMISSION)
fileUris.add(contentUri2)
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "image/*"
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, fileUris)
shareIntent.`package` = "com.instagram.android"
startActivity(Intent.createChooser(shareIntent, "Share to"))
I was looking on how to share multiple video files to instagram stories.. I couldn't find how to do it reading the facebook documentation.
Instead i set the intent to send multiple files and set the intent package to "com.instagram.android", and surprisingly it worked..
Intent share = new Intent(Intent.ACTION_SEND_MULTIPLE) ;
share.setType("video/*");
share.putExtra(Intent.EXTRA_SUBJECT, "abc");
share.putExtra(Intent.EXTRA_TITLE, "abcd");
ArrayList<Uri> files = new ArrayList<Uri>();
for (String path : filesToSend) {
File myFiles = new File(path);
Uri doneUri = FileProvider.getUriForFile(Objects.requireNonNull(getApplicationContext()),
BuildConfig.APPLICATION_ID + ".provider", myFiles);
files.add(doneUri);
}
share.setPackage("com.instagram.android");
share.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
startActivity(share);
Hope, this helps!!