Android API has DocumentFile class. This class has canWrite() method.
Suppose I called this method and it returned true. Also suppose this object was representing "raw" file.
Now how can I do what it said I can?
Namely, how to write "Hello world" text into that file?
Thanks.
Namely, how to write "Hello world" text into that file?
It is not necessarily a file.
To write to the document identified by that DocumentFile, call getUri() on that DocumentFile to get the Uri to the document. Pass that to openOutputStream() on a ContentResolver. Then, write to the stream, flush() the stream, and close() the stream. Basically, once you get the OutputStream, from there ordinary Java I/O takes over.
Related
please am working on app that store sensitive user data on a database upon googling on how to do the answers point me to cwac saferoom since am using room for my database but the problem am facing are:
Cwac saferoom required i pass in an edittable object meani g the same method i call on edittext to get the input string as passphrase i dont really know how make a that object out of a string
How do i safely store the password on the device also
Please am using java
Cwac saferoom required i pass in an edittable object
Quoting the documentation: "The SafeHelperFactory constructor takes a either a byte[] or a char[] for the passphrase.". There is a utility method that takes an Editable, for the recommended path of getting the passphrase from the user. So, just create a SafeHelperFactory object via the constructor:
SafeHelperFactory factory = new SafeHelperFactory(thePassphraseFromTheUser);
i dont really know how make a that object out of a string
It is not a good idea to have a passphrase in a String. See:
https://www.sjoerdlangkemper.nl/2016/05/22/should-passwords-be-cleared-from-memory/
Why is char[] preferred over String for passwords?
But, for tests and stuff, call toCharArray() on your String to get a char[] to pass to the SafeHelperFactory constructor:
SafeHelperFactory factory = new SafeHelperFactory(stringPassphraseFromTheUser.toCharArray());
How do i safely store the password on the device also
Generally, you don't. You get the passphrase from the user.
If your minSdkVersion is 23 or higher, you could use androidx.security:security-crypto classes to store a generated passphrase in hardware-encrypted storage.
I'm struggling with storing a realm db on the sd card. The point is that I have to use DocumentFile instead of simple File object to have write access. Another words:
Uri uri = getUriInstanceToSaveDB(); // my inner method
new File(uri.getPath()).canWrite() == false
DocumentFile.fromTreeUri(getActivity(), uri).canWrite() == true
Therefore I can't just store data using RealmConfiguration.Builder() (cause it uses File object as storing mechanism). Of course, I've also tried to use simple String there instead of File object - no result.
Real doesn't support DocumentFile at this point in time, so I'm afraid you are currently out of luck unless you can find a way to map a DocumentFile to a local File reference.
Also one of the problems with DocumentFile is that it might reference a file that doesn't exist locally, which would prevent Realm from using it. So it is unclear if Realm could ever support this.
I am newbie to Android development and I am confused between the difference of URI and Uri?
Please mention the main differences.
java.net.URI is mutable
android.net.Uri is immutable
Immutable URI reference. A URI reference includes a URI and a fragment, the component of the URI following a '#'. Builds and parses URI references which conform to RFC 2396.
In the interest of performance, this class performs little to no validation. Behavior is undefined for invalid input. This class is very forgiving--in the face of invalid input, it will return garbage rather than throw an exception unless otherwise specified.
More infos here
When I use either of them, I get the same result. It's just that to use getFileAbsolutePath, I use a file object.
Log.v("getFilesDir", "" + getFilesDir().toString());
file = getFilesDir();
Log.v("file.getAbsolutePath",""+file.getAbsolutePath());
Result:
V/getFilesDir﹕ /data/data/com.kingbell.interprocesscommunication_simple/files
V/file.getAbsolutePath﹕/data/data/com.kingbell.interprocesscommunication_simple/files
As explained in android docs.
getFilesDir()
Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.
This means that it returns a File Object, A File could represent files or directories, in this case a directory. you can check it by calling the isDirectory() method.
getAbsolutePath()
Returns the absolute path of this file.
This is the path of this file, but rememeber that directories are represented with the same file objects just like a real file. you can verify it by checking the isDirectory() or isFile() method.
So, in your case they point to the same place.
You called getFilesDir(), so your context give you a directory represented in an instance of the File class. In that file object you call getAbsolutePath() so they represent the same thing, since this is an actual directory.
Clear?
Maybe it will be more clear with the source code.
getAbsolutePath()
package java.io.file
public class File {
public String getAbsolutePath() {
return fs.resolve(this);
}
}
and getFilesDir()
package android.content
public abstract class Context {
public abstract File getFilesDir();
}
and for a specific Context(typically is an activity), the return value for the getFilesDir() call is fixed and should be always same, it tells you a location you can use to save your file.
At the same time, you can call getAbsolutePath() on any File object to get a full file path, either the file created by you or the file carried in an URI sent to you.
That is why these two calls are in the different packages.
Hope the explanation helps.
How can I construct a URI to table MediaStore.Files.FileColumns ?
I try like this:
Uri uri=Uri.parse(MediaStore.Files.FileColumns);
But in Eclipse "MediaStore.Files.FileColumns" is underlined. I guess, I do this wrong. How to correctly construct a URI?
FileColumns is interface. You cannot reference interface. If you need explicit column, use
Uri uri=Uri.parse(MediaStore.Files.FileColumns.<COLUMN>);
for example:
Uri uri=Uri.parse(MediaStore.Files.FileColumns.MEDIA_TYPE);
MediaStore.Files.FileColumns is an Interface, the method Uri.parse() receives a string.