4

I am trying to use a simple FileReader and yet, Android Studio keeps telling me that the file in question does not exist. I am using Apache POI if that makes any difference; regardless, the directory I am using is 100 percent correct and I have turned on hidden extensions, so that isn't the problem.

Any help would be appreciated,

Jacob

My java (where ncaa.xlsx exists):

  FileInputStream file = new FileInputStream(new File("C:\\AdwCleaner\\ncaa.xlsx"));
  • 1
    If I understand what you are doing correctly, then you are creating an Android application, running it in an emulator and attempting to access a file from the Windows FS from within the emulator. The emulator does not have access to the host file system, but you can push files to the emulated device's virtual file system and read them from there. – MasterAM Jul 05 '15 at 22:56
  • I am actually running it from my phone. How should I do this if I can't get the file from windows? –  Jul 05 '15 at 23:00
  • 1
    You should copy the file to a known location on your phone (e.g, you SD card) and access it from there. You may also consider using an [assets folder](http://stackoverflow.com/questions/18302603/where-to-place-assets-folder-in-android-studio). – MasterAM Jul 05 '15 at 23:16

3 Answers3

1

Your Android application is being run on a virtual or physical device, that has a completely separate file system.

There are several ways to transfer a file to the the device.

For example, you can push a file to the device using adb push or using the file explorer in DDMS.

If you copy the file to a folder in the SD card, you can open it using Environment.getExternalStorageDirectory() (see example here):

File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, "<path to your file>");

You can also place the file in the assets folder, so it will be automatically available on the device (see this for creating the folder and this for reading the file from it):

InputStream file_stream = getAssets().open("myfile.xslx")
Community
  • 1
  • 1
MasterAM
  • 16,283
  • 6
  • 45
  • 66
  • Thanks for this answer: it really cleared up some confusion, but how would I push this file to my users if I publish the app? –  Jul 06 '15 at 01:26
  • 2
    In case you want a specific file to be available to your users, you can either bundle it into the APK as a [raw resource](http://developer.android.com/guide/topics/resources/providing-resources.html) which will get its own resource identifier (`R.raw.`) or into the assets folder, or make it available for download and fetch it when appropriate (you can cache it for later use, of course). There are probably other ways that I didn't think about, but those are the ones that I know of. – MasterAM Jul 06 '15 at 01:46
1

You can not open a file existing in PC file system to an Android device.

You have to push it first either by using command line adb push command or by using GUI DDMS feature.

After that, you can use Android API's to open the file. For example:

Environment.getExternalStorageDirectory()
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, "file path");
Aminah Nuraini
  • 18,120
  • 8
  • 90
  • 108
RajSharma
  • 1,941
  • 3
  • 21
  • 34
-2

Could it be a virus scanner? Perhaps try disabling the virus scanner temporarily (with your network connection physically disconnected), and run the Java app again.

  • 1
    Thanks for the quick answer, but I don't have any virus scanning software on my computer right now –  Jul 05 '15 at 22:27
  • What about the permissions for that file? Does the account under which you're running Java have read permissions for it? –  Jul 05 '15 at 22:28
  • 1
    Not to my knowledge: I have never encountered any access denied errors before –  Jul 05 '15 at 22:36