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.
Related
In android, I need to load a set of images from res/drawable directory
I use the following code:
Field[] fields = R.drawable.class.getFields();
List<Picture> pictures = new ArrayList<>();
for (Field field : fields) {
String name = field.getName();
if (name.startsWith("img")) {
Picture picture = new Picture(name);
pictures.add(picture);
}
}
Among all files in 'drawable', it finds all files starting as a string 'img'.
For this code to work correctly, I have to manually change the names of image files by myself.
If I can get the extension of each resource in drawable (such as jpg, png, etc.), I don't need to change the file name like this.
Is there any way to achieve this?
I appreciate your help, thanks :D
I answer my question.
I moved all the image files into "asset/imgs"
and use the following code to load them
List<Picture> pictures = new ArrayList<>();
try {
String[] images = assetManager.list("imgs");
for (String name : images) {
Picture picture = new Picture(name);
pictures.add(picture);
}
} catch (IOException e) {
// you can print error or log.
throw new RuntimeException(e);
}
I'm developing an application that sets wallpapers from com.android.launcher3 package drawable resources. At some point I need to check if the wallpaper is set correctly so I can move on to other step.
After some research in SO and googling, I wasn't able to find any information about getting current wallpaper name.
Here is how I set the drawable which I have no problem:
try {
WallpaperManager wallpaper_manager = WallpaperManager.getInstance(m_context);
Resources res = m_context.getPackageManager().getResourcesForApplication("com.android.launcher3");
int drawable_id = res.getIdentifier(wallpaper_name, "drawable", "com.android.launcher3");
Drawable drawable = res.getDrawable(drawable_id, null);
if(drawable != null) {
wallpaper_manager.setBitmap(((BitmapDrawable)drawable).getBitmap());
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I can get the current wallpaper as drawable as well:
WallpaperManager wallpaper_manager = WallpaperManager.getInstance(m_context);
Drawable drawable = wallpaper_manager.getDrawable();
but I haven't managed to get current wallpaper name.
I need help.
Thanks in advance.
I would try this two options:
1. Using wallpaperManager
You shouold be able to get the information using this:
wallpaperManager.getWallpaperInfo();
This will return a WallpaperInfo object which contains all the data about the wallpaper.
More information
https://developer.android.com/reference/android/app/WallpaperManager.html
2. Getting the drawable file
You can also try to get the URI of the drawable like this:
String imageUri = "drawable://" + R.drawable.image;
And get the file name from there.
Hope it helps you.
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(); }
}
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 am converting my image to string and storing that string in sharedpreferences. then later on other activity I want to fetch that string convert back to bitmap and display it in image view. Also for precausion if nothing is fetched from sharedpreference I would like to set ic_launcher as default image in my ImageView.
THis is how i am trying to get above task done.
String pic = shared.getString("UserPic","");
Log.i("picstring-verifydetail" , "picstring : "+pic);
if (pic != null && pic != "") {
try {
userpic = ImageHelper.stringToImage(pic);
profilepic.setImageBitmap(userpic);
} catch (IOException e) {
Log.e("picsetting", e.toString());
e.printStackTrace();
}
}
else
{
Bitmap defaultImage = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
profilepic.setImageBitmap(defaultImage);
}
I have also stored some values like name and that are successfully fetched but string for image is not getting fetched from sharedpreferences. It is always going to else part and there again I am getting error : "Source not found" on profilepic.setImageBitmap(defaultImage);. I searched logcat but found no error.
Please help to achieve these 2 task.
Thanks & Regards,
Sourabh Gupta
I don't think what you are trying to do is a good idea.
Try to save the image in SD card or Internal Storage and just store the File path in SharedPreferences.
If you have these images stored in assets OR res folders. You can just store the image names into the SharedPreferences and later you can fetch the image names from it and display on the screen by fetching them from the path.