40

I have used the following code to create a temporary file in my android app:

public File streamToFile (InputStream in) throws IOException {
    File tempFile = File.createTempFile("sample", ".tmp");
    tempFile.deleteOnExit();
    FileOutputStream out = new FileOutputStream(tempFile);
    IOUtils.copy(in, out);
    return tempFile;
}

Now the problem is Cannot resolve symbol 'IOUtils'. I did a little bit of googling and discovered that for using IOUtils I need to download and include a jar file. I downloaded the jar file from here(commons-io-2.4-bin.zip). I added the jar named commons-io-2.4.jar from the zip to my bundle and when I tried to import it using:

import org.apache.commons.io.IOUtils;

It is showing error Cannot resolve symbol 'io'. So I tried to import it like:

import org.apache.commons.*

But still I am getting the error Cannot resolve symbol 'IOUtils'.

Question 1 : Why am I getting this error? How to resolve it?

Question 2 : Is there any way to create a temp file from an InputStream without using an external library? Or is this the most efficient way to do that? I am using android studio.

Harikrishnan
  • 7,765
  • 13
  • 62
  • 113

4 Answers4

101

For Android Studio:

  1. File -> Project Structure... -> Dependencies
  2. Click '+' in the upper right corner and select "Library dependency"
  3. In the search field type: "org.apache.commons.io" and click Search
  4. Select "org.apache.directory.studio:org.apache.commons.io:<X.X>

Or

Add implementation 'org.apache.directory.studio:org.apache.commons.io:2.4 to the build.gradle file

Ivo Stoyanov
  • 16,256
  • 8
  • 62
  • 65
9

Right clicking on the commons-io-2.4.jar file in project navigator and clicking 'Add to project' solved the issue.

Harikrishnan
  • 7,765
  • 13
  • 62
  • 113
  • @Zindarod late answer I know but see http://stackoverflow.com/questions/17895557/add-commons-io-dependency-to-gradle-project-in-android-studio#comment61025941_33820307 for obtaining jar – CrandellWS Apr 19 '16 at 13:59
5

Adding below dependency solved issue

 implementation 'org.apache.directory.studio:org.apache.commons.io:2.4'
Tarun Anchala
  • 2,232
  • 16
  • 15
3

For those who encountered this while working on a Netbeans Java project. What I did is I downloaded the Binaries Zip of IO here

Commons IO Util Jar Download Here

Then Right clicked on my Project>Properties>


On the Categories Pane, Click Libraries


Then Click Add Jar/Folder


Locate the jar file/folder of the extracted IO Util that was downloaded.


Then click Ok. That fixed mine. :)

iamjoshua
  • 1,157
  • 3
  • 16
  • 33
  • The question is tagged with [tag:android-studio] an answer about Netbeans doesn't answer it. – Oleg Nov 03 '17 at 15:53
  • android uses close to java syntax anyway.& I was searching for this exact problem that I encountered while working in netbeans that used to be an IDE for developing in android,seems to be worth pointing out. The Step by step problems and necessary jar files provided above for android-studio is close to what will solve the same problem above if encountered in Netbeans. I'm just providing my version of it in netbeans since this could be encountered while working in netbeans too. Fact is, I am working on this java proj.in netbeans so that I could deploy it in android later with working code. – iamjoshua Nov 03 '17 at 16:13