Get file location path in Android - android

My problem is that I cannot get a full path of a file in Android. I search on Google and find out this code which has a result nearly what I want:
public void openFolder()
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ "/sdcard/");
intent.setDataAndType(uri, "image/png");
startActivity(Intent.createChooser(intent, "Open folder"));
}
I choose file from "File Manager". After I access some folders and finally reach my file in "File Manager", the "File Manager" closes and no path is saved.
What should I do in order to save the path and file name? Ex: "/sdcard/Download/frame1.png".
Thank you in advance!

I believe you will need to implement onActivityResult as described in an example below.
Example: http://steveliles.github.io/returning_a_result_from_an_android_activity.html

First you can assign a File object to your file as fileA.Then the file path should be path = fileA.getAbsolutePath();
You can store this String in your clipBoard using ClipboardManager.

Related

How to open File manager navigated to specific directory in android?

I want to show downloaded file in File Manager by giving the File absolute Path.I read a lot about it and find same thing
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(file.getAbsolutePath());
intent.setDataAndType(uri, "*/*");
startActivity(Intent.createChooser(intent, "Open folder"));
And it results to view only recent files that has been viwed. Please guide to Open File Manager while navigating it to file whose absolute path has been given.
I read a lot about it and find same thing
ACTION_GET_CONTENT has little to do with a file manager.
Please guide to Open File Manager while navigating it to file whose absolute path has been given.
There is nothing on Android for this, sorry.

How to open the gallery using the file path in android

I have designed two buttons.One button for selecting the file and another button for to open the selected file.I have selected the file correctly and the file path also retrieved.But i cant open the particular file directly by the file path.Any one I have tried something like this
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ selectedFilePath);
intent.setDataAndType(uri, "text/csv");
startActivity(Intent.createChooser(intent, "Open folder"));
First, ACTION_GET_CONTENT does not accept a Uri as input.
Second, ACTION_GET_CONTENT has nothing to do with opening a file. Presumably, you should be using ACTION_VIEW.
Third, the value that you are passing to Uri.parse() is not a String form of a Uri.
Also, I would expect few Android devices to have an ACTION_VIEW activity for text/csv content.

Unable to share image in android

I am new in android Java.
I am trying to share an image in android application using Phonegap. I get new class "Share" as CordovaPlugin, code is follow...
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
Uri uri = Uri.parse("file:///android_asset/www/sharethis.png");
share.putExtra(Intent.EXTRA_STREAM, uri);
cordova.getActivity().startActivityForResult(share, 0);
Above code is not working, it showing like this ...
is think i cant get Exact image file location.
My file location
I tried bellow code also, But not work,
String imagePath = Environment.getExternalStorageDirectory()
+ "/assets/www/sharethis.png";
Android asset folder is private to your app so you can't share files from there directly. You need to copy the file from assets to a public directory in the filesystem and then send the share intent pointing to the public file

Input and output path in Android

I am using FFMPEG to make a video editor. I stuck when selecting a folder:
1/ I will show "File Manager" to user. They can choose a folder then return a path. How can choose a folder and get its path. For example: /sdcard/videokit/.
Here is my code, I must choose a mp4 file (not a folder I want).
public void openFolder()
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ "/sdcard/");
intent.setDataAndType(uri, "folder|video/mp4");
startActivityForResult(Intent.createChooser(intent, "Select Folder"),PICK_VIDEO_OUTPUT_REQUEST);
}
Thank you in advance!
No, you cannot use Intent to choose folder in Android. You need a dialog of your own. Luckily, there are plenty ready to use code samples available, e.g. How to select folder in android?.

Opening PDF externally with Polaris office 5 returns "This document cannot be opened"

We've been trying to solve this issue for the past few hours, finally decided we should revert to StackOverflow.
Here goes -
We have an application that downloads a PDF file from a server to the app's cache directory and then opens it using the packet manager. Of course it goes through grantUriPermission() to give read (and write) permissions to all available packages.
Though it works great on most devices, today we encountered a device that has POLARIS Office 5 installed as the default PDF viewer.
Whenever we're opening the file, Polaris simply displays a message saying "This document cannot be opened".
I should say that when trying to open the file through Acrobat Reader, it works great.
Also, when we copied the file from the cache directory to an external directory (using Android's File manager) and then opened it in Polaris manually it worked great.
We would have given up on it, but since Polaris is the default viewer on many devices, we really would love solving that problem.
Here is the code -
public void onDownloadDone(String filepath) {
// Set a file object that represents the downloaded file
File file = new File(filepath);
// Set an intent for the external app
Intent intent = new Intent(Intent.ACTION_VIEW);
// Get mime type of the downloaded file
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(filepath);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
intent.setType(mimeType);
// Look for installed packges according to the file's mime type
PackageManager pm = context.getPackageManager();
Uri contentUri = FileProvider.getUriForFile(context, "myapp.fileprovider", file);
// Set the file uri in the intent
intent.setData(contentUri);
// Give permissions to the file to each external app that can open the file
List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
for (ResolveInfo externalApp: activities){
String packageName = externalApp.activityInfo.packageName;
context.grantUriPermission(packageName, contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
// Start the activities (or launch the menu) if any activity exists
if (activities.size() > 0){
context.startActivity(intent);
} else {
System.out.println("Warning!!! No app for file " + filepath);
}
}
Thank a lot!
I had exactly the same problem. The PDF files would open with ezPDF and Adobe but not with Polaris Viewer.
You have to set the data and type with:
intent.setDataAndType(uri, "application/pdf");
instead of using:
intent.setType("application/pdf");
intent.setData(uri);
For me the following is working fine with Polaris now:
Uri uri = FileProvider.getUriForFile(MyApplication.getContext(), MyApplication.getContext().getPackageName() + ".myfileprovider", destFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);

Categories

Resources