I have a third party library which accepts a File object and modifies the same.
Now after getting the permission to SD card using
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
And selecting SD card root.
then user browses through the files which he wants to modify. Now this
file's path/ file object I need to pass to the third party library class.
DocumentFile documentFile = DocumentFile.fromFile(file);
String documentURI = documentFile.getUri().toString();
MP3File mp3File = null;
documentFile = DocumentFile.fromFile(file);
mp3File = new MP3File(file.getAbsolutePath());
Here MP3File class accepts file path. but this class does not have any Document constructor.
Can i convert document to a writable file which i can replace with the original file? Or is there any simpler solution?
Now after getting the permission to SD card using... And selecting SD card root.
There is no requirement for the user to choose any particular document. The document might be on removable storage, or external storage, or Google Drive, or Dropbox, or a file server, or anywhere else the user can access.
Now this file's path/ file object I need to pass to the third party library class.
There is no "path/file object" for that document.
Here MP3File class accepts file path
Either:
Find a better library, one that accepts an InputStream and/or OutputStream, so you can work with the document where it resides, or
Use Java I/O to copy the document's contents to a local file that you control, then pass that file to the library
Related
I am trying to share images from other applications to my application using implicit intent ACTION_SEND.
While sharing search images from chrome browser, app receives intent with a Content URI like this:
content://com.android.chrome.FileProvider/images/screenshot/1457448067808912906311.jpg
How can I fetch file path from this type of Content URI? All other apps like Facebook, Google+ are doing it.
I am using FileChooser for getting file path of other types of Content URIs (eg. from Gallery).
Tried looking everywhere, without much help.
Can someone suggest how to work with these Content URIs?
If you absolutely need a local copy of the file, you are going to need to open the InputStream copy the contents to a local file that you know the path to and then go from there. Sidenote: Guava's ByteStreams#copy is an easy way to accomplish this.
Of course this file is no longer backed by the original Uri source, so I don't think this is what you want. Instead, you should work with the Uri's intended API. Take a look at the Storage Access Framework
Edit
Here is how you can get an InputStream from your Uri
InputStream inputStream = getContentResolver().openInputStream(uri);
How can I fetch file path from this type of Content URI?
You don't, as there does not have to be a file at all behind the Uri, let alone one that you can access. That Uri might point to:
A local file on external storage
A local file on internal storage for the other app
A local file on removable storage
A local file that is encrypted and needs to be decrypted on the fly
A stream of bytes held in a BLOB column in a database
A piece of content that needs to be downloaded by the other app first
...and so on
All other apps like Facebook, Google+ are doing it
No, they are not. They are using ContentResolver and:
openInputStream() to read in the bytes associated with the content
getType() to get the MIME type associated with the content
query() and the OpenableColumns to get the size and display name associated with the content
I am trying to share images from other applications to my application using implicit intent ACTION_SEND.
While sharing search images from chrome browser, app receives intent with a Content URI like this:
content://com.android.chrome.FileProvider/images/screenshot/1457448067808912906311.jpg
How can I fetch file path from this type of Content URI? All other apps like Facebook, Google+ are doing it.
I am using FileChooser for getting file path of other types of Content URIs (eg. from Gallery).
Tried looking everywhere, without much help.
Can someone suggest how to work with these Content URIs?
If you absolutely need a local copy of the file, you are going to need to open the InputStream copy the contents to a local file that you know the path to and then go from there. Sidenote: Guava's ByteStreams#copy is an easy way to accomplish this.
Of course this file is no longer backed by the original Uri source, so I don't think this is what you want. Instead, you should work with the Uri's intended API. Take a look at the Storage Access Framework
Edit
Here is how you can get an InputStream from your Uri
InputStream inputStream = getContentResolver().openInputStream(uri);
How can I fetch file path from this type of Content URI?
You don't, as there does not have to be a file at all behind the Uri, let alone one that you can access. That Uri might point to:
A local file on external storage
A local file on internal storage for the other app
A local file on removable storage
A local file that is encrypted and needs to be decrypted on the fly
A stream of bytes held in a BLOB column in a database
A piece of content that needs to be downloaded by the other app first
...and so on
All other apps like Facebook, Google+ are doing it
No, they are not. They are using ContentResolver and:
openInputStream() to read in the bytes associated with the content
getType() to get the MIME type associated with the content
query() and the OpenableColumns to get the size and display name associated with the content
I am trying to share images from other applications to my application using implicit intent ACTION_SEND.
While sharing search images from chrome browser, app receives intent with a Content URI like this:
content://com.android.chrome.FileProvider/images/screenshot/1457448067808912906311.jpg
How can I fetch file path from this type of Content URI? All other apps like Facebook, Google+ are doing it.
I am using FileChooser for getting file path of other types of Content URIs (eg. from Gallery).
Tried looking everywhere, without much help.
Can someone suggest how to work with these Content URIs?
If you absolutely need a local copy of the file, you are going to need to open the InputStream copy the contents to a local file that you know the path to and then go from there. Sidenote: Guava's ByteStreams#copy is an easy way to accomplish this.
Of course this file is no longer backed by the original Uri source, so I don't think this is what you want. Instead, you should work with the Uri's intended API. Take a look at the Storage Access Framework
Edit
Here is how you can get an InputStream from your Uri
InputStream inputStream = getContentResolver().openInputStream(uri);
How can I fetch file path from this type of Content URI?
You don't, as there does not have to be a file at all behind the Uri, let alone one that you can access. That Uri might point to:
A local file on external storage
A local file on internal storage for the other app
A local file on removable storage
A local file that is encrypted and needs to be decrypted on the fly
A stream of bytes held in a BLOB column in a database
A piece of content that needs to be downloaded by the other app first
...and so on
All other apps like Facebook, Google+ are doing it
No, they are not. They are using ContentResolver and:
openInputStream() to read in the bytes associated with the content
getType() to get the MIME type associated with the content
query() and the OpenableColumns to get the size and display name associated with the content
I have a content URI in the form of "content://media/external/images/media/175". I am trying to find the absolute path of the image in question in order to send the file in a request. Is there any way to accomplish this?
I am trying to find the absolute path of the image
A Uri is not a file. You cannot convert arbitrary Uri values to some filesystem path that you can access. Those Uri values might point to:
A file on removable storage
A file on internal storage of some other app
A file that has not yet been downloaded, but will be downloaded on the fly by the ContentProvider
A file that has been encrypted and will be decrypted on the fly by the ContentProvider
A BLOB in a database
Content that will be generated dynamically, the way this Web page is
And so on
in order to send the file in a request
Use a ContentResolver and openInputStream() to get an InputStream to use in your request. If whatever API you are using does not support an InputStream, either:
Switch to a better API, or
Use that InputStream to make a local file copy on your own internal storage, then use that file
I am creating a file into Internal storage (/data/data/package_name/myfile_name), I want to send that file with attachment but I am getting a blank file in attachment(although I checked that file from file explorer, that file is present and not empty at same location).
And same code running well when I used external storage( I am getting my actual file in attachment). Is there any restrictions that we can not send file which are present in internal storage? Or other steps I am missing?
I'm assuming you are trying to send the file as an email attachment using intents.
The reason why the file is empty is that the email app does not have access to the file in /data/data/package_name/myfile_name, due to Androids security model (the /data/data/package_name directory is private to your app).
In order to add the file as an attachment, you need to write it to public storage (such as the SD card) so the email app can access it.
How do you know the file exists at the path you're interested in? Can you view it with DDMS or ADB after your application saved it there? What code are you using to save/read the file? I may be able to provide more specific assistance with that information.
The method used to obtain the internal storage directory on any given device is Context.getFilesDir(). To create a reference to a file named "myfile.dat", for instance:
File myFile = new File(getFilesDir(),"myfile.dat");
Assuming you call the code from inside an Activity or other Context. In order to attach this file to an email, you would be passing a Uri to that location as an extra, so let's add the creation of that to the example:
File myFile = new File(getFilesDir(),"myfile.dat");
Uri fileUri = Uri.fromFile(myFile);
This is all assuming the file was properly saved into Internal Storage in the first place.
Hope that Helps!