I have problem when I try to set an image to a ImageView browsed from assets folder. When I place the same image in the drawable folder and use imageView.setImageResource(R.id.image); it is larger and fits the screen better than the image browsed by the previous method. Is there a way to resize the image so that it fits the screen exactly the same way as usual.
Here is the layout file and the code in java.
AssetManager manager = getActivity().getAssets();
InputStream input = null;
try {
input = manager.open("images/" + mQuestion.getQImage() + ".png");
} catch (IOException e) {
e.printStackTrace();
}
Bitmap image = BitmapFactory.decodeStream(input);
ImageView qImageView = (ImageView) questionView.findViewById(R.id.ImageView_qImage);
qImageView.setImageBitmap(image);
<ImageView
android:id="#+id/ImageView_qImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="#dimen/questionTopBottomMargin"
android:background="#color/radioButtonStrokeColor"
android:contentDescription="#string/hello_world"
android:padding="1px"
android:scaleType="fitXY" />
When you place an image in drawable folder then the images are classified on density of screen while if you put images in assest folder then android need to calculate density.
If you want to use images from assest folder then this code will help you.
Options opts = new BitmapFactory.Options();
opts.inDensity = DisplayMetrics.DENSITY_HIGH;
drawable = Drawable.createFromResourceStream(context.getResources(), null, is, srcName, opts);
see here
Image loaded from assets folder comes in different size than res/drawable
Android: Accessing images from assets/drawable folders
If you place images in the drawables folder they will be scaled by some functions it they load them from resources. This will not happen with images in assets folder which you load yourself.
Have a look at android docs
You should always use images from the drawable folder
There are different drawable folders in android
(drawable-hdpi,drawable-mdpi,drawable-ldpi)
Android will decide which folder image should be drawn depending on
the phone requirements and resolution
Use 9-patch image for auto-scaling
When you load from assets, the "source" density is unknown, so this
autoscaling is not performed. Hence, there is difference.
You just define the imageView width and height in the xml layout then you should try this code it works perfectly for me
public Bitmap getImageBitmap(String imgName){
AssetManager mgr = getAssets();
InputStream inputStream = null;
try {
inputStream = mgr.open(imgName + ".png");
return BitmapFactory.decodeStream(inputStream);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = getImageBitmap(imageName);
imageView.setImageBitmap(bitmap);
Related
The image I want to do is get the shape of the imageview before it comes to the screen.
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"/>
I did it this way but the imageview does not appear because the height is wrap_content before the image is loaded. The image expands after being uploaded, but I want to get the image size without loading the image.
I assume that you are getting the image URL / path from the server. Like, http://......
Then, you will definitely get the height and the width of the image from its path / URL by converting it into the Bitmap.
Try this:
try {
URL url = new URL("http://....");// here put your string URL comes from server
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
int width = b.getWidth(); // here is the your image width
int height = b.getHeight(); // here is the your image height
} catch(IOException e) {
System.out.println(e);
}
I want to use a Image from the assets folder as my background image in android
but the only picture I can use is out oif the drawable folder
random = String.valueOf(util_random.random(1, 4));
String drawable = "/assets/images/img"+random ;
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundDrawable(drawable);
this.setContentView(ll);
You cannot pass a String in View.setBackgroundDrawable(Drawable).
Use BitmapFactory to create a Bitmap, then create a BitmapDrawable and use it as your background image.
I am using the following method to pull a PNG file from the assets folder in my Android application:
public static Bitmap getBitmapFromAssets(Context context, String fileName) {
try {
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open(fileName);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
catch(Exception ex) {
ex.printStackTrace();
return null;
}
}
I am then setting the source of an ImageView, in the item of a GridView, to that Bitmap.
Here is the layout XML in question:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/containingLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:background="#android:color/transparent"
android:padding="0dp"
android:layout_margin="0dp"
>
<ImageView
android:id="#+id/ivPackageIcon"
style="#style/smLargeGridItemPackageIconStyle"
/>
</LinearLayout>
And the style referred to in that XML is:
<style name="smLargeGridItemPackageIconStyle">
<item name="android:scaleType">fitXY</item>
<item name="android:layout_width">100dp</item>
<item name="android:layout_height">142dp</item>
<item name="android:layout_margin">5dp</item>
<item name="android:background">#android:color/transparent</item>
</style>
Here is the code that sets the source of the ImageView:
ImageView ivPackageIcon = (ImageView)containingView.findViewById(R.id.ivPackageIcon);
if(ivPackageIcon != null) {
Bitmap coverImage = getBitmapFromAssets(containingView.getContext(), "myimage.png");
ivPackageIcon.setImageBitmap(coverImage);
}
The PNG image has some transparent areas, but for some reason when the image is displayed in my GridView, the transparent areas come through as black.
To preempt some questions: No, the background of the Activity, ImageView, GridView, and GridView item are not black. As a matter of fact, no matter what the background colors are set to, the transparent parts of the image always come through as black.
Consider this, though...If I place the PNG image in the drawable folder and set the ImageView as follows, the transparency is perfect:
ivPackageIcon.setImageResource(R.drawable.myimage);
I'm pretty sure that I'm using the decodeStream(...) method incorrectly somehow, but I don't know what I'm doing wrong. I even modified my original method to set some options as shown here:
public static Bitmap getBitmapFromAssets(Context context, String fileName) {
try {
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open(fileName);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = true;
options.inPreferredConfig = Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
return bitmap;
}
catch(Exception ex) {
ex.printStackTrace();
return null;
}
}
But that gave me the same poor result.
Any ideas, anyone?
Thank you.
Instead this, try:
ImageView img=new ImageView(this);
Drawable d = Drawable.createFromStream(getAssets().open(path_in_assets), null);
img.setImageDrawable(d);
In case your image is made out of application resource and it is placed under res/drawable/ instead of one of the specific resolution folders (e.g. res/drawable-hdpi)
The android compiler is optimizing the PNG so the transparent pixels are made white (or black in case of GIFF files)
Resolution:
move your PNG file to res/drawable-hdpi or one of the other drawable folders, and you should be OK.
I am looking for the way to assign image src to image view control. I read few example and they says something src="#drawable\image" but didn't understand this, also I want to assign image src at runtime by java code also want to apply default image in XML.
If you want to display an image file on the phone, you can do this:
private ImageView mImageView;
mImageView = (ImageView) findViewById(R.id.imageViewId);
mImageView.setImageBitmap(BitmapFactory.decodeFile("pathToImageFile"));
If you want to display an image from your drawable resources, do this:
private ImageView mImageView;
mImageView = (ImageView) findViewById(R.id.imageViewId);
mImageView.setImageResource(R.drawable.imageFileId);
You'll find the drawable folder(s) in the project res folder. You can put your image files there.
You can set imageview in XML file like this :
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/imagep1" />
and you can define image view in android java file like :
ImageView imageView = (ImageView) findViewById(R.id.imageViewId);
and set Image with drawable like :
imageView.setImageResource(R.drawable.imageFileId);
and set image with your memory folder like :
File file = new File(SupportedClass.getString("pbg"));
if (file.exists()) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap selectDrawable = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
imageView.setImageBitmap(selectDrawable);
}
else
{
Toast.makeText(getApplicationContext(), "File not Exist", Toast.LENGTH_SHORT).show();
}
In res folder select the XML file in which you want to view your images,
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/imagep1" />
shoud be #drawable/image where image could have any extension like: image.png, image.xml, image.gif. Android will automatically create a reference in R class with its name, so you cannot have in any drawable folder image.png and image.gif.
Drag image from your hard drive to Drawable folder in your project and in code use it like this:
ImageView image;
image = (ImageView) findViewById(R.id.yourimageviewid);
image.setImageResource(R.drawable.imagename);
Is there a way to generate a drawable object from a layout?
In fact, I need a cropped part of my initial layout, and my idea is to transform the layout into a drawable object and then to crop drawable.
A simple version:
Bitmap snapshot = null;
Drawable drawable = null;
yourView.setDrawingCacheEnabled(true);
yourView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW); //Quality of the snpashot
try {
snapshot = Bitmap.createBitmap(yourView.getDrawingCache(), sizes and stuff); // You can tell how to crop the snapshot and whatever in this method
drawable = new BitmapDrawable(snapshot)
} finally {
yourView.setDrawingCacheEnabled(false);
}