Android - Store inputstream in file

AndroidInputStream

Android Problem Overview


I am retrieveing an XML feed from a url and then parsing it. What I need to do is also store that internally to the phone so that when there is no internet connection it can parse the saved option rather than the live one.

The problem I am facing is that I can create the url object, use getInputStream to get the contents, but it will not let me save it.

URL url = null;
InputStream inputStreamReader = null;
XmlPullParser xpp = null;

url = new URL("http://*********");
inputStreamReader = getInputStream(url);

ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(getCacheDir(),"")+"cacheFileAppeal.srl"));

//--------------------------------------------------------
//This line is where it is erroring.
//--------------------------------------------------------
out.writeObject( inputStreamReader );
//--------------------------------------------------------
out.close();

Any ideas how I can go about saving the input stream so I can load it later.

Cheers

Android Solutions


Solution 1 - Android

Here it is, input is your inputStream. Then use same File (name) and FileInputStream to read the data in future.

try {
	File file = new File(getCacheDir(), "cacheFileAppeal.srl");
	try (OutputStream output = new FileOutputStream(file)) {
		byte[] buffer = new byte[4 * 1024]; // or other buffer size
		int read;

		while ((read = input.read(buffer)) != -1) {
			output.write(buffer, 0, read);
		}
		
		output.flush();
	}
} finally {
	input.close();
}

Solution 2 - Android

Simple Function

Try this simple function to neatly wrap it up in:

// Copy an InputStream to a File.
//
private void copyInputStreamToFile(InputStream in, File file) {
	OutputStream out = null;

	try {
		out = new FileOutputStream(file);
		byte[] buf = new byte[1024];
		int len;
		while((len=in.read(buf))>0){
			out.write(buf,0,len);
		}
	} 
    catch (Exception e) {
		e.printStackTrace();
	} 
    finally {
        // Ensure that the InputStreams are closed even if there's an exception.
		try {
			if ( out != null ) {
			    out.close();
			}
            
            // If you want to close the "in" InputStream yourself then remove this
            // from here but ensure that you close it yourself eventually.
			in.close();  
		}
		catch ( IOException e ) {
			e.printStackTrace();
		}
	}
}

Thanks to Jordan LaPrise and his answer.

Solution 3 - Android

Kotlin version (tested and no library needed):

fun copyStreamToFile(inputStream: InputStream, outputFile: File) {
    inputStream.use { input ->
        val outputStream = FileOutputStream(outputFile)
        outputStream.use { output ->
            val buffer = ByteArray(4 * 1024) // buffer size
            while (true) {
                val byteCount = input.read(buffer)
                if (byteCount < 0) break
                output.write(buffer, 0, byteCount)
            }
            output.flush()
        }
    }
}

We take advantage of use function which will automatically close both streams at the end.

The streams are closed down correctly even in case an exception occurs.

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html
https://kotlinlang.org/docs/tutorials/kotlin-for-py/scoped-resource-usage.html

Solution 4 - Android

A shorter version:

OutputStream out = new FileOutputStream(file);
fos.write(IOUtils.read(in));
out.close();
in.close();

Solution 5 - Android

Here is a solution which handles all the Exceptions and is based on the previous answers:

void writeStreamToFile(InputStream input, File file) {
    try {
        try (OutputStream output = new FileOutputStream(file)) {
            byte[] buffer = new byte[4 * 1024]; // or other buffer size
            int read;
            while ((read = input.read(buffer)) != -1) {
                output.write(buffer, 0, read);
            }
            output.flush();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Solution 6 - Android

  1. In your application's build.gradle file add under dependencies:
    implementation 'commons-io:commons-io:2.5'
  1. In your code:
import org.apache.commons.io.FileUtils;

// given you have a stream, e.g.
InputStream inputStream = getContext().getContentResolver().openInputStream(uri);

// you can now write it to a file with
FileUtils.copyToFile(inputStream, new File("myfile.txt"));

Solution 7 - Android

There's the way of IOUtils:

copy(InputStream input, OutputStream output)

The code of it is similar to this :

public static long copyStream(InputStream input, OutputStream output) throws IOException {
    long count = 0L;
    byte[] buffer = new byte[4096]; 
    for (int n; -1 != (n = input.read(buffer)); count += (long) n)
        output.write(buffer, 0, n);
    return count;
}

Solution 8 - Android

You can using Google Guava

 import com.google.common.io.ByteStreams;

Code:

 try (FileOutputStream fos = new FileOutputStream(new File("C:\\example.txt"))){
      ByteStreams.copy(inputStream, fos)
 }

Solution 9 - Android

Modern Kotlin way

fun File.copyInputStreamToFile(inputStream: InputStream?) {
    outputStream().use { fileOut ->
        inputStream?.copyTo(fileOut)
    }
}

// Sample of usage
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        data?.data?.let { uri ->
            val inputStream = contentResolver.openInputStream(uri)
            val file = File(cacheDir, "todo_filename.jpg")
            file.copyInputStreamToFile(inputStream)
        }
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDobesView Question on Stackoverflow
Solution 1 - AndroidVolodymyr LykhonisView Answer on Stackoverflow
Solution 2 - AndroidJoshua PinterView Answer on Stackoverflow
Solution 3 - AndroidvovahostView Answer on Stackoverflow
Solution 4 - AndroidTuan ChauView Answer on Stackoverflow
Solution 5 - AndroidvovahostView Answer on Stackoverflow
Solution 6 - AndroidccpizzaView Answer on Stackoverflow
Solution 7 - Androidandroid developerView Answer on Stackoverflow
Solution 8 - AndroidQiuyan SuView Answer on Stackoverflow
Solution 9 - AndroidZakhar RodionovView Answer on Stackoverflow