set dynamic a image form bitmap - android

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]);

Related

Save RelativeLayout with content as an image

I have relativelayout (A template) where it contain textboxes whose content is populated at runtime.On clicking save button,I need to save the template as an image in SD card.Is it possible.
I referred the below link:
How do I convert a RelativeLayout with an Imageview and TextView to a PNG image?
But the image saved cannot be opened.
Is my requiremnet possible.Or else please advice how can I achieve it.
I am behind this for several days.I am new to ANdroid.Please help.
Thanks in Advance.
You can pass any view or layout to devBitmapFrmViewFnc function and get the bitmap. You can save the bitmap in jpeg using devImjFylFnc.
|==| Dev Bitmap Image from View :
Bitmap devBitmapFrmViewFnc(View viewVar)
{
viewVar.setDrawingCacheEnabled(true);
viewVar.buildDrawingCache();
return viewVar.getDrawingCache();
}
|==| Create a JPG File from Bitmap :
static void devImjFylFnc(String MobUrlVar, Bitmap BitmapVar)
{
try
{
FileOutputStream FylVar = new FileOutputStream(MobUrlVar);
BitmapVar.compress(Bitmap.CompressFormat.JPEG, 100, FylVar);
FylVar.close();
}
catch (Exception ErrVar) { ErrVar.printStackTrace(); }
}

Android: Loading images from the internal folder by using String ID

I'm new to android programming and have been trying to figure out how to
load an image (at startup) from the assets folder and save that image in a object (What kind of object should i save it into?)
So that when i want to display that object in an imageview i can just point to it and retreve an imageview displaying that image.
I tryed looking for similar posts but there was nothing specific enough for me to understand fully.
Is there any way to do this or is it a completely wrong approach to using images in android?
Thanks in advance
Here is the code you need. It gets the AssetManager, reads a bitmap from assets, and places the bitmap in an image view.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the AssetManager
AssetManager manager = getAssets();
// Read a Bitmap from Assets
try {
InputStream open = manager.open("icon.png");
Bitmap bitmap = BitmapFactory.decodeStream(open);
// Assign the bitmap to an ImageView in this layout
ImageView view = (ImageView) findViewById(R.id.ImageView01);
view.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
The onCreate method runs when the activity is created. The content view is set and then the AssetManager is retrieved. The AssetManager opens the image "icon".png and saves it as a bitmap. The bitmap can now be assigned to imageviews.

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

Can I save a image dynamically and use it in layouts?

I have a small query, is it possible to get an image dynamically every-time the app is launched over the internet and then that Image can be used to be shown on UI(via xml file)please let me know if this can be achieved in android and with any sample code.
Thanks & Regards.
In this case the best way to do it is to load image via HTTP GET, store it in Bitmap object and inject programmatically into ImageView element defined in layout.
You can't override resource image so you cant use #drawable.
Here you have loadBitmap method that loads image by url and stores it in Bitmap: How to load an ImageView by URL in Android?
Next thing is to inject it into ImageView:
ImageView image = (ImageView) findViewById(R.id.image);
Bitmap bitmap = loadBitmap("http://www.android.com/images/logo.png");
image.setImageBitmap(bitmap);
Not possible through XML.
I'd recommend having a placebo bitmap also in case there is no Internet access available at the time. Use AsyncTask when downloading.
new GetAndSetBitmap.execute(url)
private class GetAndSetBitmap extends AsyncTask<String, Void, Bitmap>
{
protected Bitmap doInBackground(String... urls)
{
Bitmap bitmap = loadBitmap(url[0]);
// Used the loadBitmap from other answer. Anything goes wrong
// load a local resource with the same dimensions.
return bitmap;
}
#Override
protected void onPostExecute(Bitmap bm)
{
yourImageView.setImageBitMap(bm);
}
}
Guys found a better solution
try {
ImageView i = (ImageView)findViewById(R.id.image);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Can't see image in ImageView android

I am trying to populate ListView Item with an object from MyClass. One of the property of the class is jpg image. I put my images in images/ folder. I use this code for populating
private static final String ASSETS_DIR = "images/";
String imgFilePath=ASSETS_DIR+r.resourceID;
try{
Bitmap bitmap = BitmapFactory.decodeFile(imgFilePath);
resourceIdView.setImageBitmap(bitmap);
}
catch(Exception e)
{
System.out.println(" Error");
}
r.resourceID is the name of the image for example "AUD.jpg"
resourceIDView is ImageView
The program don't get in the catch part, however I can't see the image
could somebody help me??
From your naming convention I conclude that you are storing your images in the "assets" folder. If yes, then you can use the following lines of code and get this issue resolved:
private static final String ASSETS_DIR = "images/";
String imgFilePath=ASSETS_DIR+r.resourceID;
try{
Drawable d = Drawable.createFromStream(getAssets().open(imgFilePath), null);
resourceIdView.setImageDrawable(d);
}
catch(Exception e)
{
System.out.println(" Error");
}
Hope this helps.
put your image in drawable folder and set so imageview...
resourceIdView.setImageResource(R.drawable.AUD);
You can try to put your images in drawable folder under /res. Use an ImageAdapterthat extend BaseAdapterto populate your ListView. You can use this code : http://www.java2s.com/Code/Android/2D-Graphics/extendsBaseAdaptertocreateImageadapter.htm
What I had was that the image was showing in Designer but not on device, so I put the image in all the drawable-xdpi directories. This worked for me.

Categories

Resources