How to reference a File in raw folder in Android
Asked Answered
D

4

12

I just want to create a File object like this

File myImageFile = new File ("image1") ;

but it is giving me exception of FileNotFoundException
How can i reference a file inside my raw Folder

EDIT: Actually i wanted to do something like this

MultipartEntity multipartEntity= new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); multipartEntity.addPart("uploaded", new FileBody(new File("myimage")));

Denyse answered 17/3, 2012 at 18:1 Comment(3)
and where is your images folder?Mezzanine
@djaqeel Sorry raw folder not images folder (which is inside res folder).. also i tried to access images in drawable folder but getting the same errorDenyse
do you really need is as file, does input stream work for you?Wolbrom
H
4

here are 2 functions. one to read from RAW and one from the Assets

/**
 * Method to read in a text file placed in the res/raw directory of the
 * application. The method reads in all lines of the file sequentially.
 */

public static void readRaw(Context ctx,int res_id) {

    InputStream is = ctx.getResources().openRawResource(res_id);
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr, 8192); // 2nd arg is buffer
                                                        // size

    // More efficient (less readable) implementation of above is the
    // composite expression
    /*
     * BufferedReader br = new BufferedReader(new InputStreamReader(
     * this.getResources().openRawResource(R.raw.textfile)), 8192);
     */

    try {
        String test;
        while (true) {
            test = br.readLine();
            // readLine() returns null if no more lines in the file
            if (test == null)
                break;
        }
        isr.close();
        is.close();
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

and from Assets folder

/**
 * Read a file from assets
 * 
 * @return the string from assets
 */

public static String getQuestions(Context ctx,String file_name) {

    AssetManager assetManager = ctx.getAssets();
    ByteArrayOutputStream outputStream = null;
    InputStream inputStream = null;
    try {
        inputStream = assetManager.open(file_name);
        outputStream = new ByteArrayOutputStream();
        byte buf[] = new byte[1024];
        int len;
        try {
            while ((len = inputStream.read(buf)) != -1) {
                outputStream.write(buf, 0, len);
            }
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
        }
    } catch (IOException e) {
    }
    return outputStream.toString();

}
Harbinger answered 17/3, 2012 at 18:40 Comment(2)
Regarding "readRaw" method, when it gets to the end of the file, test variable becomes null and thus it becomes useless.Blaney
Can you somehow access the raw/assets without using a context?Acuff
I
9

Generally you access the files through getResources().openRawResource(R.id._your_id). If you absolutely need a File reference to it, one option is to copy it over to internal storage:

File file = new File(this.getFilesDir() + File.separator + "DefaultProperties.xml");
try {
        InputStream inputStream = resources.openRawResource(R.id._your_id);
        FileOutputStream fileOutputStream = new FileOutputStream(file);

        byte buf[]=new byte[1024];
        int len;
        while((len=inputStream.read(buf))>0) {
            fileOutputStream.write(buf,0,len);
        }

        fileOutputStream.close();
        inputStream.close();
    } catch (IOException e1) {}

Now you have a File that you can access anywhere you need it.

Ionic answered 17/3, 2012 at 18:19 Comment(1)
That helped me so much! Thank you! I needed the File to create Uri for an Intent.EXTRA_STREAM image held in raw folder.Hordein
H
4

here are 2 functions. one to read from RAW and one from the Assets

/**
 * Method to read in a text file placed in the res/raw directory of the
 * application. The method reads in all lines of the file sequentially.
 */

public static void readRaw(Context ctx,int res_id) {

    InputStream is = ctx.getResources().openRawResource(res_id);
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr, 8192); // 2nd arg is buffer
                                                        // size

    // More efficient (less readable) implementation of above is the
    // composite expression
    /*
     * BufferedReader br = new BufferedReader(new InputStreamReader(
     * this.getResources().openRawResource(R.raw.textfile)), 8192);
     */

    try {
        String test;
        while (true) {
            test = br.readLine();
            // readLine() returns null if no more lines in the file
            if (test == null)
                break;
        }
        isr.close();
        is.close();
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

and from Assets folder

/**
 * Read a file from assets
 * 
 * @return the string from assets
 */

public static String getQuestions(Context ctx,String file_name) {

    AssetManager assetManager = ctx.getAssets();
    ByteArrayOutputStream outputStream = null;
    InputStream inputStream = null;
    try {
        inputStream = assetManager.open(file_name);
        outputStream = new ByteArrayOutputStream();
        byte buf[] = new byte[1024];
        int len;
        try {
            while ((len = inputStream.read(buf)) != -1) {
                outputStream.write(buf, 0, len);
            }
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
        }
    } catch (IOException e) {
    }
    return outputStream.toString();

}
Harbinger answered 17/3, 2012 at 18:40 Comment(2)
Regarding "readRaw" method, when it gets to the end of the file, test variable becomes null and thus it becomes useless.Blaney
Can you somehow access the raw/assets without using a context?Acuff
C
2

You can open it as InputStream, I don't know if possible as a file:

int rid = resources.getIdentifier(packageName + ":raw/" + fileName, null, null);  
//get the file as a stream  
InputStrea is = resources.openRawResource(rid);  
Cowart answered 17/3, 2012 at 18:11 Comment(3)
if he knows the file name, using the R.raw.id is better option I would say.Wolbrom
@BorisStrandjev - I agree, but sometime people prefer to do it that way.Cowart
I have read in many places this is not advisable: harder to code and with a lot worse performance.Wolbrom
E
1

You can use InputStreamBody instead of FileBody so you can use it like this:

InputStream inputStream = resources.openRawResource(R.raw.yourresource);

MultipartEntity multipartEntity= new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("uploaded", new InputStreamBody(inputStream));
Enameling answered 17/3, 2012 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.