How to display the image from assets folder in android? - 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.

Related

How to access a file from asset/raw directory

with this below code i'm trying to access the file which is stored in asset/raw folder, but getting null and
E/ERR: file:/android_asset/raw/default_book.txt (No such file or directory)
error, my code is:
private void implementingDefaultBook() {
String filePath = Uri.parse("file:///android_asset/raw/default_book.txt").toString();
File file = new File(filePath);
try {
FileInputStream stream = new FileInputStream(file);
} catch (Exception e) {
e.printStackTrace();
Log.e("ERR ", e.getMessage());
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
}
Place your text file in the /assets directory under the Android project and use AssetManager class as follows to access it.
AssetManager am = context.getAssets();
InputStream is = am.open("default_book.txt");
Or you can also put the file in the /res/raw directory, from where the file can be accessed by an id as follows
InputStream is =
context.getResources().openRawResource(R.raw.default_book);
Assets and resources are files on your development machine. They are not files on the device.
For assets, use open() on AssetManager to get an InputStream on your asset.
Also, FWIW:
Uri.parse("file:///android_asset/raw/default_book.txt").toString() is pointless, as it gives you the same string that you started with
file:///android_asset/ only works for WebView
As the actual question wasn't sufficiently answered, here we go
InputStream is = context.getAssets().openFd("raw/"+"filename.txt")
context can be this or getActivity() or basically any other context
Important is to include the folder before the filename separated by an /
In Kotlin we can achieve as-
val string = requireContext().assets.open("default_book.txt").bufferedReader().use {
it.readText()
}
InputStream is = getAssets().open("default_book.txt");

Adding images from assets folder in adapter

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;
}

Creating an Android Bitmap from an Amazon S3 object with BitmapFactory?

I have an image in the S3 server stored with the .bmp extension and retrieve the object with a call to getObject(). I then instantiate an InputStream with a call to getObjectContent(). I'm trying to create a bitmap object by passing the inputStream to BitmapFactory's decodeStream() method. As stated in the docs, it is returning null (instead of a Bitmap) presumably because the inputStream "cannot be used to decode a bitmap". Any ideas on why this is happening? Alternatively, ideas on other ways to download a photo from S3 would be greatly appreciated. Thanks so much!
GetObjectRequest getPhoto = new GetObjectRequest(MainActivity.USERS_BUCKET_NAME, finalList.get(i));
S3Object photoObject = s3.getObject(getPhoto);
InputStream inputStream = photoObject.getObjectContent();
//process inputStream
Bitmap bmp = BitmapFactory.decodeStream(inputStream); //this is returning null
//close inputStream
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
I fixed the problem by using the Apache Commons IOUtils library - I converted the inputstream into a byte array with IOUtils.toByteArray(inputStream) and passed that into BitmapFactory.decodeByteArray(); it no longer returns null.

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;
}
}

NinePatchDrawable from Drawable.createFromPath or FileInputStream

I cannot get a nine patch drawable to work (i.e. it is stretching the nine patch image) when loading from either Drawable.createFromPath or from a number of other methods using an InputStream.
Loading the same nine patch works fine when loading from when resources. Here's the methods I am trying:
Button b = (Button) findViewById(R.id.Button01);
Drawable d = null;
//From Resources (WORKS!)
d = getResources().getDrawable(R.drawable.test);
//From Raw Resources (Doesn't Work)
InputStream is = getResources().openRawResource(R.raw.test);
d = Drawable.createFromStream(is, null);
//From Assets (Doesn't Work)
try {
InputStream is = getResources().getAssets().open("test.9.png");
d = Drawable.createFromStream(is, null);
} catch (Exception e) {e.printStackTrace();}
//From FileInputStream (Doesn't Work)
try {
FileInputStream is = openFileInput("test.9.png");
d = Drawable.createFromStream(is, null);
} catch (Exception e) {e.printStackTrace();}
//From File path (Doesn't Work)
d = Drawable.createFromPath(getFilesDir() + "/test.9.png");
if (d != null) b.setBackgroundDrawable(d);
I answered a very similar question here about non-resource-based ninepatches. To read a file from within .apk archives, I use something like this:
String filepath = "com/kanzure/images/thx1138.png";
InputStream stream = getClass().getClassLoader().getResourceAsStream(filepath);
I have a very similar problem. I'm trying to create a NinePatchDrawable from a Bitmap that I've retrieved over the network. I still haven't found a solution to that problem. Since the Drawable.createFrom... methods apparently doesn't work, my only option seems to be to understand how to format the nine-patch chunk, and to invoke the NinePatch constructor. See the SO-thread for details.

Categories

Resources