Can I update an image in the drawable directory from code? - android

I have a picture in res/drawable directory: res/drawable/picture.jpeg.
Can I dynamically update this picture.jpeg from code? i.e. I want to use another picture to replace this picture in the drawable directory dynamically.
If I can, what path should I use to access the picture? Should I use "res/drawable/picture.jpeg"?
Thanks.

Can I dynamically update this
picture.jpeg from code?
No. Resources are part of the signed APK file and cannot be modified at runtime, other than by shipping an updated APK to your users.

Related

Why I cannot access the image in my drawable folder in android studio?

Here is the screenshot of my code. I want to access the image in my drawable folder and pass it to a class. However, I have put in an image name as asif.JPG in a drawable folder but I cannot access it using R.drawable.asif.
Do clean build and it will regenerate R.java class.

Image in android?

My ImageButton in my app is very tiny and cut off. I realized this could be because the image was in the drawable folder instead of the mitmap folders. So, I copied the images from the drawable folder, and pasted them in mitmap folders. Now, should I go about deleting the image from the drawable folder? I get the following saying it is not safe:
I know how to access the images, but should I delete them from the drawable folder now that I have already moved them to the mitmaps? If I do a delete anyway, will I need to alter my code to access the mitmap image? What is safest to do?
Here is an image of my mitmaps:
Thanks for the expert advice,
Rich
"Not safe" in this context just means that it's still being used somewhere. If you delete it, the current usage will fail to compile, and you'll be easily able to find and alter it to point at the new location.
You can safely delete this image, in Android Studio if you are using this image or else the file will be open at some place, you can get this error actually, so try deleting this image will safely remove the only selected image into your workspace. So hassle free to delete it.
After deleting your image, clear your project and run it, it works fine.
The mipmap folder is used for image that are very small and should be used in the action bar, and other menus. If you find your image too tiny, it just will be worst if you put it in the mipmap folder. If it's tiny although the image is in the drawable folder, maybe the image has a very small dimension or your imageview is too small.

Saving to drawable folder programmatically in Android

I'm looking to have a user select an image from the gallery, and have this file saved as a permanent drawable resource when they open the app at a later time. Is this possible?
I mean as an object in the actual drawable folder.
No, it's not possible. "The drawable folder" doesn't exist as a file system folder at runtime - it's part of your (read-only) binary .apk file.
You could instead save the image at internal folder and/or define a preference string.
Check this old answer https://stackoverflow.com/a/3374138/4618976

getting string image path placed in drawable folder

Is it possible to get the path of an image in string form in android like
res/drawable/image.png
Or is their any other way for getting string path of an image that is placed in drawable.
No, it is not.
Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
e.g. will give you the default launcher icon. It is not possible to get the exact path for an image , that is stored in drawable.
Why not? When you compile your app to an *.apk file, all resources (ok, except from them in /raw) are compiled as well. You can only acces them, using their R. id.
Solution? Not really, you could copy them to a location on the sd card for example. Now you know the location :)
its not possible to get path of drawable image directly , but you can get this by storing it in internal memory or sdcard of device.For this i have written blog.. check out the link
here
i am sure it will help you.

How to access res/drawable/"folder"

On my application I have many pictures which I have grouped in folder under res/drawable. But Android dosen't let me access them trough "R". Is there a way to access those folders.
This is the Code im using for.
ImageView iv = new ImageView(conext);
iv.setImageResource(R.drawable.**smiley.666**);
I marked the part which I can't access.
Thx in Advance
safari
No, Android does not allow subfolders under /res/drawable: Can the Android drawable directory contain subdirectories?
You can however, add all image files under /drawable and then access them programmatically via:
int drawableID = context.getResources().getIdentifier("drawableName", "drawable", getPackageName());
iv.setImageResource(drawableID);
Where drawableName is a name of the drawable file, i.e. myimage if image file is myimage.jpg.
The code above will get image resource with id R.drawable.drawableName.
R.drawable.xxx is just a reference that the Android SDK generates in your R.java file. You are trying to make a sub-folder in your res-folder. The SDK can't generate a reference to any of your sub-folders, you have to put it in the predefined folders drawable-hdpi, -ldpi and -mdpi.
If you dont know how this works. I'll sum up. Android runs on a lot of different devices with a lot of different screen resolutions. With these three folders, you can have the same picture in three resolutions and depending on the device you are running your app on, one of these three folders will be used. You can read more here:
http://developer.android.com/guide/practices/screens_support.html
You can't create folders in res/drawable the system doesn't recognize those.
you need to save your image first and then make a xml for them so you can access it through R.your_pics
context.getResources().getDrawable(R.drawable.progressbar1);
Android - Open resource from #drawable String

Categories

Resources