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?
Related
final Bitmap b = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
I am getting a null references error.
B is null.
Why is that?
Good question i went in the exact same issue (with the exact same file :)
It seems that
BitmapFactory.decodeResource works quite perfect with R.drawable and R.raw but not with mipmaps probably caused by the different screen resolutions, but this is just a guess.
Looking in the source i can only see that decodeResource calls Resource#openRawResource(..) and the documentation says:
... asset file -- that is, it can be used to open drawable, sound, and
raw resources;it will fail on string and color resources.
i guess mipmap files are not considered as raw resources as there is additional handling for choosing density dependent resources.
So you will have to specify the density of the resource: https://stackoverflow.com/a/41910618/8524651
Or quick and dirty just move a copy of that file to your raw folder.
The solution code in kotlin (decide for one way ;)
val launcherDrawable = ResourcesCompat.getDrawableForDensity(context.resources, R.mipmap.ic_launcher, DisplayMetrics.DENSITY_LOW, context.getTheme());
var bm = BitmapFactory.decodeResource(context.resources, R.raw.ic_launcher)
bm = launcherDrawable!!.toBitmap(launcherDrawable.minimumWidth, launcherDrawable.minimumHeight)
This may help you
Drawable d = getResources().getDrawable(R.mipmap.imagefile);
Bitmap b = ((BitmapDrawable)d).getBitmap();
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)
I have an image view and a string src. I want to set the imageview source to the string src that I have, but am unable to do so beacuse the method expects an int:
imgview.setImageResource(int);
Since this method takes an int how can I accomplish my goal of using a string?
Each image has a resource-number, which is an integer. Pass this number to "setImageResource" and you should be ok.
Check this link for further information:
http://developer.android.com/guide/topics/resources/accessing-resources.html
e.g.:
imageView.setImageResource(R.drawable.myimage);
To set image cource in imageview you can use any of the following ways. First confirm your image is present in which format.
If you have image in the form of bitmap then use
imageview.setImageBitmap(bm);
If you have image in the form of drawable then use
imageview.setImageDrawable(drawable);
If you have image in your resource example if image is present in drawable folder then use
imageview.setImageResource(R.drawable.image);
If you have path of image then use
imageview.setImageURI(Uri.parse("pathofimage"));
What you are looking for is probably this:
ImageView myImageView;
myImageView = mDialog.findViewById(R.id.image_id);
String src = "imageFileName"
int drawableId = this.getResources().getIdentifier(src, "drawable", context.getPackageName())
popupImageView.setImageResource(drawableId);
Let me know if this was helpful :)
I am facing problem while setting the backGround Image of LinearLayout from asset folder.
String filePath="file:///android_asset/images/test.png";
Drawable d = Drawable.createFromPath(filePath);
frontTextView.setBackgroundDrawable(d);
Can someone help me out.
First you create a Drawable object from the asset file:
Drawable d = Drawable.createFromStream(getAssets().open(path_in_assets), null);
and then set it to some View that only supports Drawables as a background.
As far as I'm aware, you cannot access assets directly like you are trying to. You'll need to use the AssetManager class to get at your data if you want to store it as an asset. Here's a pretty decent blog post explaining a bit about resources and assets, though the official documentation is also a good resource, of course.
I'll also add, though, that things like background images are typically best stored in res/drawable and accessed using the R.drawable.* style (the blog post linked above also discusses this) whenever possible. It's not really clear why you need to do it this way from your provided code sample, though, so I suppose that's ultimately your call.
EDIT: added create image from InputStream...
I had the similar problem using ImageButton. I figured it out by loading bitmap from assets and using it as image for ImageButton. Probably not a good approach, but is working and solved my problem - unability to have subfolders in drawable dir and not allowed characters in file names.
(Yes, I can use prefix instead of subdir, and rename files to match the pattern (lowercase only and numbers) and I probably will do it later.)
InputStream is = null;
try {
is = this.getResources().getAssets().open("Images/Fruits/Apple.png");
} catch (IOException e) {
Log.w("EL", e);
}
Bitmap image = BitmapFactory.decodeStream(is);
ImageButton ib2 = (ImageButton) findViewById( R.id.imageButton2);
ib2.setImageBitmap( image);
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)