getDrawable from resources - android

I want retrieve the drawable from drawable folder by my own properties instead to use R.drawable
How can i achieve this ?
public void updateLanguageImg(String img)
{
Bitmap myBitmap = BitmapFactory.decodeResource(this.getResources(),R.drawable.);
// i want retrieve the img String here
ImageView imageV = (ImageView) findViewById(R.id.imageLangue);
imageV.setImageBitmap(myBitmap);
}
Thank you very much

If i understood properly the question, and according to the parameter received in the method, you want to call a resource by its string name, so if that's correct this is what you have to do:
"context.getResources().getIdentifier("bitmapName","folderName",this.class.getPackageName())"
where: folderName could be ("raw", "drawable" etc...)
This will return the identifierNumber which is what you have already, and you can use the method in stead, something like this:
int id = context.getResources().getIdentifier("bitmapName","folderName",this.class.getPackageName();
Bitmap myBitmap = BitmapFactory.decodeResource(this.getResources(), id);
Regards!

Related

Get image from drawable dynamically

I am working on a barcode reader app and I want to show product image when the barcode read, so I renamed photos with product barcode numbers and put drawables folder. Photo name is for ex 1234567, try to retrieve with barcodeNo. But I got error on barcodeNo. How can i do this ?
ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageResource(R.drawable.barcodeNo);
just pass your code in getImage method to findout your image from drawable folder.
iv.setImageResource(getImage(barcodeNo));
public int getImage(String imageName) {
int drawableResourceId = this.getResources().getIdentifier(imageName, "drawable", this.getPackageName());
return drawableResourceId;
}
String uri = "#drawable/myresource"; // where myresource (without the extension) is the file
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
imageview= (ImageView)findViewById(R.id.imageView);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);
OR
imageview.setImageResource(imageResource)
#Narendra Sorathiya answer is right. you have write this code for solving the NullPointerException, which you posted on screenshot:
ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageResource(getImage(barcodeNo));
.
.
.
.
public int getImage(String imageName) {
int drawableResourceId = this.getResources().getIdentifier(imageName, "drawable", this.getPackageName());
return drawableResourceId;
}

how to display images using filename in android

i am using imageAdapter class in which i have an array that stores drawables
public Integer[] mThumbIds = {
R.drawable.blue, R.drawable.floral,
R.drawable.bluefloral };
in first activity when user click on save button i have saved those images in android internal memory like data/ data/ com.myapp.color , i have get the file name, and file path too and passed it to imageAdapter class i just wanted to know that through this file name and path how can i save these images in to this array. because through this array i am displaying images in gridview.
If the image inside the internal or external storage you can load it by this:
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
If the image inside the drawable folder inside the apk you can use:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.drawable_name);
If the image inside the assets folder you can use:
InputStream inputStream = getAssets().open("image_name.jpg");
Drawable drawable = Drawable.createFromStream(inputStream, null);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageDrawable(drawable);
If the image inside drawable folder and you have only the name you can get the resource id by using:
int drawableResourceId = this.getResources().getIdentifier("image_name_without_extension", "drawable", this.getPackageName());
This is how you can get the image id's using their file names. Using this id you can load the image in image view.
If the image name is my_image this method would return id value associated with R.id.my_image
public static int getImageIDFromName(String imageName)
{
int imageID = 0;
if(imageName == null
|| imageName.equalsIgnoreCase(""))
{
return 0;
}
try
{
#SuppressWarnings("rawtypes")
Class res = R.drawable.class;
Field field = res.getField(imageName);
imageID = field.getInt(null);
}
catch(Exception e)
{
}
return imageID;
}

What should be the original ID of thumbnails in MediaStore.Images.Thumbnails methods?

I'm trying to get the thumbnails of some images but the documentation is very unclear about what the origId should be. I have a simple method that takes a file as parameter and returns a drawable for thumbnails like this:
Bitmap thumbnail = MediaStore.Images.Thumbnails.getThumbnail(mContext.getContentResolver(), Long.parseLong(Uri.fromFile(file).getLastPathSegment()), Thumbnails.MINI_KIND, null);
BitmapDrawable bd = new BitmapDrawable(mContext.getResources(), thumbnail);
return bd;
But I get a java.lang.NumberFormatException: Invalid long exception whenever getThumbnail is called.
So how should I obtain the correct origId for an image file?
Try this way (this worked for me):
Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);

Android get image path from drawable as string

Is there any way that I can get the image path from drawable folder in android as String. I need this because I implement my own viewflow where I'm showing the images by using their path on sd card, but I want to show a default image when there is no image to show.
Any ideas how to do that?
These all are ways:
String imageUri = "drawable://" + R.drawable.image;
Other ways I tested
Uri path = Uri.parse("android.resource://com.segf4ult.test/" + R.drawable.icon);
Uri otherPath = Uri.parse("android.resource://com.segf4ult.test/drawable/icon");
String path = path.toString();
String path = otherPath .toString();
based on the some of above replies i improvised it a bit
create this method and call it by passing your resource
Reusable Method
public String getURLForResource (int resourceId) {
//use BuildConfig.APPLICATION_ID instead of R.class.getPackage().getName() if both are not same
return Uri.parse("android.resource://"+R.class.getPackage().getName()+"/" +resourceId).toString();
}
Sample call
getURLForResource(R.drawable.personIcon)
complete example of loading image
String imageUrl = getURLForResource(R.drawable.personIcon);
// Load image
Glide.with(patientProfileImageView.getContext())
.load(imageUrl)
.into(patientProfileImageView);
you can move the function getURLForResource to a Util file and make it static so it can be reused
If you are planning to get the image from its path, it's better to use Assets instead of trying to figure out the path of the drawable folder.
InputStream stream = getAssets().open("image.png");
Drawable d = Drawable.createFromStream(stream, null);
I think you cannot get it as String but you can get it as int by get resource id:
int resId = this.getResources().getIdentifier("imageNameHere", "drawable", this.getPackageName());
First check whether the file exists in SDCard. If the file doesnot exists in SDcard then you can set image using setImageResource() methodand passing default image from drawable folder
Sample Code
File imageFile = new File(absolutepathOfImage);//absolutepathOfImage is absolute path of image including its name
if(!imageFile.exists()){//file doesnot exist in SDCard
imageview.setImageResource(R.drawable.defaultImage);//set default image from drawable folder
}

How do I get the initial url of an ImageView formed from a Bitmap

Initially I created a bitmap from an external URL by using a bitmapFactory. I then put the bitmap in an Image view, like:
ImageView mim = (ImageView) findViewById(R.id.moviepix);
String mi = "http://xxxxxxx.com/"+ result.movie_pix;
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(mi).getContent());
mim.setImageBitmap(bitmap);
Now I need to reverse engineer the url from the ImageView.
All help is appreciated.
You can't, but you can however store the URL for later use with ImageView.setTag()

Categories

Resources