I need set image resource by Send parameter (String src)
I have this code
public void getSource(View view , String src){
ImageView image = (ImageView)findViewById(R.id.img);
image.setImageURI(src);
}
How do I solve this problem ؟
// method to call to set image
private void setImage(int src) {
ImageView iv = findViewById(R.id.your_image_view);
iv.setImageResource(src);
}
//Pass your resource and use it like this :-
setImage(R.drawable.plus_active);
If you want to set image from local storage, then you have to get the uri of the image and use setImageUri like this:
public void getSource(View view , String srcImageUri){
ImageView image = (ImageView)findViewById(R.id.img);
image.setImageURI(Uri.parse(src));
}
If you have image in drawable folder , then you have to set image like this:
image.setImageResource(R.drawable.imageName);
And if you want to set image from web url, then you have to use libraries like picaso or fresco.
use getResources().getIdentifier(); (there you can pass the image name String as a parameter to getIdentifier() function) to get the resourceID of the named image and then use setImageResource(resourceID); to set the image to ImageView.
public void getSource(View view , String src){
int resID = getResources().getIdentifier( src, "drawable", Context.getPackageName());
ImageView image = (ImageView)findViewById(R.id.img);
image.setImageResource(resID);
}
you may need to pass the Context as a parameter to your getSource() function.
Related
like the title said, i really want to get a drawable name from my current image view
Imageview img = findViewById(R.id.IMG);
and it set with image from drawable named "img001.png"
and I have a button that will change the image to "img002.png", and when clicked again it should be changed the image again to "img001".
i use code like this
if (myimg == "img001"){
myimg= "img002"
}else if (myimg == "img002"){
myimg= "img001"}
it not work since "myimg" is null at start, so i want "myimg" to check which image is already in use.
i ve googled some info how to get resource name but all I got is how to get resource id from its name.
Get the image name like is explained here:
Android: How to get Drawable Image name
Then compare the name by using equals():
if("img001".equals(imageName)) {
...
}
I would suggest using custom tags instead of finding resource name, changing tag for that imageview and checking that in if-else condition instead of taking image names. When you apply resource to image view, assign that resource id as tag to image view.
if(imgView != null) {
imgView.setImageResource(resourceId_1);
imgView.setTag(resourceId_1);
imgView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(imgView.getTag != null) {
if(imgView.getTag() = resourceId_1) {
imgView.setImageResource(resourceId_2);
imgView.setTag(resourceId_2);
} else if(imgView.getTag() = resourceId_2) {
imgView.setImageResource(resourceId_1);
imgView.setTag(resourceId_1);
}
} else {
Log.v(TAG, "ImageView tag is null");
}
}
}
}
I'm relatively new to android and I'm working on a horizontal scroll view with drag and drop functionality. Right now everything is working perfectly fine I can drag the image and drop it in the drop zone with count of failed and successful drops. For now the image that is being dragged and its shadow looks the same. What I want is that when I drag an image the shadow that appears for that image is an image from drawables that I select.
Here is the code for shadow builder:
void dragAndDropImage(int imageId)
{
// int drawableid = getResources().getIdentifier("drawable/a1", "id", getPackageName());
final ImageView drag = (ImageView)findViewById(imageId);
drag.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent arg1) {
// TODO Auto-generated method stub
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadow = new View.DragShadowBuilder(drag);//I want the drawable id here View.DragShadowBuilder(drawableid)
v.startDrag(data, shadow, null, 0);
return false;
}
});
I want to get the id of one particular image from drawables and then I can use it in View.DragShadowBuilder(id of the image from drawables) to change the image of the shadow. Any help in this regard is appreciated.
It will be something like:
R.drawable.resourcename
Make sure you don't have the Android.R namespace imported as it can confuse Eclipse (if thats what you're using).
If that doesn't work, you can always use a context's getResources method ...
Drawable resImg = this.context.getResources().getDrawable(R.drawable.resource);
Where this.context is intialised as an Activity, Service or any other Context subclass.
Update:
If it's the name you want, the Resources class (returned by getResources()) has a getResourceName(int) method, and a getResourceTypeName(int)?
Update 2:
The Resources class has this method:
public int getIdentifier (String name, String defType, String defPackage)
Which returns the integer of the specified resource name, type & package.
(I have been seeing this post getting more views every time I log in and I solved this a long time ago but I'm going to post the solution in hopes that it helps someone else.)
In my case I had multiple images in Drawable that I wanted to use therefore
for (int j=1; j<10; j++)
{
setImages("drawable/img" +j);
// SetPopup(drag);
}
void setImages(final String draw)
{
int id12 = getResources().getIdentifier(draw, "id", "your_package_name");
drag1.setImageResource(id12);
I just posted the part of code that helped me access different images directly from drawables and use them as drag images on multiple imageViews.
String uri = "#drawable/yourdrawable";
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
Can you try this?
Try this,
final ImageView drag = (ImageView)findViewById(R.id.imageId);
drag.setImageResource(R.drawable.a1);
View.DragShadowBuilder shadow = new View.DragShadowBuilder(drag);
Assume you have several drawables in your Android resource folder and what to iterate over them. You use a certain naming schema and would like to use this to determine the resources ID’s.
The following steps shows how to get a resource ID based on the resource name:
// Name of resource
//Type
// Package of the app
int identifier = getResources().getIdentifier("pic1", "drawable", "android.demo");
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(identifier);
I'm trying to display a captured image from a camera intent, but when I call a showPhoto method the image isn't shown in the ImageView. There is no error output to the stack trace so I'm not sure how the issue can be debugged.
Does anyone know what is wrong with the method that its not showing the captured image?
The showPhoto method is as follows:
private void showPhoto(Uri photoUri) {
String filePath = photoUri.getEncodedPath();
File imageFile = new File(filePath);
//File imageFile = new File(photoUri.getPath());
if (imageFile.exists()){
Drawable oldDrawable = photoImage.getDrawable();
if (oldDrawable != null) {
((BitmapDrawable)oldDrawable).getBitmap().recycle();
}
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
BitmapDrawable drawable = new BitmapDrawable(this.getResources(), bitmap);
photoImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
photoImage.setImageDrawable(drawable);
}
}
This is the tutorial that I've followed: http://www.linux.com/learn/tutorials/722038-android-calling-the-camera
The device I'm testing on is JellyBean 4.1 and this is the link to the complete class:
http://hastebin.com/oqokupulol.java
Use BitmapOptions.inJustDecodeBounds to find out the dimension of the image first
Typically, Android won't load images larger than 4000px wide/tall
If you find out the image is too large, you can use BitmapOptions.inSampleSize to downscale the image so that Android would load it
And in fact, you can directly use ImageView.setImageBitmap(Bitmap) instead of setImageDrawable
i am beginner in android programing, and i created a Full Screen Image Slider app using this tutorial:
Link:
http://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/
in this tutorial it reads images from Sd Card but i want to read images from drawable folder, i don't want to access images from SD Card instead access images from drawable.
This is the way I've seen, I hope it is correct:
private Bitmap bitmap = null;
private void createBitmap(Context context)
{
Resources resources = context.getResources();
Drawable d = resources.getDrawable(R.drawable.imagename);
Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
this.bitmap = bitmap;
}
It can be done like this:
Drawable drawable = this.getResources().getDrawable(R.drawable.image);
where this refers to the context of the actual activity.
OR
you can just give the name of the image and then you can get the image using getIdentifier,
getResources().getIdentifier(name,"drawable", getPackageName());
Where name will be the name of your image i.e - "image1"
I am currently involved in a project about hotels and restaurants. My database contains hotel IDs(1,2,3,...) and I need to load the images(image1,image2,...) of the respective hotels/restaurants for which I have to give the image src through .java file.The code that I used is :
String icon="image" + h_id;
int resID = getResources().getIdentifier(icon, "drawable","testing.Image_Demo");
image.setImageResource(resID);
But, the problem is that the hotel image is not loading. I had gone through different questions in this site but the problem remains as it is. Does anyone have an idea to solve this? Thanks in advance!
I created the following sample activity, which works fine if I have a "image.png" image file in the drawable folder:
public class TestActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String icon = "image";
int resId = getResources().getIdentifier(icon, "drawable", getPackageName());
ImageView imgView = new ImageView(this);
imgView.setImageResource(resId);
setContentView(imgView);
}
}
Did you check that your package name is correct ?