Get image from drawable dynamically - android

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;
}

Related

Loading BitmapDrawable in Fresco

So i basically have method which converts one image with some shape and than makes Drawable from my bitmap. Now there was method load bitmap drawable (setImageDrawable) but its deprecated.
So code is like this:
public static Drawable getDrawableFromName(String name, Activity activity) {
int resourceId = activity.getResources().getIdentifier(name, "drawable", activity.getPackageName());
if(resourceId == 0) {
return null;
} else {
Bitmap croppedIcon = cropImage(activity, resourceId);
if(croppedIcon == null)
return null;
return new BitmapDrawable(activity.getResources(), croppedIcon);
}
}
cropImage return bitmap image with some custom shape.
How should i now load this drawable, which is actually nowhere located only in memory (as its generated) to Fresco SimpleDraweeView. Is it somehow possible to have this as Uri resource?
Uri uri = new Uri.Builder()
.scheme(UriUtil.LOCAL_RESOURCE_SCHEME) // "res"
.path(String.valueOf(resId))
.build();
simpleDraweeView.setImageURI(uri);

Convert drawable picture in Bitmap Android

How do I convert in Bitmap a droawable resource that I have in form of String as it follows
android.resource://com.example.myapp/drawable/name_picture
Bitmap bitmap= BitmapFactory.decodeResource(context.getResources(),R.drawable.myphoto);
Try the following(there maybe typos):
String path = "android.resource://com.example.myapp/drawable/name_picture";
String[] splitByPath = path.split("drawable/");
if(splitByPath!=null && splitByPath.length >1){
int varId = getResources().getIdentifier(splitByPath[1], "drawable", getPackageName());
Bitmap bmp = BitmapFactory.decodeResource(getResources(),varId);
}else{
//Improper path
}
Uri uriString = Uri.parse("android.resource://com.example.myapp/drawable/name_picture
");
String lastPathIcon = uriString.getLastPathSegment();
int drawableResource = getApplicationContext().getResources()
.getIdentifier("drawable/" + lastPathIcon, null, getApplicationContext().getPackageName());
largeIcon = BitmapFactory.decodeResource(getResources(), drawableResource);
Here the solution that worked for me.

setImageBitmap gets Syntax Errors

I am trying to take a Bitmap and display it in an ImageView.
ImageView iv = new ImageView(this);
Bitmap bMap = BitmapFactory.decodeFile("/res/drawable/" + imageFileName);
iv.setImageBitmap(bMap);
That's my code for it. I create an ImageView and a Bitmap. I want to display my Bitmap in my ImageView. But I always get these two errors on the iv.setImageBitmap(bMap); statment
Syntax error on token "bMap", VariableDeclaratorId expected after this token
Syntax error on token(s), misplaced construct(s)
Does anybody has an idea why this happens and what i have to change?
In OP's pastebin code I've found the following snippet:
String imageFileName =getIntent().getStringExtra("imageFileName");
//String bildquelle = "R.drawable." + imageFileName;
public class ImageChange extends Activity {
ImageView iv = new ImageView(this);
String bildquelle = "R.drawable." + imageFileName;
int id = getResources().getIdentifier(bildquelle, "drawable", getPackageName());
Bitmap bMap = BitmapFactory.decodeResource(getResources(), id);
//Bitmap bMap = BitmapFactory.decodeFile("/res/drawable/" + imageFileName);
iv.setImageBitmap(bMap);
}
There are a couple of problems with this including:
The code is not inside a method body.
The code is a Activity subclass.
The code improperly calls getResources().getIdentifier(...).
Instead of the above code I suggest MERGING the rest of activity with the following:
public class BildAnzeigen extends ActionBarActivity {
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bild_anzeigen);
iv = (ImageView) findViewById(R.id.my_image_view); // replace the id with the one from layout definition
handleIntent(getIntent());
}
private void handleIntent(Intent intent) {
String imageFileName = intent.getStringExtra("imageFileName");
int id = getResources().getIdentifier(imageFileName, "drawable", null);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), id);
iv.setImageBitmap(bMap);
}
}
Note: This will not work if the images are not compiled within your apk. There's no way to add anything to drawables after building. It just doesn't work that way.

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;
}

getDrawable from resources

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!

Categories

Resources