1

I know that I can read files from assets using AssetManager, like here

AssetManager assetManager = getAssets();
InputStream is = assetManager.open(filename)

but open() method returns InputStream. So what I should do when I need to work with FileInputStream not with it superclass. Is there a way to get FileInputStream instance by InputStream instance?

Nischal Hada
  • 3,230
  • 3
  • 27
  • 57
Mike Herasimov
  • 1,319
  • 3
  • 14
  • 31

2 Answers2

3

No, there is not a way to convert an arbitrary InputStream back to a FileInputStream, as the data source may be of a different nature - something other than a literal File.

Assets are not files on the device, but rather particular chunks of the zip file which is your .apk. The Asset APIs give you access that is file-like in many ways (particularly with regard to input streams), but does not ultimately wrap an individual java.io.File, but rather an engine for extracting data directly from the .apk

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • one more question how I can read embeded files? I mean files that was created before first running of application (files that application don't created at all) I tried to google this, but I get no result – Mike Herasimov May 18 '15 at 15:36
  • 1
    Apart from the binary form of NDK libraries themselves, there isn't really a mechanism for packaging "files" in an APK that show up as "files" on the device - only Assets and Resources which have their own APIs for access. In the case of databases, people sometimes will do that, and copy out to a file on first run. – Chris Stratton May 18 '15 at 15:41
  • but where I can to store them? after apk creation they deleting from `data/data/files` directory – Mike Herasimov May 18 '15 at 15:45
  • 1
    Not normally they won't be. The app's storage folder is only erased by the system if you uninstall before installing, or switch to a build with a different signature, or explicitly clear app data. Of course `data/data/files` is an invalid path - you should be calling `getFilesDir()` on your Activity or Service. – Chris Stratton May 18 '15 at 15:46
  • so, when I only install aplication from apk file files will be in `files` folder or not? – Mike Herasimov May 18 '15 at 15:49
  • 1
    Nothing (apart from NDK libs) will be unpacked automatically, but anything you explicitly wrote in your private folder while running an earlier version of your app would normally still be there, provided you haven't uninstalled, cleared data, or changed certificates (such as between debug and release). – Chris Stratton May 18 '15 at 15:51
  • aha, now I understand more clearly, so I shood call `getFilesDir()` when I deal with internal files, yes? – Mike Herasimov May 18 '15 at 15:51
  • 1
    When you want to deal with files you have created at runtime yourself, yes. That's an entirely different subject from Assets or Resources packaged in the .apk, which for practical purposes you have to access using their unique non-File APIs. In some cases you may want to copy the later to the former on first run, for example, writable databases with initial data shipped in the .apk. – Chris Stratton May 18 '15 at 15:53
  • I know about DB that I need to copy them on first run and throught that when you working with files you need to do the same thing – Mike Herasimov May 18 '15 at 15:58
  • now I understand that they storing in another directory) – Mike Herasimov May 18 '15 at 15:59
  • and the same answer will be, when I only put them in `files` directory in ADT? – Mike Herasimov May 18 '15 at 16:01
  • 1
    There is no "files" directory in the project source which will be included in the package installed on the device. If you want to include data in your apk, you must use one of the provided mechanisms. – Chris Stratton May 18 '15 at 16:04
  • so my application cannot connect to this files? you had said that all files will be stored in directory, returned by `getFilesDir()`, yes? – Mike Herasimov May 18 '15 at 16:07
  • one of which mechanisms? – Mike Herasimov May 18 '15 at 16:11
  • 1
    No, **only** files which your app itself creates there **at runtime**. There is no connection between the installation directory tree and the project source directory tree. – Chris Stratton May 18 '15 at 16:11
  • "No, only files which your app itself creates at runtime." the thing that I've suspected from the beginning – Mike Herasimov May 18 '15 at 16:13
  • 1
    This has gone on too long, and is not going to be resolved here on SO. It's time you did some reading in the SDK docs. – Chris Stratton May 18 '15 at 16:14
  • so what I should do to properly store and read files? – Mike Herasimov May 18 '15 at 16:14
  • "This has gone on too long" you are right, thanks for advice and your time – Mike Herasimov May 18 '15 at 16:16
  • can you paste there some links, please? because I don't know what I'm actually need to google – Mike Herasimov May 18 '15 at 16:17
2

In that case you can create a StringBuilder and read whole content of text file in it something like this

StringBuilder buf=new StringBuilder();
InputStream is=getAssets().open(filename);
BufferedReader in= new BufferedReader(new InputStreamReader(is, "UTF-8"));
String str;

while ((str=in.readLine()) != null) {
     buf.append(str);
}

in.close();

Note: Things differ based on the file type you try to access; as Chris Stratton wrote as the data source may be of a different nature.

Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41