I have been using the images in the drawable folder to display images In my Android Application. Is there any way to access the images outside the drawable folder? Or images in another through a url?
The BitmapFactory class has different methods allowing you to decode resources from streams or files, you should check it out.
Just use PackageManager:
getDrawable (String packageName, int resid, ApplicationInfo appInfo)
Related
I want to know how can i use nostra13 / Android-Universal-Image-Loader for displaying Images locally i.e from drawable folder along with the Memorycache. I want to use it with ViewPager.
any help will be greatly appreciated.
To load images from assets and drawables you should take ExtendedImageDownloader from example project (this class is not a part of library yet) and also set it to configuration.
UPD: Loading local resources (from drawable, assets, content provider) works out of the box since UIL v1.8.0.
See README:
String imageUri = "assets://image.png"; // from assets
String imageUri = "drawable://" + R.drawable.image; // from drawables (only images, non-9patch)
NOTE: Use drawable:// only if you really need it! Always consider the native way to load drawables — ImageView.setImageResource(...) instead of using of ImageLoader.
Whenever More than one image load from resource dynamically (#runtime) than prefer these one:
String imgUri = "drawable://" + getResources().getIdentifier(imgName, "drawable", getActivity().getPackageName());
Here, imgName = Name of image in resource
I'm using an API in my Android app that downloads images from the web and shows a placeholder animation while the images are downloading (RemoteImageView, part of the Ignition package here.
At some points in the app, I need to show images from the local resource drawables and not downloaded from the web.
Is it possible to access the local resource drawables using a URL ?
The way to do it:
use this line "android.resource://[your package]/"+[res id]
Here is an example
Uri myURI = Uri.parse("android.resource://com.example.project/" + R.drawable.myimage);
for convenient you can use it as a method:
public static Uri getUrl(int res){
return Uri.parse("android.resource://com.example.project/" + res);
}
Warning do not use context.getPackageName() as substitute to package name because it might return a null value.
Local image resources do not have urls, they have URIs. So if you have image in drawable, you can parse them from resource id to URI.
Uri uri=Uri.parse("R.drawable.image");
However, if you can also put your images in asset folder of the package and access them using their URL. The URL of the image files would be "file:///android_asset/image.png"
You can use either of the option.
I need to add images into res/drawable folder dynamically as the user select images of his choice from a server...then store the corresponding R.drawable.imageid to Database..so as to load the user chosen images on the next run...Is there no way to do so...?
int[] images = {
R.drawable.m1,R.drawable.m2, R.drawable.m3,
R.drawable.m4,R.drawable.m5, R.drawable.m6,
R.drawable.m7, R.drawable.m8,
R.drawable.m9
};
ImageView iv = (ImageView)findViewById(imageViews[next]);
iv.setImageResource(images[j]);
Here R.drawable.m1, R.drawable.m2, R.drawable.m3, R.drawable.m4, R.drawable.m5, R.drawable.m6, R.drawable.m7, R.drawable.m8, R.drawable.m9 should be ids of images chosen by user from the server
Quoting Android Engineer RomainGuy
You cannot write to res/drawable.
However you can look for other alternatives like external storage.
Links to consider are
android image save to res/drawable folder
How to convert a Drawable to a Bitmap?
Android: Image save to location
u cannot copy the images in drawable folder.
create folder in data folder and write images there and use bitmap class to map the image.
from the beginning I use
Drawable pic = getResources().getDrawable(R.drawable.android);
to get the drawable object while R.drawable.android means the ID of the picture I put in drawable folder.
However, I would like to get also the drawable object of the images which are stored in library.
do you have any suggestion
I hope you mean by stored in library mean some where in SD card, then use
Drawable pic = Drawable.createFromPath(pathName);
or to get from stream
Drawable pic = Drawable.createFromStream(is, srcName)
Bitmap picture = BitmapFactory.decodeResource(getResources(),R.drawable.phscale);
apart from above code any other ways to get the bitmap from drawables
If you get your png name as string, you could use the following:
int resID = getResources().getIdentifier("bug", "drawable", "org.anddev.android.testproject");
source
If you would like to have full control over resource bits, you'd better put it either in res/asset or res/raw folders and then get access to them as:
InputStream is=this.getResources().getAssets().open("drawable.png");
//
is=this.getResources().openRawResource(R.raw.myDrawable);
See th link
How to convert a Drawable to a Bitmap?