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
}
}
Related
I am trying to select PDF file on button click but I'm getting following message on samsung device :
all apps associated with this action have been turned off blocked or
are not installed.
private void selectPDFFiles(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse("package:" + getPackageName()));
startActivity(intent);
return;
}
intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent,"select PDF "), 1);
}
Try below code
private void selectPDFFiles(){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
startActivityForResult(intent, 555);
}
And you can get file in activity result
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 555:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
String uriString = uri.toString();
File myFile = new File(uriString);
String path = myFile.getAbsolutePath();
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
Update
Also try below code
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("application/pdf");
startActivityForResult(intent, 555);
I hope this can help You!
Thank You.
I have implemented adding multiple image selection from the gallery in my project. However, I want to limit the user so he/she can select only 3 images from the gallery.
I have implemented selecting multiple images from the gallery like this:
`Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);`
How can I achieve this?
Thanks.
You can get the count of ClipData when selecting multiple images from and gallery and if that count is greater than 3 you can notify the user about it.
You can do something like this after selecting images from gallery:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK || resultCode != RESULT_CANCELED){
ClipData clipData = data.getClipData();
if(clipData.getItemCount() > 3){
//notify user here...
}
}
}
This is not possible.
https://developer.android.com/reference/android/content/Intent.html#EXTRA_ALLOW_MULTIPLE
You'll have to manually check the returned data to see if it's more than 10 items, and if so, show a Toast
put this in your build.gradle(app)
compile 'com.github.esafirm.android-image-picker:imagepicker:1.5.0'
compile 'com.github.esafirm.android-image-picker:rximagepicker:1.5.0'
and this in your activity
private void pickImage() {
ImagePicker.create(UploadPhotosActivity.this)
.showCamera(false)
.limit(3)
.imageTitle(getString(R.string.select_image))
.folderTitle(getString(R.string.folder))
.theme(R.style.ImagePickerTheme)
.start(RC_CODE_PICKER);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_CODE_PICKER) {
Log.d("===uploadPhoto", "gallery : " + data);
imagesList = (ArrayList<Image>) ImagePicker.getImages(data);
Intent intent = new Intent(UploadPhotosActivity.this, ImageCropperActivity.class);
intent.putExtra(ImageCropperActivity.EXTRA_VIEW_PORT_RATIO, imagesList);
startActivity(intent);
}
} else {
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
}
}
Anyone knows the intent to search directly through the official Duckduckgo app on Android?
Tried this one so far, i think they dont have a query key in extras
Intent intent = new Intent(Intent.ACTION_SEARCH);
intent.setPackage("com.duckduckgo.mobile.android");
intent.putExtra("query", subject);
context.startActivity(intent);
It is actually pretty simple than i thought
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://duckduckgo.com/?q=" + subject));
try {
intent.setPackage("com.duckduckgo.mobile.android");
context.startActivity(intent);
} catch (ActivityNotFoundException a) {
intent.setPackage(null);
context.startActivity(intent);
}
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.
I want to choose a file via an Android File Manager.
so, to open file intent code is
Intent intent = new Intent();
intent.setType("file/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select File"), 1);
But I was unable to access file manager in samsung devices to resolve this problem i do
Intent intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(intent, 1);
But this code in not working in other devices so what should i do to open file manager in all devices ?
private void openFile(int requestCODE) {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("*/*");
startActivityForResult(intent, requestCODE);
}