Adding images from assets folder in adapter - android

I'm trying to display images from assets folder. I have this error:
Bitmap bitmap = getBitmapFormatAssets(product.getProductId());
try { Bitmap bitmap = getBitmap(product.getProductId()); imageView.setImageResource(bitmap);

It must be setImageBitmap(bitmap) not setImageResource(bitmap)

Instead of imageView.setImageResource(bitmap) try to use imageView.setImageBitmap(bitmap) directly.

when you want to set a bitmap image to a image view don't use setImageResource(bitmap)
use setImageBitmap(bitmap) as below
setImageBitmap(bitmap) Sets a Bitmap as the content of this ImageView.
like this
imageView.setImageBitmap(bitmap)
get your bitmap from assets using below code
private Bitmap getBitmapFromAsset(String strName)
{
AssetManager assetManager = getAssets();
InputStream istr = null;
try {
istr = assetManager.open(strName);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(istr);
return bitmap;
}

following are used for getting the image from asset folder and set it to ImageView.
// load image
try {
// get input stream
InputStream ims = getAssets().open("avatar.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
mImage.setImageDrawable(d);
}
catch(IOException ex) {
return;
}

Related

Load Images from Assets in Widget remoteView

I am using the following code to load my image into Android widget ImageView:
remoteViews.setImageViewResource(R.id.widget_image, R.drawable.image);
I have to save the image in drawable folder. Is it possible to save the image in assets instead and load it to remoteView?
Using BitmapFactory, you can load an image in assets/ as a Bitmap from an InputStream provided by AssetManager, and then use the RemoteViews#setImageViewBitmap() method to set it on your widget.
For example:
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
...
try {
InputStream is = context.getAssets().open("image.jpg");
Bitmap bmp = BitmapFactory.decodeStream(is);
remoteViews.setImageViewBitmap(R.id.widget_image, bmp);
}
catch (IOException e) {
e.printStackTrace();
}
Here is the solution :
imageView = (ImageView)findViewById(R.id.iv);
try {
// get input stream
InputStream ims = getAssets().open("download.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
imageView.setImageDrawable(d);
}
catch(IOException ex) {
return;
}

Add local image in ImageView

I read from XML.
<movie>
<id>1</id>
<title>Fast</title>
<background>assets://icon/photography1</background>
</movie>
Now I have gotten Class Movie.
How can I add assets://icon/photography1 in my ImageView?
You can add an image from assests to an image view programmatically through java code in android. Code for that is given below:
ImageView image = (ImageView) findViewById(R.id.imageView1);
AssetManager assetManager = getAssets();
InputStream istr;
try {
istr = assetManager.open("vishal.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(istr);
image.setImageBitmap(bitmap);
istr.close();
} catch (IOException e) {
e.printStackTrace();
}

Load images from assets folder ( error loading *.png )

I wrote the code loader images from assets folder following the example code
my code
I don't know why load *.png not work. JPG work.
JPG work
Bitmap bitmap = decodeStreamFromAssets("test.jpg", 64, 64);
if(bitmap != null){
imageViewTest.setImageBitmap(bitmap);
}
else {
Logs.e("error");
}
PNG not work ( is error )
Bitmap bitmap = decodeStreamFromAssets("test.png", 64, 64);
if(bitmap != null){
imageViewTest.setImageBitmap(bitmap);
}
else {
Logs.e("error");
}
There are two ways by which you can load image from assets folder.
Solution 1:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test, null);
imageViewTest.setImageBitmap(bitmap);
Solution 2:
InputStream ins = null;
try {
ins = getAssets().open("test.png");
Bitmap bitmap = BitmapFactory.decodeStream(ins);
imageViewTest.setImageBitmap(bitmap);
} catch (final IOException e) {
e.printStackTrace();
} finally {
if (ins != null)
try {
ins.close();
} catch (IOException e) { }
}
I suggest to use second one because good performance.
Running both functions 50 times to load a small PNG file (230*230) on Nexus
Galaxy running Android 4.2.2:
decodeResource: 1793ms
decodeStream: 188ms
where the decodeStreamFromAssets?
InputStream is = getAssets().open("ic_launcher.png");
Bitmap bitmap = BitmapFactory.decodeStream(is);
it work!

Android : cannot read image in assets folder and assign to imageview

I have a file food.jpg in assets folder. I have try many ways. but I still cannot get this image into imageView. when run, instead of display image "food.jpg", it displays image that I have declared in xml file. (that image is in drawable folder).
The first way is:
int asset_id = context.getResources().getIdentifier("food", "drawable", context.getPackageName());
imageView.setImageResource(asset_id);
The second way is:
AssetManager assetManager = context.getAssets();
InputStream istr;
try {
istr = assetManager.open("food");
Bitmap bitmap = BitmapFactory.decodeStream(istr);
imageView.setImageBitmap(bitmap);
istr.close();
} catch (IOException e) {
e.printStackTrace();
}
Please teach me how to fix this problem.
thanks :)
Your code is working fine. just add image MIME-Type(.jpg,jpeg,.png... etc) in below line:
istr = assetManager.open("ceo.jpg");
full code for your refrance
AssetManager assetManager = getAssets();
InputStream istr;
try {
istr = assetManager.open("ceo.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(istr);
imageView.setImageBitmap(bitmap);
istr.close();
} catch (IOException e) {
e.printStackTrace();
}
Try this Code...
try {
// get input stream
InputStream ims = getAssets().open("food.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
imgNews.setImageDrawable(d);
} catch (Exception ex) {
return;
}
}

How to display the image from assets folder in android?

I have kept my images in assets folder and tried to display them. I tried in many ways but still I am unable to display the image. In logcat it is not displaying any error.
Please help me regarding this..........
AssetManager mngr = mContext.getResources().getAssets();
is = mngr.open("test3.png");
bitmap = BitmapFactory.decodeStream(is);
Uri uriSavedImage=Uri.fromFile(pictures_directory);
((ImageView) view.findViewById(R.id.imageView1)).setImageBitmap(bitmap);
You can use AssetManager to get the InputStream using its open() method and then use decodeStream() method of BitmapFactory to get the Bitmap.
private Bitmap getBitmapFromAsset(String strName)
{
AssetManager assetManager = getAssets();
InputStream istr = null;
try {
istr = assetManager.open(strName);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(istr);
return bitmap;
}
Something isn't right here, what error are you getting? Because the error in you question wouldn't come up from that code below... if anything it would say "Unable to open content: file:///testing/test3.png."
It sound like your images are stored in the wrong place and that is why it's saying it can't find them.

Categories

Resources