How can I create implicit Intent for file in Android - java

I have a XML file on the external storage, and I have different apps which are able to open this file format.
I am going to sent implicit intent like this in order to find apps to open XML file:
public static boolean isIntentAvailable(Context context, Intent intent) {
List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return !list.isEmpty();
}
This method has 2 arguments. The question is how to create intent for second argument?
I have only path to file which I need to open and I tried this way:
Intent intent = new Intent(**path to file**);
But it doesn't work... How can I create this intent properly?

You could do something like this:
File file = new File("path to file");
//checking if the File exists
if (file.exists()) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "text/xml");
//checking if an Activity exists that can handle this Intent
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}

Related

How to open mp3 file on phone's default app?

My code is, at the moment like this:
When the user clicks on an audio file in my file manager, it should open it on their default audio app, but the file manager app just crashes. It also happens with apk and pdf files, but I removed those from the code to focus on the mp3 problem.
Also, if anyone knows a universal way to open files without specifying an extension, it would be amazing!
Thanks in advance, my friends.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
if (files[position].toString().contains(".png") || files[position].toString().contains(".jpg") || files[position].toString().contains(".jpeg")) {
intent.setDataAndType(Uri.parse(files[position].toString()), "image/jpeg");
} else if (files[position].toString().contains(".mp4")) {
intent.setDataAndType(Uri.parse(files[position].toString()), "video/*");
} else if (files[position].toString().contains(".mp3")) {
intent.setDataAndType(Uri.parse(files[position].toString()), "audio/*");
} else if (files[position].toString().contains(".doc") || files[position].toString().contains(".docx")) {
intent.setDataAndType(Uri.parse(files[position].toString()), "application/msword");
} else {
intent.setDataAndType(Uri.parse(files[position].toString()), "*/*");
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
You can use intent like this,
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)), "audio/*");
startActivity(intent);
I don't understand what you did in the code, but if you have the uri to the mp3 file, just use intent.
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
edit: I checked my codebase for a use like this, I opened a txt file for an import. Setting a type always crashed the app for me, hence I set the type to "* /*". See below for a similar code that works.
public void importDictionary() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*"); //needed to not make it crash
startActivityForResult(intent, 2);
}
Override onActivityResult for action afterwards
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2 && resultCode == Activity.RESULT_OK) {
// your code here
}
}

Browse files with a certain extension

I'm developing an application that displays .dxf files (using Kabeja library). I've done all that part and everything works just as I wanted to. Problem is, I need to have a button that opens a file browser so the user can import his own .dxf from the sd card or local storage. It needs to filter files to only display .dxf so no other extension can be imported. I have absolutely no idea how to do this, nor to make a basic file browser. Could you help me getting on the right track ?
Thanks
Try this
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
// Verify that the intent will resolve to an activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, 1);
}
And if you want to enforce an app chooser
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
Intent chooser = Intent.createChooser(sendIntent, "Title");
// Verify that the intent will resolve to an activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(chooser, 1);
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*"); // all images
//intent.setType("image/png"); // only pngs
startActivityForResult(intent);
And get response in onActivityResult method. More details in documentation.

how open a file exclusively with an app

i want open a file with hp ePrint exclusively...but with my code, file is opened with adobe reader...i don't know why....
THANKS IN ADVANCE!
public void viewPDF() {
String path = "/sdcard/droidText/ciccia.pdf";
try {
File targetFile = new File(path);
Uri targetUri = Uri.fromFile(targetFile);
Intent intent=new Intent();
intent.setPackage("com.hp.android.print");
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(targetUri, "application/pdf");
startActivity(intent);
}catch(ActivityNotFoundException anfe){
final String appPackageName="com.hp.android.print";
try{
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("market://details?id=" +appPackageName)));
}catch(android.content.ActivityNotFoundException anfer){
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("https://play.google.com/store/apps/details?id=" +appPackageName)));
}
}
}
Intent intent=new Intent();
intent.setPackage("com.hp.android.print")
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(targetUri, "application/pdf");
You redeclared the intent object.

Android - How to open a specific folder in a gallery using an implicit intent?

We are building a camera app that saves photos in a specific folder in the gallery. And we have to open the folder of our app in the gallery using an intent. We are using this code however it shows all folders.
View.OnClickListener goToGallery = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
};
To open gallery I use this intent.
public static final int RESULT_GALLERY = 0;
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent , RESULT_GALLERY );
Try this code.
It will retrieve view pictures under storage/emulated/0/Pictures/MyAppPics
You can change the file path according to your directory path.
File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File f = new File(sdDir, "MyAppPics");
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.withAppendedPath(Uri.fromFile(f), "/MyAppPics"), "image/*");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);

Open an image in gallery using file path

i have an image file path stored in my database. Now using it i want to open images from SD Card in my gallery. How can i do that. I have seen methods from Uri and using this method but i am getting error
File file = new File(filename);
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setType("image/*");
startActivity(intent); /** replace with your own uri */
How to fix it,
04-20 08:42:23.516: E/AndroidRuntime(16815): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/My%20App/image_umxto_1.jpg }
best regards
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(filename)), "image/*");
startActivity(intent);
Use this code and tell me
You can also use the below code:::
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + "/sdcard/test.jpg"), "image/*");
startActivity(intent);
you can use the below line to do your task:::
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/external/images/media/16"))); /** replace with your own uri */
I was getting FileUriExposedException and after some digging found solution. Convert file path to URI and pass it in intent.
MediaScannerConnection.scanFile(getContext(),
new String[] { getImagePath() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("onScanCompleted", uri.getPath());
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
}
});
Hope it helps.

Categories