Saving to drawable folder programmatically in Android - 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

Related

Android XML says image is not found in drawable folder?

I have this code:
android:drawable="#pickuptools.png"
the picture is called pickuptools.png and is inside the folder drawable-xhdpi.
I have tried #drawable-xhdpi/pickuptools.png, pickuptools without the file extension, and many more.
I can't find the picture through the XML.
The XML is located inside the drawable-xhdpi folder.
What am I doing wrong?
Use:
android:drawable="#drawable/picuptools"
You don't need to use #drawable-xhdpi or something. For any resources , you should use resource type(In your case its drawable) and name of the resource.
For further reference, check out this Android page on how to access Resources.

How to set icon for a file?

I am creating different types of Files. For example File extension as.txt, .doc, .xml, .java, .jpg, .png, and etc. How to set the icon to be matched with the files. For example i am creating one pdf file. How to set pdf icon to that pdf file.
You dont have to explicitly set icons for the files, unless you are creating file with your own extension.
The operating system will understand the file type and binds the icon accordingly.
That is why in windows, if you manually change a file's extension, the icon changes automatically. Same case applies to Android also.
If you want to create different files via your android app & want to show the files list along with icon as we can see it in Windows OS, then you have to put all different images of each file extension in drawable folder. example- pdfImage.png, jpgImage.png, pngImage.png etc. Now you have to process the file name to determine the file extension runtime. then update your iconView (an ImageView item) like:
iconView.setImageBitmap(bitmap); // bitmap is the Bitmap image
or
iconView.setBackgroundResource(R.drawable.pdfImage_icon);

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

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

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.

Categories

Resources