Desthstro Desthstro - 4 months ago 66
Android Question

How to save a pdf in assets folder

This is probably one of the dumbest questions that you guys have ever seen, but I need help with the assets folder. I want to save a .pdf file into it. My problem is I do not know how to save an assets folder - I think I did it right - but I have no clue to save a pdf there after!

To all who do not know what I mean

I want to produce something like this:

Capture

Answer

You cannot save anything in assets folder programmatically. For all the possibilities of data storage in Android Framework check this tutorial.

Although, you may put any file in assets folder manually before compile. Here is the code example of how to use it:

public void loadDataFromAsset() {
        // load text
        try {
            // get input stream for text
            InputStream is = getAssets().open("text.txt");
            // check size
            int size = is.available();
            // create buffer for IO
            byte[] buffer = new byte[size];
            // get data to buffer
            is.read(buffer);
            // close stream
            is.close();
            // set result to TextView
            mText.setText(new String(buffer));
        }
        catch (IOException ex) {
            return;
        }

        // load image
        try {
            // get input stream
            InputStream ims = getAssets().open("avatar.jpg");
            // load image as Drawable
            Drawable d = Drawable.createFromStream(ims, null);
            // set image to ImageView
            mImage.setImageDrawable(d);
        }
        catch(IOException ex) {
            return;
        }

    }
Comments