Resources.Openrawresource() Issue Android

Resources.openRawResource() issue Android

Yes, you should be able to use openRawResource to copy a binary across from your raw resource folder to the device.

Based on the example code in the API demos (content/ReadAsset), you should be able to use a variation of the following code snippet to read the db file data.

InputStream ins = getResources().openRawResource(R.raw.my_db_file);
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
int size = 0;
// Read the entire resource into a local byte buffer.
byte[] buffer = new byte[1024];
while((size=ins.read(buffer,0,1024))>=0){
outputStream.write(buffer,0,size);
}
ins.close();
buffer=outputStream.toByteArray();

A copy of your file should now exist in buffer, so you can use a FileOutputStream to save the buffer to a new file.

FileOutputStream fos = new FileOutputStream("mycopy.db");
fos.write(buffer);
fos.close();

Android openRawResource() not working for a drawable

@Broatian I don't currently have a res/raw folder. I found an alternative solution:
is = context.getResources().openRawResource(+ R.drawable.image1);
The + shows additional folders. Thanks for the help!

openRawResource() throws always a NotFoundException

The problem is that you are using the system level resources with the call to Resources.getSystem. Specifically the documentation states that this does not provide access to application resources. You need to call getResources on a Context object in your application - all your activities are a Context object since they inherit from Context.

Error in getResources().openRawResource when it's not used in MainActivity

Do not create random subclasses of Activity, such as your GetResources class.

Do not create instances of an Activity yourself, as you are doing with your GetResources class.

Either:

  • Move readData() into MainActivity, or

  • Have GetResources extend nothing, change readData() to public List<VDienas> readData(Context ctxt), call getResources() on that ctxt parameter, and pass this when you create the GetResources instance (new GetResources(this))

getResources() is a method on Context. Activity extends from Context.

How to get access to raw resources that I put in res folder?

InputStream raw = context.getAssets().open("filename.ext");

Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));

Android: mock Resources.openRawResource(int)

Assuming your able to set the Context of the code you're testing, you can do this by creating the following two classes (I tend to make them private classes within my test class because they contain test specific information):

private class mMockContext extends MockContext {

@Override
public Resources getResources() {
return new mMockResources();
}
}

private class mMockResources extends MockResources {

@Override
public InputStream openRawResource(int id) {
String fileContents = "line one\n" +
"line two\n" +
"line three";

return new ByteArrayInputStream(fileContents.getBytes());
}
}

Then, when you're creating your class to be tested, pass it the mMockContext class in place of the Context.

Why am I getting this error Expected resource of type raw in Android Studio?

The error occurred because Android Studio expected a resource file of type raw.

Solution 1:

Create a new folder in your "res" folder called "raw", and put your icon there. The raw folder should contain all your media files of your app.

Then replace

InputStream is = getResources().openRawResource(R.drawable.icon);

with

InputStream is = getResources().openRawResource(R.raw.icon);


Solution 2:

Another solution is to do it like this. This doesn't require you to create a raw folder:

InputStream is = getResources().openRawResource(+ R.drawable.icon);

Android read text raw resource file

What if you use a character-based BufferedReader instead of byte-based InputStream?

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = reader.readLine();
while (line != null) {
...
line = reader.readLine();
}

Don't forget that readLine() skips the new-lines!



Related Topics



Leave a reply



Submit