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;
}
Related
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);
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;
}
I have a question. I am new to Android
my program set a image in grid view . get name from list and go to assets folder and show picture. I want show picture from drawable but i cant. my class is extends ArrayAdapter
ImageView imageView = (ImageView) view.findViewById(R.id.imageView1);
try { BitmapFactory.decodeStream(context.getAssets().open(items.get(position).uri));
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
i am need get name pic from list and go to drawable and show in imageview
thanks
You need to create an Integer array of drawables like this:
private Integer[] drawbleArray = {R.drawable.image1, R.drawable.image2, R.drawable.image3};
Pass this array to your adapter as a argument in constructor and you can set image like this:
imageView.setImageResource(drawbleArray[i]);
i have saved drawable images in internal storage by compressing them in bitmap format. the problem is that i want to retrieve those image files and wants to display them in grid view i am able to list the total no of files but i am not able to display those files in image view. here is my code
Intent i = getIntent();
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.SingleView);
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
File[] imageList = directory.listFiles();
if(imageList == null){
imageList = new File[0];
}
Log.i("My","ImageList Size = "+imageList.length);
imageView.setImageResource(imageAdapter. (....?) );
set Image(From Sd card) to imageview get the path of the image which is in sdcard. and you can assign it by creating Bitmap
String filePath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + File.separator + "your_image_name.png";
Bitmap bmp = BitmapFactory.decodeFile(filePath);
imageView.setImageBitmap(bmp);
Another way to set imageview you can also use third party library also which may have resize and Scaling options too. like ImageLoader
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!