Install apk programmatically with DocumentFile - android

This is how i install a apk file:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Now i want to install a apk file from sdcard, i'm getting the uri through Storage Access Framework (From Kitkat), but code does not working.
Uri uri1 = Uri.fromFile(file); //file class File
Uri uri2 = df.getUri(); //df class DocumentFile
The code with uri1 works, but not with uri2
Any workaround?
Thanks

Related

Android Blank screen on PDF

In My App, I have given all permissions like storage, contact etc to app (Given run time permission for marshmallow and above version), but still app shows black screen for some mobile phone.
Permission Denial: opening provider
android.support.v4.content.FileProvider from ProcessRecord{bd58bf1
15489:com.google.android.apps.pdfviewer/u0a213} (pid=15489, uid=10213)
That is not exported from uid 10227.
//Code
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri photoURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider",
pdfFile);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
List<ResolveInfo> resolvedIntentActivities = context.getPackageManager().queryIntentActivities(objIntent,
PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolvedIntentInfo : resolvedIntentActivities) {
String packageName = resolvedIntentInfo.activityInfo.packageName;
context.grantUriPermission(packageName, photoURI, Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
objIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
objIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
objIntent.setDataAndType(photoURI, "application/pdf");
context.startActivity(objIntent);
}else{
Uri path = Uri.fromFile(pdfFile);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
objIntent.setDataAndType(path, "application/pdf");
objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(objIntent);
}`
[Screen]
Hi actually there might be some issue with accessing the FILE URI as it doesnt have the write read uri permission while raising the intent.
check the following thread.. Android - file provider - permission denial
hope you will get some idea.
Also post the code here to understand more.

How to display image in android imageview from SD or internal storage

I tried below code but when,it launches Google Drive App and shows me error not valid format
String completePath = Environment.getExternalStorageDirectory() + "/images/"+"rose.jpeg";
File file = new File(completePath);
Uri imageUri = Uri.fromFile(file);
Intent i = new Intent(Intent.ACTION_VIEW, imageUri);
startActivity(i);

why does Uri.fromFile(new File(url)) work and Uri.parse(url) not in open pdf by intent?

The code below can open pdf file by intent and works well:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(url)), "application/pdf");
if(intent.resolveActivity(getPackageManager()) != null){
startActivity(intent);
}
but if I change Uri.fromFile(new File(url)) to Uri.parse(url), just like below, and it will fail to open pdf file. Why?
intent.setDataAndType(Uri.parse(url), "application/pdf");
I know the class type of instance that the two method return is different, but is this the key that the code above works?
By the way, the url is right and the file exists.
Uri is Type used to provide content to the ContentProvider. The uri is the path to a pdf file right? How would Uri.parse know that what you're passing it is a pdf and not just a url? This is why fromFile is needed.

play video from android internal storage

I'm trying to play video files stored in android internal memory through intent. Files exist there but when i try to play them through intent, it give me the error "Media file not supported" or "can't play video" depending on device. I couldn't find where i'm wrong. here is my code
File mydir = activity.getDir("Videos", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(mydir, filename);
String videoResource = fileWithinMyDir.getPath();
Uri intentUri = Uri.parse(videoResource);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(intentUri, "video/mp4");
startActivity(intent);
I don't know where i'm wrong and what should i do. any help would be much appreciated. Thank you :)
Thank you so much friends for participation, your efforts are very much appreciated. I'v found the solution of my problem. There is need to set file read true before playing it.
fileWithinMyDir.setReadable(true, false);
Now here is complete code of intent to play mp4 video from internal storage of android.
File mydir = activity.getDir("Videos", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(mydir, headingsList.get(mPosition));
fileWithinMyDir.setReadable(true, false);
String videoResource = fileWithinMyDir.getPath();
Uri intentUri = Uri.fromFile(new File(videoResource));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(intentUri, "video/mp4");
startActivity(intent);
This code works for me:
Uri fileUri = Uri.fromFile(new File(String file_path));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri, URLConnection.guessContentTypeFromName(fileUri.toString()));
startActivity(intent);

How can I show file in sdcard to user?

I want to show user my apk file using intents, here is the code:
SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot,fileName);
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new
Intent(Intent.ACTION_VIEW,path);
startActivity(intent);
but it is giving me ActivityNotFoundException,I want to start the apk file so that user can install it.
Try this?
Uri path = Uri.fromFile(file);
Intent intent = new
Intent(Intent.ACTION_VIEW,path);
intent.setDataAndType(path, "application/vnd.android.package-archive");
startActivity(intent);

Categories

Resources