0

I'm new in android programming and I have a problem.

I can't find how to create and store data in file.

I need to create a file (non text file) that will store array of strings (example: Array{Nick, James, Director, 005001235NICK JAMES}.

I know how this work in delphi and I could do it in few minutes, but with android I'm not familiar jet. Can anyone help me?

Thank you and best regards.

2
  • what do you mean by (non text file) Commented Feb 9, 2016 at 18:20
  • Is there a reason you don't want to use a text file to store your arrays? If you really want to store your arrays in a binary file format, you could look into using a container class that implements the Java Serializable interface. With this you can write your classes to file, and read a file into your container/array class. The Android page on the serializable interface recommends the use of json text files. developer.android.com/reference/java/io/Serializable.html Commented Feb 9, 2016 at 18:30

1 Answer 1

4

You might find org.apache.commons.io.FileUtils and java.io.File useful. Before any writing is possible, your manifest must include an appropriate permission decleration. To write to the external storage, use:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Here is a sample code that uses java.io.File to write a bitmap to file:

private void writeBitmapToFile(Bitmap bmp, File file) {
    try (final FileOutputStream out = new FileOutputStream(file)) {
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.