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
Related
Ok, after searching and browsing over the internet, I just didn't get what I was looking for. I have a database with a field for "Path", where my images file names are listed.
**Table1:**
PATH ID
imgRice.jpg 1
imgMango.jpg 2
imgBanana.jpg 3
Now I want my imageView to change picture accordingly from my query:
"Select Path from table1 where ID = 1";
I will declare a String, like String queryResultString to handle the result (ex.imgRice.jpg).
I want a code that looks like imgView.image="drawable/" + queryResultString
Any help is appreciated. Thanks guys!
Thanks for the comments and suggestions guys, highly appreciated.
But I've got a simple yet tricky turnaround for my problem.
First, it is IMPOSSIBLE to rip the Drawable path because it is compressed when published to .apk file.
Second, to make it possible, you need to use id for every file in your drawable.
Third, it is much better to add files(images) to asset folder(\app\src\main\assets).
Lastly, follow these simple codes:
//code to initiate the object ImageView...
ImageView myImage = (ImageView) findViewById(R.id.pic);//pic is the id of ImageView in my xml file...
try
{
// now, get the input stream
InputStream input= getAssets().open(c.getString(c.getColumnIndex("image")));//image is the name of field in my db which holds the path (ex. rice.jpg)...
// initiate image as drawable
Drawable draw = Drawable.createFromStream(input, null);
// set image to ImageView
myImage.setImageDrawable(draw);
}
catch(IOException ex)
{
return;
}
Enjoy Coding! ^_^
Is there any good way to use frame by frame animation AnimationDrawable using UIL library. AnimationDrawable accept only BitmpaDrawable . Is there any way to convert quickly to bitmaps my images or any maybe there is method like imageLoader.getBitmap, I haven't find anything like that.
Please help , I would be very grateful for any help .
Edit I have my images in assets folder. Maybe there any way to get bitmap from cache or something else . I need open new activity , maybe several times . I need to show animation from files , but if I use decode it takes a lot of time to decode them . Please suggest something
If your images are resources you can use this for obtain Bitmap:
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon_resource);
else, if you want donwload the image you can use this:
String name = c.getString(url);
URL urlAux = new URL(name);
ImageView prueba = (ImageView)v.findViewById(R.id.iv_prueba);
if (prueba != null) {
Bitmap bitmap =
BitmapFactory.decodeStream(urlAux.openConnection().getInputStream());
prueba.setImageBitmap(bitmap);
}
Hope it helps you.
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 .
i developing an application, in that i configure html data . But the background image for data is not effected means the image path is not getting.
i placed my image :res/drawable-hdpi folder.
and i am using the following logic:
strBody += "<tr><td background='../res/drawable-hdpi/msgblue_box.png' style='word-break:break-all;width:50%;'>" + dslist.get(i).getBody()+"<br>"+ dslist.get(i).getDateformat()+ "</td><td style='width:50%'></td></tr>";
So, please guide me how solve this.
you need to use the getResources.getDrawable to get image in your code
ImageView img=new ImageView(this);
Drawable im= getResources().getDrawable(R.drawable.name);
img.setImageDrawable(im);
strBody += "<tr><td background="+img+ "style='word-break:break-all;width:50%;'>" + dslist.get(i).getBody()+"<br>"+ dslist.get(i).getDateformat()+ "</td><td style='width:50%'></td></tr>";
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 :)