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 :)
Related
Firebase tells my app, an image name.
Example: test.jpg (as a String)
This image exists in my drawable directory.
My app should read the image name and search this image in the drawable directory and display it in an ImageView.
How can this be done?
This code:
int resId = context.getResources().getIdentifier(imagename, "drawable", context.getPackageName());
iv.setImageResource(resId);
sets to the ImageView with id iv the image with name (id) imagename (excluding .jpg) after finding its id in folder drawable.
If your code is in an activity, replace context with this.
Welcome to stackoverflow.
Try bellow code in your button click:
EditText editText=findViewById(R.id.yourEditText);
String text=editText.getText().toString();
int resourceId = getResources().getIdentifier(text, "drawable", this.getPackageName());
ImageView imageView=findViewById(R.id.yourImageView);
imageView.setImageResource(resourceId);
Now, if you type image name (without extension) in EditText and click the button, image will be shown.
is it possible to get the src Value of an ImageView as URI?
Help would be great
No. By the time the ImageView has the image, the source of that image is long gone.
If you know what image it is in your drawable, you can do something like this.
String path = Uri.parse("android.resource://my.package.name/" + R.drawable.myimage).toString();
More info - https://stackoverflow.com/a/28505621/2197529
I have to show a drawable from res into an ImageView. In this app, I'm using Picasso for some reasons.
In this case, I need to load the drawable using its URI and not its id.
To do that, here is my code:
uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+context.getPackageName()+"/drawable/" + drawableName);
where drawableName used here are file names rather than their resource ids.
Then
Picasso.with(context).load(uri).into(imageView);
I know for sure that drawable name is correct, but Picasso seems it does not like this uri.
If the images is in your drawable folder then you can just load it.
Picasso.with(context).load(R.drawable.drawableName).into(imageView);
and picasso will load it no need for an Uri.
Found the answer. Unfortunately, Picasso do not allow drawable loading via URI. It is an incoming feature.
This is if you don't want to hardcode the image that you are going
to load...
You can load local image files from your drawable folder lazily if you know the integer value of the image that you want to be loaded.
Then you can just do:
Picasso.with(getContext()).load(imageResourceId)
.error(R.drawable.ic_launcher)
.into(imageView);
Where
imageView
is the view you wish to display the image. For example:
imageView = (ImageView) convertView
.findViewById(R.id.itemImage);
And where
imageResourceId
is the integer value of the drawable. You can retrieve this integer value by:
int productImageId = resources.getIdentifier(
productImageName, "drawable", context.getPackageName());
as well as
productImageName
is the name of the drawable you want to draw (i.e. "ic_launcher")
THIS CAN ALL BE DONE INSIDE FROM THE ADAPTER
From picasso v2+ here is a big modification. The new version is very helpful in order to manage image cache data. It's using Singleton Instance.
GRADLE
implementation 'com.squareup.picasso:picasso:2.71828'
Set drawable image
Picasso.get()
.load(url)
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.into(imageView);
Bonus, get drawable by name:
public static int getDrawableIdFromFileName(Context context, String nameOfDrawable) {
return context.getResources().getIdentifier(nameOfDrawable, "drawable", context.getPackageName());
}
As mentioned in the documentation of Picasso .
they are now supporting loading Image from URI like the following :
load(android.net.Uri uri)
so you have to do something like the following :
Picasso.with(context).load(uri).into(imageView);
just like what you are doing already .
Hopethat helps .
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?
I want to retrieve an image from my data/data/com.apps.myapp/images folder and display it in an ImageView. Any clue?
Try this :
Bitmap bitmap = BitmapFactory.decodeFile("data/data/com.apps.myapp/images/img.png");
ImageView imgView = (ImageView) this.findViewById(R.id.imgViewId);
imgView.setImageBitmap(bitmap);
There are several components involved in this.
To get the path of your data folder you can use the method getDir in the Context.
Now you have to know the file name and open an stream here again the Context class is your friend. Now the stream can be decoded into a Bitmap via a Bitmap Factory.
After you got a Bitmap create a BitmapDrawable from it and pass it to your ImageView