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;
}
}
Related
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;
}
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;
}
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();
}
Am trying to add icons to the grid dynamically based on the path provided by service,
i have added the icons inside the assets folder, am getting file not found exception, how can i fix it.
Below is my code,
try
{
InputStream im = context.getAssets().open(App.Path);
Drawable d = Drawable.createFromStream(im, null);
}
catch(IOException e)
{
e.printStackTrace();
}
I see you have "Profile Registration.png" -> rename the file so it does not contain spaces. Something like: "profile_registration.png"
Actually the problem is with prefix /assets/ :)
try {
String image = App.Path;
if (image.contains("/assets/")) {
image = image.replace("/assets/", "");
}
InputStream ims = getAssets().open(image);
Drawable d = Drawable.createFromStream(ims, null);
imageView.setImageDrawable(d);
} catch (IOException ex) {
return;
}
Please Check Your files availablity.
If Files there,do following instructions
Instead of
InputStream im = context.getAssets().open(App.Path);
please use this
InputStreamReader im = new InputStreamReader(getApplicationContext().getAssets().open("path name upto file")));
Please Use this.It will work
Try this:
try {
String image_path = App.Path;
String filepath = null;
if (image_path.contains("/ASSETS/")) {
filepath = image_path .replace(ASSETS, "");
}
InputStreamReader im = new InputStreamReader(getApplicationContext().getAssets().open(filepath)));
Drawable d = Drawable.createFromStream(ims, null);
imageView.setImageDrawable(d);
} catch (IOException ex) {
return;
}
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.