1

Background

Starting from API 21 (Lollipop), apps can get a special "permission" to modify real SD-cards, as shown on previous posts I've written (here and here).

The problem

I can delete files, and I can also create them, but I can't find a way to perform other basic file operations:

  • read, write, using InputStream and OutputStream
  • move files.
  • create a folder and not just a file
  • rename a file
  • get file info (recent updated,etc...)
  • share/open the file via other apps.
  • other operations I might have forgot about.

The question

How do I get those features? Surely it's possible to perform basic read and write, for example...

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270

1 Answers1

1

Using FileUilts for Lollipop gives you most of those options, have you seen this: https://github.com/jeisfeld/Augendiagnose/blob/master/Augendiagnose/src/de/eisfeldj/augendiagnose/util/imagefile/FileUtil.java ?

(I could not add a comment)

Sahar Avr
  • 131
  • 8
  • This is interesting. But I have to ask: how did you find those solutions? Where in the docs is there information about it? Why some of the code seem like workarounds? How is it that there is even support for Kitkat? Will it work for Kitkat? And, what should be done to get file information (like when it was created etc... ) ? – android developer Aug 15 '15 at 17:19
  • Just like you, I researched. To share via other apps, research about "intent filters". – Sahar Avr Aug 15 '15 at 22:02
  • But where did you get it from, so that you've made this code? Also, please answer the other questions. – android developer Aug 16 '15 at 06:27
  • I didn't write this code. When I studied Android development I had to make a file manager as a project. I searched for the best way to manage files and came to a conclusion that FileUtils is the best option so far. I did not search the docs, I searched Google and found about it somehow. I'm sorry. Yes, it will work for Kitkat as well. To read the file's attributes, use `File.readAttributes` - https://docs.oracle.com/javase/tutorial/essential/io/fileAttr.html . Also look at - http://kodejava.org/how-do-i-get-file-basic-attributes/ – Sahar Avr Aug 17 '15 at 01:16
  • Sure that File.readAttributes will work for the new API of Lollipop, and even Kitkat? Won't you need to use DocumentFile for this? – android developer Aug 17 '15 at 08:31
  • I'm pretty sure. You can check it yourself by launching different devices as emulators in Android Studio. – Sahar Avr Aug 17 '15 at 15:52
  • Emulators do not really support the SD-card. It's considered as primary external storage. not secondary, so it doesn't need to use the DocumentFile for anything. – android developer Aug 17 '15 at 19:06
  • I understand you. I can't think of another way to check this, other than just finding a Kitkat device... I'm deeply sorry that I couldn't help – Sahar Avr Aug 17 '15 at 19:09
  • Well at least put the code you've found here, and not just a link to it, as it might some day be unavailable. Here, have a +1 for now. – android developer Aug 18 '15 at 05:20
  • Just found out that this repo is GNU... I can't use this, no? doesn't it mean that whoever use this, has to make the entire code open, or something? – android developer Aug 21 '15 at 23:24
  • Really curious as to how to do this, but the link is broken. – JohannB Dec 11 '15 at 16:22
  • https://github.com/jeisfeld/Augendiagnose/blob/master/AugendiagnoseIdea/augendiagnoseLib/src/main/java/de/jeisfeld/augendiagnoselib/util/imagefile/FileUtil.java – isabsent Jan 10 '16 at 08:14