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!
Related
Working on Android fashion app and downloading various images from AWS Cloudfront, then storing them locally on internal storage.
The same image looks different if I show it from my app or from the gallery app. It's not about display, I'm sure of this because I tested with the Photoshop color picker.
I guess it may depend on compression but I have maximum value (100). Also, I tried to write directly array byte to my memory and decode the array without any compression.
Look this image to better understand:
I know that Android handles different color profile and I've tried many of those, with no effects.
Also, I use Glide library to load the images and I set no cache and ARGB8888 color profile.
Then I tried to use Picasso, but nothing changed.
That's how I download and save the images:
#Override
protected Bitmap doInBackground(String... strings) {
try {
return BitmapFactory.decodeStream((InputStream)new URL(strings[0]).getContent());
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
#Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (bitmap != null) {
try {
String filename = colorImage.getUniqueId() + "_zoom.jpg";
// I tried this
File file = new File(getContext().getFilesDir(), filename);
FileOutputStream fos = new FileOutputStream(file, false);
// Writing the bitmap to the output stream
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
// But also this
// File file = new File(getContext().getFilesDir(), filename);
// FileOutputStream fos = new FileOutputStream(file, false);
// FileUtils.writeByteArrayToFile(file, array);
fos.close();
} catch (Exception e) {
Log.e("mylog", "saveInternalStorageError(): " + e.getMessage());
}
}
}
That's how I show the image (after I read all issues referenced here https://github.com/bumptech/glide/issues/515):
Glide.with(this)
.load(imageUriF)
.asBitmap()
.encoder(new BitmapEncoder(Bitmap.CompressFormat.JPEG, 100))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(firstImage);
I also tried to show the image without Glide with same results
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
img.setImageBitmap(myBitmap);
And Picasso too, with same results.
Have you got any idea about this issue?
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;
}
As part of my program, I need to show images belonging to a folder (JPG files). To accomplish that, I have this code:
private ArrayList<ImageGalleryItem> getData() {
final ArrayList<ImageGalleryItem> imageItems = new ArrayList<>();
try {
Intent intent = getIntent();
String imagesFolder = intent.getStringExtra("SourceFolder");
String questionId = Integer.toString(intent.getIntExtra("QuestionId", 0));
File file = new File(imagesFolder);
if (file.exists() && file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
try {
if (f.getName().startsWith(questionId + "_")) {
Bitmap bitmap = BitmapFactory.decodeFile(f.getPath());
imageItems.add(new ImageGalleryItem(bitmap, f.getName()));
}
}
catch (Exception e)
{
Log.e("TDC#", e.getMessage());
}
}
}
}
catch (Exception e)
{
Log.e("TDC#", e.getMessage());
}
return imageItems;
}
The instruction "BitmapFactory.decodeFile(f.getPath());" creates the bitmap files for all files but for one of them. When that code is reached for the conflicting file, decodeFile throws an exception, but the stranger thing is that the exception is not catched by the try catch block.
When debugging using F7, the exception is thrown in BitmapFactory.java, in the throw shown in this code:
try {
bm = nativeDecodeByteArray(data, offset, length, opts);
if (bm == null && opts != null && opts.inBitmap != null) {
throw new IllegalArgumentException("Problem decoding into existing bitmap");
}
setDensityFromOptions(bm, opts);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
}
So the questions are, why the bitmap cannot be correctly decoded and why the exception is not catched.
If I browse for the file in mobile file manager, and open the file, it is shown correctly, so, there is no problem with the image format. Furthermore, this image was taken with the camera, the same as the other images that do decode correctly.
How to solve this problem or, is there an alternate way to do this?
I don't know why that happens. Try this:
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(f));
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;
}
}
i try the example i gotten from here. The example shows how to display a image that is larger then the screen and the screen acting like a window, allowing user to look at the images via scrolling. However, what i want to achieved is similar just that, instead of one single image, i tried to combining 18 smaller images (171X205) into one single image. I am able to did that but to save loading time on downloading of images from the server, i cached the images. Heres the problem, i cant seems to display the images out on screen even though the images are indeed cached. Thus, i tried something else by fetching image from the drawable folder but still the same issue arise. Does anyone have any idea how to go about solving this problem?
A snippets of code to load images from cache:
for(int i =0; i<18; i++)
File cacheMap = new File(context.getCacheDir(), smallMapImageNames.get(i).toString());
if(cacheMap.exists()){
//retrieved from cached
try {
FileInputStream fis = new FileInputStream(cacheMap);
Bitmap bitmap = BitmapFactory.decodeStream(fis)
puzzle.add(bitmap);
}catch(...){}
}else{
Drawable smallMap = LoadImageFromWebOperations(mapPiecesURL.get(i).toString());
if(i==0){
height1 = smallMap.getIntrinsicHeight();
width1 = smallMap.getIntrinsicWidth();
}
if (smallMap instanceof BitmapDrawable) {
Bitmap bitmap = ((BitmapDrawable)smallMap).getBitmap();
FileOutputStream fos = null;
try {
cacheMap.createNewFile();
fos = new FileOutputStream(cacheMap);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
puzzle.add(bitmap);
}
}
}
The function where it retrieved images from the server
private Drawable LoadImageFromWebOperations(String url) {
// TODO Auto-generated method stub
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
I am drawing onto a canvas and so no ImageView is use to display the images.
For all of my lazy image loading I use Prime.