context.getResources().openRawResource() crashes the app - android

I'm trying for a while to make android read from a text file that I already have in the R.raw folder.
I have the function void readfile(Context context) that contains the line
InputStream is = context.getResources().openRawResource(R.raw.data);
However when I try to run the app it crashes at that exact line. Can you tell me a workaround? Or another way to read from that file?

There is obviously a problem with the given context. You should post your logcat.
There is 2 ways to solve your problem.
Pass a correct context: see what is wrong with your program and why the given context is not correct.
Extend Application, and save the application context in a static variable: see this post

Related

PreferenceManager(context) is private in the pacakage

PreferenceManager(context) is private in the package and I need to convert it to public to use. I don't know how to do so. So, can anyone help me ?
First of all, if you have an issue, you should post the actual code that's causing it.
But specifically with PreferenceManager, you can't invoke it the way you posted. If you want to instantiate it, you would need to do something like this:
String mPrefManager = mContext.getPreferenceManager()
where mContext would be the actual Context that you're trying to use

Access to a ressource in Android application

I have tried to use the gpuimage for Android. the issue is not related to gpuimage but it happens at this moment. I'm trying to build an image filter but just using an image as overlay on the original image.
GPUImageColorDodgeBlendFilter tmp = new GPUImageColorDodgeBlendFilter();
tmp.setBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.hipster));
My issue is that I need to access to the file called hipster.png located in res/drawable/hipster.png
I would like to avoid using decodeResource because this image will be always the same and I'm want to avoid using context as the filter class is builtin inside a lib aar and I reuse it in my app.
Any idea ?
I have tried to use the context coming from an activity but it's crashing and I think that context is useless in that case
Thanks
Hi Seb I will tell you how I do it. The only difference is that I use the context, but I pass it as a parameter in certain parts of my code and I don't have any problem.
getResources().getIdentifier(<nameOfImage>, "drawable", getPackageName())
Hope it helps!

How to properly reference the Strings.xml file, android

My string.xml has a string:
<string name="loginLocation">http://website.com/afile.php</string>
In my java file, I am trying to reference it like so:
url = new URL(R.string.loginLocation);
except I am getting the error:
The constructor URL(int) is undefined
I managed to get the error to go away by doing:
url = new URL(Integer.toString(R.string.loginLocation));
except when I make the call to it, I get a Protocol Error
I can do:
url = new URL("http://website.com/afile.php");
and it works fine, but I'd like to define it in the Strings.xml file. Any help is appreciated, thanks!
If you're trying to do it in a method of the Activity subclass, then do the following:
url = new URL(getString(R.string.loginLocation));
getResources() will give you a lot of methods for accessing what's in your /res directory. Among the rest, the method you are looking for is getString(R.string....), which is also available directly from your Context (without the Resources object).
As simple as this:
this.getxt(R.string.btn_menu_logout);
What nobody mentions online is to put .this before gettxt() or getText():

Problems accessing my strings.xml items… i got numbers and not the string value

i found this problem some time ago, but i solve it using this: getString(), or this: getResources().getString()
but now, for this case, it doesn't works, i think it's because i need to get the string values on a NON ANDROID ACTIVITY CLASS. I need the resource values on a remote connection class, that doesn't extends any kind of activity or service.
how i can acces to the variables from my strings.xml on this normal class?
this is the code where i get the error (it gets an integer, and not the string value)
String a =R.string.totalpermission;
Take a look at these two answers (are the same XD):
How to obtain AssetManager without reference to Context?
How can I get a resource content from a static context?
Just an advice: try to read some basic concepts... it seems you don't understand what the R class is and how to use it. Trust me, you waste less time studying than trying to figure out how things work.
I'll add something to existing answers since I found it very useful.
To get your strings you have to use a Context. Your activity will work just great.
String string = getString(R.string.myString);
But if you have something more complex... for exemple
R.string.result -> "You %1$s %2$d cats"
String result = getString(R.string.result, killed ? "killed": "saved", count);
That would give you a result like that:
You saved 10 cats or You killed 2 cats... and so on. You can pass parameters and positional arguments in strings will get replaced by your arguments in getString.
All Android resources are referenced via a resource ID, like R.string.totalpermission. You can see those numbers in R.java (although there's no reason to ever do that).
In cases of strings, you can easily get those using Context.getString. Bonus: You can even pass optional arguments and add dynamic strings that way. You always have a context - how are you getting called? If you really don't have a context, you can create one for the package your resources are in.

Cannot get resource from android application

I am building an Android app, and I have a problem to get resource from string.xml.
I need to have a URL in String.xml that will be used several times through the app.
I tried Resources.getText("my_url"), but this does not work. Eclipse complains when I use this.
Do you think this is the best way to do ?
What you probably want is:
String myUrl = getString(R.string.my_url);
The getString() method is in Context which means that it's available directly in your Activity class since Activity is a subclass of Context. (The getString() method is also in Resources but it's easier to call on it directly on your Activity.)
What happens with your XML resources is that each is given a unique integer ID and this is added to the automatically generated R class as a public static final int. You then use these IDs to reference the resources. Have a look in the gen folder in your Eclipse project and you'll find the R class in there.
Do you ever refer this page: https://developer.android.com/guide/topics/resources/available-resources.html ?
If you want to retrieve the String represented by a resource ID, you can call the Context.getString() method.
Or, you have to post Eclipse's complains.

Categories

Resources