This is the part of my function that assigns an image to my ImageView. I can not get it to show an image. I have my files listed under assets/signs/"image names." This code came straight from my textbook with some minor tweaks to fit my code. I appreciate the help.
String nextImage = signNames.remove(0);
AssetManager assets = getAssets();
InputStream stream;
try{
stream = assets.open("signs/"+nextImage + ".gif");
Drawable sign = Drawable.createFromStream(stream,nextImage +".gif" );
signImageView.setImageDrawable(sign);
}
catch(IOException e){
Log.e(TAG, "Error loading " +nextImage, e);
}
I think there is a much simpler idiom for what you are trying to do. No need to stream the image the want to display. Simply put the image in res/drawable (or res/drawable-hdpi, etc). Then simply:
signImageView.setImageResource(R.drawable.myImage)
Of if you need to get the resource from a dynamic string:
int id = getResources().getIdentifier("yourpackagename:drawable/" + nextImage, null, null);
signImageView.setImageResource(R.drawable.myImage)
see here
This assumes your signImageView is indeed an ImageView.
Related
I have an assets subfolder and images on it all png's some bigger, I'm writing a hybrid app from Angular so I pass via an interface the name of a file to pop it in an activity I pass only the name not the extession. It works for some files but not for others (a bigger one on my case) is there a reason for that???, I'm using this basic code below, the smaller file resID has a number for the bigger file it comes out as 0. The png with the problem does load on a basic page in angular, I want to pop a zoomer of that image. Probably I could place a copy in the Drawable res folder, but Angular needs it hanging from my assets.
image = (TouchImageView) findViewById(R.id.cur_img);
if( getIntent().hasExtra("imageResName") ) // set it to passed image
{ // a popup pass in the image
String curResName =getIntent().getExtras().getString("imageResName");
// gte the di from a drawble ..it wprks
int resID = getResources().getIdentifier(curResName , "drawable", getPackageName());
image.setImageResource(resID);
}
UPDATE: Yep that code will NOT work I could pull an SO answer where
I probably misunderstood it ... this is the mod'ed code that will work
if( getIntent().hasExtra("imageResName") ) // set it to passed image
{ // a popup pass in the image
String curResName =getIntent().getExtras().getString("imageResName");
Bitmap bitmap=null;
try
{ // for image in: /assets/imagenes/xxx.png
AssetManager assetManager = getApplicationContext().getAssets();
InputStream istr = assetManager.open("imagenes/" + curResName + ".png" );
bitmap = BitmapFactory.decodeStream(istr);
istr.close();
}
catch (Exception ex)
{
bitmap = null;
}
finally
{
image.setImageBitmap(bitmap);
}
My application files are bellow, I want to get sharethis.png file in java code , What is syntax ?
I used this ...
Bitmap bMap = BitmapFactory.decodeFile("file:///android_asset/www/sharethis.png");
but not working
Try:
Bitmap shareThis = BitmapFactory.decodeResource(context.getResources(),
R.drawable.shareThis);
If you want to reference it from a JavaScript or html file in your assets folder, use file:///android_asset/www/sharethis.jpg.
Otherwise, according to this answer, this will list all the files in the assets folder:
AssetManager assetManager = getAssets();
String[] files = assetManager.list("");
This to open a certain file:
InputStream input = assetManager.open(assetName);
Try this:
try {
// get input stream
InputStream ims = getAssets().open("sharethis.png");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
mImage.setImageDrawable(d);
}
catch(IOException ex) {
Log.e("I/O ERROR","Failed")
}
Instead of using the assets dir, put the file into /res/raw , and you can then access it using the following URI: android.resource://com.your.packagename/" + R.raw.sharethis
I have issue to retrieve image path from assets folder.
I have an image folder in assets folder. Within the folder i have three different folders.
Here is the code that i used:
String IntroImage1= "" + languageSelected + "/" + Intro1ImagePath + ".png" ;
try{
AssetManager mngr =getAssets();
InputStream BulletImgInput = mngr.open(IntroImage1);
//InputStream BulletImgInput = mngr.open("image/Malay/bullet.png");
Bitmap bitmapBullet = BitmapFactory.decodeStream(BulletImgInput);
BulletImage.setImageBitmap(bitmapBullet);
}catch(final IOException e){
e.printStackTrace();
}
I am wondering why can't i display the image? Because I have try to retrieve it through this code:
InputStream BulletImgInput = mngr.open("image/Malay/bullet.png");
It did retrieve the file, but with the string that i replaced in mngr.open it doesn't show up.
Really need you guys to help up.Thanks.
You don't need the AssetManager. You can do
BitmapFactory.decodeFile("file:///android_asset/image/Malay/bullet.jpg")
Though storing Images in the Assets is not the best way to go. You will not be able to take advantage of the Android resource management system. So unless you have a compelling reason to do this, I'd suggest that you take a look at using the res folder and the resources system.
Update: Explanation
The BitmapFactory has a method to decode a file via the decodeFile method. That's point one. Android lets you access a file in the assets folder via the file:///android_asset/{path} path. In your case, the Image at /image/Malay/bullet.jpg is the assets folder can be accessed via the the file:///android_asset/image/Malay/bullet.jpg.
Try this:
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) {
Log.e("I/O ERROR","Failed when ..."
}
your BulletImage
String url = "file:///android_asset/NewFile.txt";
String url = "file:///android_asset/logo.png";
you can access any file....
InputStream BulletImgInput = mngr.open("file:///android_asset/image/Malay/bullet.png");
Maybe this could work for u.
May be problem is in missed image part of path?
String IntroImage1= "image/" + languageSelected + "/" + Intro1ImagePath + ".png" ;
instead of
String IntroImage1= "" + languageSelected + "/" + Intro1ImagePath + ".png" ;
Upd: Check values of languageSelected and Intro1ImagePath also.
I am writing an Android app that includes an ImageView. The image to be included in the view resides on the local app directory of the emulator. The image is not being displayed in the view. The relavent XML and method used to display the image are given below.
The image file is a "png" format file and is able to be opened from the local app directory. The path returned by getFilesDir() includes the full path, "/data/data/net.vitalrecord.android/files." This path designation does not exist on the development system but does exists when DDMS File Explorer is used.
Any help with this problem is greatly appreciated.
Thanks.
LAYOUT XML:
<AbsoluteLayout>
......
<ImageView
android:id="#+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="100px"
android:layout_y="100px"/>
</AbsoluteLayout>
Method:
public void checkFilesExists() {
.....
try {
Context context = getApplicationContext();
FILENAME = getString(R.string.MedicalImage);
fis = openFileInput(FILENAME);
int size = fis.available();
byte[] buffer = new byte[size];
fis.read(buffer);
fis.close();
setContentView(R.layout.medtogoimage);
ImageView iv = (ImageView)findViewById(R.id.picture);
String imagePath = context.getFilesDir() + "/" + FILENAME;
Drawable d = Drawable.createFromPath(imagePath);
iv.setImageDrawable(d);
medtogo.setbMedicalImageFileExists(true);
} catch (IOException e) {
medtogo.setbMedicalImageFileExists(false);
}
You should put images inside the res/drawable folder. Then you can get its reference with the R class, or assign it to the ImageView from layout xml file.
Did you try setting the mode to MODE_WORLD_READABLE?
i want to show image in imageview without using id.
i will place all images in raw folder and open
try {
String ss = "res/raw/images/inrax/3150-MCM.jpg";
in = new FileInputStream(ss);
buf = new BufferedInputStream(in);
Bitmap bMap = BitmapFactory.decodeStream(buf);
image.setImageBitmap(bMap);
if (in != null) {
in.close();
}
if (buf != null) {
buf.close();
}
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
but this is not working i want to access image using its path not by name
read a stream of bytes using openRawResource()
some thing like this should work
InputStream is = context.getResources().openRawResource(R.raw.urfilename);
Check this link
http://developer.android.com/guide/topics/resources/accessing-resources.html#ResourcesFromCode
It clearly says the following
While uncommon, you might need access your original files and directories. If you do, then saving your files in res/ won't work for you, because the only way to read a resource from res/ is with the resource ID
If you want to give a file name like the one mentioned in ur code probably you need to save it on assets folder.
You might be able to use Resources.getIdentifier(name, type, package) with raw files. This'll get the id for you and then you can just continue with setImageResource(id) or whatever.
int id = getResources().getIdentifier("3150-MCM", "raw", getPackageName());
if (id != 0) //if it's zero then its not valid
image.setImageResource(id);
is what you want? It might not like the multiple folders though, but worth a try.
try {
// Get reference to AssetManager
AssetManager mngr = getAssets();
// Create an input stream to read from the asset folder
InputStream ins = mngr.open(imdir);
// Convert the input stream into a bitmap
img = BitmapFactory.decodeStream(ins);
} catch (final IOException e) {
e.printStackTrace();
}
here image directory is path of assets
like
assest -> image -> somefolder -> some.jpg
then path will be
image/somefolder/some.jpg
now no need of resource id for image , you can populate image on runtime using this