Android doesn't find Intent for opening a PNG File - android

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")

Related

How to open DCIM/Camera folder in default Files app from intent

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();

start directory of a file explorer Intent for 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)
}

No activity error found when trying to install an apk from a local file

I made an application where I download an APK from an internal server, save it locally and want to prompt the user to install it.
My code is the following:
protected void onPostExecute(String path) {
Intent promptInstall = new Intent(Intent.ACTION_VIEW);
promptInstall.setDataAndType(Uri.parse(path), "application/vnd.android.package-archive");
promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(promptInstall);
}
The path has the file of the APK so no problem with the download but when trying to launch the activity I get:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=/storage/sdcard/download/internalLocalApplication.apk typ=application/vnd.android.package-archive flg=0x10000000 }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1765)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1602)
at android.app.Activity.startActivityFromFragment(Activity.java:4340)
at android.app.Activity.startActivityFromFragment(Activity.java:4312)
at android.app.Fragment.startActivity(Fragment.java:1075)
at android.app.Fragment.startActivity(Fragment.java:1054)
What am I doing wrong here?
UPDATE:
Seems that if I change the code as follows it works! Seems that there is an issue with the Uri?
#Override
protected void onPostExecute(String path) {
Uri fileLoc = Uri.fromFile(new File(path));
Intent promptInstall = new Intent(Intent.ACTION_VIEW);
promptInstall.setDataAndType(fileLoc, "application/vnd.android.package-archive");
promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(promptInstall);
}
When you call Uri.parse(path), the path argument needs to contain something like this:
file:///storage/sdcard/download/internalLocalApplication.apk
Currently your path argument doesn't contain the scheme (ie: the file:// part), which is why it can't be properly parsed.
When Android then looks for an Activity that can view this URI, It can't find one that matches, since the URI has no "scheme".
I've had this same error when trying to open an URI generated by the DownloadManager, like dm.getUriForDownloadedFile(id);
Fixed it with the following code:
Uri uri;
Cursor q = dm.query(new DownloadManager.Query().setFilterById(id));
if ( q == null ) {
Toast.makeText(AppContext,
"FAILED: Unable to read downloaded file" ,
Toast.LENGTH_LONG).show();
break;
}
q.moveToFirst();
String path = "file://" + q.getString(q.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
uri = Uri.parse(path);
Log.d(TAG,"dm query: " + path );
intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(uri, dm.getMimeTypeForDownloadedFile(id));

Android - No Activity found to handle Intent { act=android.intent.action.VIEW - Trying to open a PDF File

I searched a lot to find a solution , but still can't find any
I am trying to open a PDF file in my android application
Here is my code to do this :
try
{
File file = new File(Environment.getExternalStorageDirectory()+"/pdf/Read.pdf");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
Log.d("CheckingURI", uri.toString());
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
}
catch (Exception e)
{
Log.d("OpenPDFError", e.getMessage());
}
The Tomcat Log implies that there is no activity to handle the intent
09-19 19:55:02.938: D/CheckingURI(30483): file:///mnt/sdcard/pdf/Read.pdf
09-19 19:55:02.948: D/OpenPDFError(30483): No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/pdf/Read.pdf typ=application/pdf }
Is there any other way to do this or can i open the PDF file with another default PDF viewer? If yes, how?
Update :
the main problem was that i did not install a PDF viewer application on my emulator ...
Try this instead:
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/pdf/Read.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
CAVEAT: The above method only work if you have a pre-installed PDF Viewer. If not, you need to install a PDF Viewer.
You just have to popup some sort of error msg to the user if no activity was found to launch the PDF (In the error dialog you can let the user know he needs some PDF app and send him to the Play Store), though i'd suggest using this piece of code instead of the genral exception catcher you're using:
try {
/* your code */
...
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
or you can use some PDF library, like this one.

opening local html file with Android Browser

i'm trying to open a local html file using the default browser using the following code:
Uri uri = Uri.fromFile(file);
Intent browserIntent = new Intent(Intent.ACTION_VIEW).setData(uri);
startActivity(browserIntent);
but i'm getting the following exception:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///sdcard/SolveDroid/solution.html }
i'm confused - should i create an activity to hande the web beowser? isn't it supposed to just call its activity?
please advise :)
UPDATE:
the same code works if i pass a URL like so:
Uri uri = Uri.parse("http://www.metalist.co.il");
Uri uri = Uri.fromFile(file);
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
browserIntent.setData(uri);
startActivity(browserIntent);
I found an answer for this problem... just needed to add
browserIntent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
i used it with the "file://" uri by using Uri.fromfile(file) and it works (Android v.2.2.1)
try this
Intent in = new Intent(Intent.ACTION_VIEW);
File f=new File("/sdcard/html.html");
in.setDataAndType(Uri.fromFile(f), "text/html");
startActivity(in);

Categories

Resources