How to Set Background Image from asset folder in android? - android

I am facing problem while setting the backGround Image of LinearLayout from asset folder.
String filePath="file:///android_asset/images/test.png";
Drawable d = Drawable.createFromPath(filePath);
frontTextView.setBackgroundDrawable(d);
Can someone help me out.

First you create a Drawable object from the asset file:
Drawable d = Drawable.createFromStream(getAssets().open(path_in_assets), null);
and then set it to some View that only supports Drawables as a background.

As far as I'm aware, you cannot access assets directly like you are trying to. You'll need to use the AssetManager class to get at your data if you want to store it as an asset. Here's a pretty decent blog post explaining a bit about resources and assets, though the official documentation is also a good resource, of course.
I'll also add, though, that things like background images are typically best stored in res/drawable and accessed using the R.drawable.* style (the blog post linked above also discusses this) whenever possible. It's not really clear why you need to do it this way from your provided code sample, though, so I suppose that's ultimately your call.

EDIT: added create image from InputStream...
I had the similar problem using ImageButton. I figured it out by loading bitmap from assets and using it as image for ImageButton. Probably not a good approach, but is working and solved my problem - unability to have subfolders in drawable dir and not allowed characters in file names.
(Yes, I can use prefix instead of subdir, and rename files to match the pattern (lowercase only and numbers) and I probably will do it later.)
InputStream is = null;
try {
is = this.getResources().getAssets().open("Images/Fruits/Apple.png");
} catch (IOException e) {
Log.w("EL", e);
}
Bitmap image = BitmapFactory.decodeStream(is);
ImageButton ib2 = (ImageButton) findViewById( R.id.imageButton2);
ib2.setImageBitmap( image);

Related

How to show SVG string as an Image in Xamarin?

So the simple idea is that I have SVG data as a string that I fetch from Internet.
I would like to show that as an icon in my app. Is there any way I can do this?
I have seen countless examples where SVG data is in a file located in the app's directory that is then showed but this is not what I am looking for. I literally have the data in XML format after http request and I only need to transform that to a Image or something else visible on the screen.
I have been trying to find a solution to this for hours now, so I would really appreciate some help :S
Android doesn't support svg with ImageView directly,you could display SVG with some commonly used third-party controls.
Like FFImageLoading.
Add Xamarin.FFImageLoading.Svg nuget package to platform project. Use ImageService with svg data resolver for displaying svg images.
For Android:
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
then use like:
var svgString = #"<svg><rect width=""30"" height=""30"" style=""fill:blue"" /></svg>";
ImageView imageView = FindViewById<ImageView>(Resource.Id.image_view);
ImageService.Instance
.LoadString(svgString)
.LoadingPlaceholder(placeHolderPath)
.WithCustomDataResolver(new SvgDataResolver(64, 0, true))
.WithCustomLoadingPlaceholderDataResolver(new SvgDataResolver(64, 0, true))
.Into(imageView);

how to define a LottieDrawable?

It looks like LottieComposition.Factory is deprecated already. I'm wondering how I can define a LottieDrawable? I could not find the documentation for this.
The reason I want a Drawable is because I want to pass a Lottie animation into fresco as a placeholder drawable.
I realise this is an old question, but since I too came against the same problem and managed to find how to do it, might as well share it.
So a LottieCompositionFactory can be used to obtain a LottieComposition from a raw .json file, URL, or assets in the assets folder etc. using the corresponding function as given in the API reference, and then use the composition to assign it to a LottieDrawable.
For example, using a .json file
val animDrawable = LottieDrawable()
LottieCompositionFactory.fromRawRes(requireContext(), R.raw.anim_file).addListener {comp ->
animDrawable.composition = comp
}
Then the drawable can be used anywhere a Drawable can be passed.
the right answer is :
val animDrawable = LottieDrawable()
LottieCompositionFactory.fromRawRes(context, R.raw.loading_banner).addListener { comp ->
animDrawable.composition = comp
}

JPG image shows from phone but not from server - android

In my app I am able to pick up a picture from the Gallery and show it on my phone. The picture has extension JPG. But when I email it to myself, save it on the server and then try to display it on my phone, it does not display. I even tried to downsize it to 30% using my email app on the phone, so now it is 220KB instead of 1.4MB but it still does not display.
In both cases I use the method
imageView.setImageBitmap(BitmapFactory.decodeFile(personPicture))
What do I need to do to overcome this problem?
BTW: the name of the picture was changed when I saved it on the server. I do not think it matters but I mentioned it anyway.
EDIT
The above is all the code I am using. Just to complete the issue here is the code that handles both jpg and png and it works if the picture is renamed to png.
if (url.contains("jpg")) {
imageView.setImageBitmap(BitmapFactory.decodeFile(url));
} else {
Drawable drw = LoadImageFromWebOperations(url);
if (drw != null) {
imageView.setImageDrawable(drw);
}
}
Note: the 1.4MB PNG file worked fine on the emulator but gave Out of memory exception on the device. When I re-sized the PNG file to 350KB it displayed properly on the device also.
If needed here is the url used in the above code (a picture of a cat)/
http://212.150.56.58:8080/feedback/pictures/56.png
When you try to load image from server into app, load it using Picasso library like as below:
Picasso.with(MainActivity.this).load("image_to_be_loaded").into(profile_image);
Edit
If you don't want to use third party library then try the following code:
public Drawable loadImageFromURL(String url, String name) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, name);
return d;
} catch (Exception e) {
return null;
}
}
Use Picasso to load server images to your app image view it also supports caching and lot many features.
Use is like this
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
checkout Picasso Library for more details.
I do not understand why, but if I renamed the picture on my server to extension PNG the picture displays, in all sizes.

How to add image background using String path from database in android studio?

Ok, after searching and browsing over the internet, I just didn't get what I was looking for. I have a database with a field for "Path", where my images file names are listed.
**Table1:**
PATH ID
imgRice.jpg 1
imgMango.jpg 2
imgBanana.jpg 3
Now I want my imageView to change picture accordingly from my query:
"Select Path from table1 where ID = 1";
I will declare a String, like String queryResultString to handle the result (ex.imgRice.jpg).
I want a code that looks like imgView.image="drawable/" + queryResultString
Any help is appreciated. Thanks guys!
Thanks for the comments and suggestions guys, highly appreciated.
But I've got a simple yet tricky turnaround for my problem.
First, it is IMPOSSIBLE to rip the Drawable path because it is compressed when published to .apk file.
Second, to make it possible, you need to use id for every file in your drawable.
Third, it is much better to add files(images) to asset folder(\app\src\main\assets).
Lastly, follow these simple codes:
//code to initiate the object ImageView...
ImageView myImage = (ImageView) findViewById(R.id.pic);//pic is the id of ImageView in my xml file...
try
{
// now, get the input stream
InputStream input= getAssets().open(c.getString(c.getColumnIndex("image")));//image is the name of field in my db which holds the path (ex. rice.jpg)...
// initiate image as drawable
Drawable draw = Drawable.createFromStream(input, null);
// set image to ImageView
myImage.setImageDrawable(draw);
}
catch(IOException ex)
{
return;
}
Enjoy Coding! ^_^

Set image / background source dynamically

is there any possibility to set background image dynamically?!
I try to explain what I mean.
String picVariable = getPictureFromServer();
ImageView image = (ImageView)v.findViewById(R.id.dynamic_image);
// I know, that doesn't work, but that's the way I looking for
image.setBackgroundResource(picVariable);
Thank you in advance,
Mur
Ps.
I also read this article. It would suggested in one answer, to use java reflection to get a field of the R class by name. But I've never used reflextion before. An example would be very helpfull
Sometimes I should take a bit more time for searching :)
I found the answer reading this article, and it works fine for me:
// The server says, it should be *.png
String picName = getPictureFromServer();
picName = picName.replace(".png", "");
Resources r = getResources();
int picId = r.getIdentifier(picName, "drawable", "com.mypackage.myapp");
ImageView image = (ImageView)v.findViewById(R.id.dynamic_image);
image.setBackgroundResource(picId);
ImageView does not have any background, but for other widget (like Button), you should use setBackgroundResource(int).
Sorry, I am not sure I read the question correctly... maybe your problem is just that you are trying to use it with a Widget that does not use background ?

Categories

Resources