How to load an SVG file from SD Card into an ImageView? - android

I'm trying to load an svg file downloaded into sd card with Picasso but it doesn't work.
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()+"/secciones.svg");
Picasso.with(context).load(file).into(holder.ivIcon);
I found this question before but he load the file from assets.
Load a vector drawable into imageview from sd card
Is it possible to load the .svg downloaded into the imageView?

You may achieve this by converting SVG to drawable before setting it to ImageView. For this purpose there is a nice library (a bit old and un-managed) Here, however I tried and worked for me. The code is as below :
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "your_file_path/filename.svg");
try {
FileInputStream fileInputStream = new FileInputStream(yourFile);
SVG svg = SVGParser.getSVGFromInputStream(fileInputStream);
Drawable drawable = svg.createPictureDrawable();
imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
imageView.setImageDrawable(drawable);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

We cannot use SVG with android application. (yes…there are 3rd party libraries that we can use to work with SVG files)
Source : Android — Working with VectorDrawable
VectorDrawable images are simply xml file that contains all information of an image (like lines, curves etc. ) and the color associated with each one of them. The biggest advantage of using vectorDrawable is that we need only one image for different screen devices.This not only reduces the size of the final apk, also it simplifies the maintenance of the project.

Related

How to read SVG file for ImageView from assets folder Android Studio [duplicate]

I want to show a vector image (say vectorimage.xml) in an imageview from the sd card. Please throw some insight on how to do this in android.
What I have tried already :-
String imagePath = Environment.getExternalStorageDirectory() + "/ folder/productImage.xml";
bitmapImage = BitmapFactory.decodeFile(imagePath);
Drawable bgrImageDrawable = new BitmapDrawable(bitmapImage);
The above code snippet does not work since the bitmapImage is coming as null.
The BitmapFactory can't load vector drawables.
You have to use the VectorDrawable or VectorDrawableCompat class.
To load a vector drawable you need to use a xml loader.
Some parser like the one for the resources need a precompiled binary xml file. You can find them in the apk file when you put the vector drawable in the drawable resource directory.
Here is a sample to load it from the assets, you should be able to use a similar code for the loading from the sd card.
final XmlResourceParser parser = context.getAssets().openXmlResourceParser("assets/folder/image.xml");
drawable = VectorDrawableCompat.createFromXml(context.getResources(), parser);
This way needs at least Android 5.0

Use assets folder images for gallery

I am creating an android application.In that one layout file uses a gallery.
I want to know how to use the images from assets folder as gallery images.
for e.g.
for a specific layout file new_am.xml I use the images from assets/new_am folder for the Gallery of that layout.
I know how to use it in case of minimum amount of images , but in my app each folder in assets contains more than 50 images.
You can dynamically access assets folders and set images to Gallery like below,
File fA=new File("file:///android_asset/folderA");
File[] filesA =fA.listFiles();
File fB=new File("file:///android_asset/folderB");
File[] filesB =fB.listFiles();
Also you can collect asset's folders names dynamically.
Hope this will help you.
// to reach asset
AssetManager assetManager = getAssets();
// to get all item in dogs folder.
String[] images = assetManager.list("FolderA");
InputStream inputStream = getAssets().open("FolderA/" + images[0]);
// load image as Drawable
Drawable d = Drawable.createFromStream(inputStream, null);
mImageView.setImageDrawable(d); //set to an Image view
inputStream.close();

How to store images of application

I have a lot of png images in assets folder. I need to use them in app in ImageView and Gallary. Android supports only Bitmaps images. So what is the right way to store files. For example while first run of application it decodes images from png to bmp and saves it somewhere on internal storage , is it possible ? And every start app checks if folder with bmp images exist or not , if exists it uses previously decoded bmp images.
Or there is another way to store a lot of images ?
Any help would be greatly appreciated. Thanks
-4 but 0 answers. Please help
I wan't to call decode method only once on first app start , and than use decoded bmps saved on internal storage .
I think it's not need to decode! Universal Image Loader is a powerful image loader library for android. it can load image from drawables folder, assets, url and ...... this library have cashing system. refer this tutorial.
https://github.com/nostra13/Android-Universal-Image-Loader
Start with:
Converting your largest images from png to jpeg. Jpeg has some disadvantages for a mobile env. but it does produce as smaller image.
Try using png 'reducer' tools like OptiPNG or PNGCrush to reduce png image size. You will usually not see a noticeable difference in image quality.
And, if that does not solve your problem consider zipping all (or at least the largest) of your images into a zip file to be stored in assets/ directory and, at first run, open it to an sdcard folder.
After you do that you will need to populate your ImageViews as follows:
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
Which is a pain but if you have no other choice..
Extraction of assets/zip file goes like this:
public void unzipFromAssets(String zipFileInAssets, String destFolder) {
FileInputStream fin = new FileInputStream(zipFileInAssets);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
if(ze.isDirectory()) {
File f = new File(_location + ze.getName());
if(!f.isDirectory()) {
f.mkdirs();
}
} else {
FileOutputStream fout = new FileOutputStream(destFolder + ze.getName());
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
}
You will need to work a bit on this method - try..catch, getting path for asserts/ files etc.
Good luck.

How to read .gif file from internal storage?

I want to read .gif file from internal storage. I can read image, mp3 but not .gif. Actually I am searching global way to read any types of file.
Regards
Edit:
private void globalGif() throws FileNotFoundException{
FileInputStream fis = openFileInput("frog");
GifMovieView gmv = new GifMovieView(getApplicationContext(), fis);
setContentView(gmv);
}
I use this code but it shows this error. divide by zero fis haven't gif file.
If you want to read gif files, take a look at the Movie class.
Here's a nice tutorial which show you how to load a gif and show it.
you can find the path of image following way
File file= new File(Environment.getExternalStorageDirectory()
+ "/Images/a.gif");
But you can not set this gif image to ImageView the gif file is show in webView.

How to open a JPG file as a BITMAP with the JPG stored on the SDCARD?

Actually i know how to open PNG files as bitmaps. But my code doens't works for open JPG files, i dont know why.
I can't find correct examples on SO or google about how to do this.
I need to have a bitmap with the JPG file opened from a dir of the sdcard. For example "sdcard/images/01.jpg"
Thanks
File root = Environment.getExternalStorageDirectory();
ImageView IV = (ImageView) findViewById(R.id."image view");
Bitmap bMap = BitmapFactory.decodeFile(root+"/images/01.jpg");
IV.setImageBitmap(bMap);
Always try to use Environment.getExternalStorageDirectory(); instead of sdcard.
You need an ImageView somewhere in your layout, however that's how I do this kind of things.
I use this code personally too, and it works here.
Any of the BitmapFactory.decode* methods should be able to handle standard JPG files.
If you post some code it could be easier to see why it won't work.

Categories

Resources