How to store images of application - android

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.

Related

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

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.

How can I export Layout or Views as images to visible gallery folder in Android?

After searching about "How to save Layout views as images", I've found some solution to save in Internal and External Storage. But It seems the image file created is going to save in some data/data/... folder that is not visible normally. Actually I want the image visible in gallery for the user. I've found some code like below, but I even can't check if the image is created or not:
View content = findViewById(R.id.relativeLayout);
String yourimagename = "MyImageFile";
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/" + yourimagename + ".png");
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 10, ostream);
ostream.close();
content.invalidate();
} catch (Exception e) {
e.printStackTrace();
} finally {
content.setDrawingCacheEnabled(false);
}
But It seems the image file created is going to save in some data/data/... folder that is not visible normally.
The file will be saved where the programmer elects to save it.
Actually I want the image visible in gallery for the user. I've found some code like below, but I even can't check if the image is created or not
That code will not work on any version of Android, as new File("/" + yourimagename + ".png") is not going to give you a usable File, as it points to a place that you can neither read nor write.
You are welcome to save the image to internal storage or external storage. Since you want this image to be picked up by "gallery"-type apps, you are best off choosing external storage, then using MediaScannerConnection and its scanFile() method to get the file indexed by the MediaStore, since gallery apps will tend to use the MediaStore as their source of images.
On the whole, I worry that getDrawingCache() will be unreliable. You may be better served telling your root View to draw to your own Bitmap-backed Canvas instead.

Using downloaded images in an Android Application

I want to download PDF's and images into my app, essentially it will call a JSON web service that returns the link to the PDF, link to the image, and the title. It will download the image and PDF and save them. Then it will display the PDF's and images with the title. My only question is how do I deal with images? They cant be saved to APK since it is locked. The images are high enough resolution that they can scaled down to fit all the other densities, should I just use the large image, and let the activity scale it down when it uses it. Or should I implement an image scaler during the retrieval process?
Eventually the PDF's and images would be loaded into the APK. How would I check the assets folder to remove the images, would I just need to call a service that runs when the Application First starts to check for the files in the assets folder then remove them if they are present?
Sample code for your reference where u can download Images from the web.Its better to store Images in asset folder than Internal Memory and resize the images for good performance.U can delete the folder before making web service call and load new set images.
if (Utility.isWifiPresent()
|| Utility.isMobileConnectionPresent()) {
URL url = new URL(fileUrl);
InputStream iStream = url.openConnection().getInputStream();// .read(data)
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] tmpArray = new byte[1024];
int nRead;
while ((nRead = iStream.read(tmpArray, 0, tmpArray.length)) != -1) {
buffer.write(tmpArray, 0, nRead);
}
buffer.flush();
data = buffer.toByteArray();
FileOutputStream fOut = null;
//path to store
fOut = Utility.getFileOutputStreamForCloud(
sdcardFolderPath, fileUrl);
}
fOut.write(data);
fOut.flush();
fOut.close();

Saving Images in to local folder in android

I created android app which reads images from url. Now I want to store those images in local file structure or SD Card. so i created a folder names "images" in my android project and added image xyz.png manually to test reading of images.
and wrote below code to read.
Bitmap bMap = BitmapFactory.decodeFile("/images/xyz.png");
ImageView imgView = (ImageView) this.findViewById(R.id.imgViewId);
imgView.setImageBitmap(bMap);
But eclipse says unable to find the resource!!
What is the best way to store and read images in android app?
I did caching but cache get clears if i force close the app.
I want to store in android mobile/tablet and it should be part of app.
try the enviroment variable to get the directoty
Bitmap bMap = BitmapFactory.decodeFile("/images/xyz.png");
Bitmap bMap = BitmapFactory.decodeFile(Environment.getRootDirectory()+"/images/xyz.png");
give a try to this ....
 
private String filepath = "MyFileStorage";
ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
myInternalFile = new File(directory , "abc.png");
FileOutputStream fos = new FileOutputStream(myInternalFile);
bMap.compress(CompressFormat.PNG, 90, fos); //output image is the image bitmap that you obtain
Check this android description

load Image from specific folder on the sdcard?

I'm attempting to create a gallery/gridview that is loaded with images from a specific folder that resides on an SDCard. The path to the folder is known, ("mnt/sdcard/iWallet/Images") , but in the examples I've seen online I am unsure how or where to specify the path to the pictures folder I want to load images from. I have read through dozens of tutorials, even the HelloGridView tutorial at developer.android.com but those tutorials do not teach me what i am seeking.
Every tutorial I have read so far has either:
A) called the images as a Drawable from the /res folder and put them into an array to be loaded, not using the SDCard at all.
B) Accessed all pictures on the SDCard using the MediaStore but not specifying how to set the path to the folder I want to display images form
or
C) Suggested using BitmapFactory, which I haven't the slightest clue how to use.
If I'm going about this in the wrong way, please let me know and direct me toward the proper method to do what I'm trying to do.
my target android sdk version 1.6...
thanks..
You can directly create Bitmaps from decodeFile (String pathName) that will give you Bitmap object that can be set on ImageView
Update: Below is sudo code with minor errors modify it to suit your needs
File path = new File(Environment.getExternalStorageDirectory(),"iWallet/Images");
if(path.exists())
{
String[] fileNames = path.list();
}
for(int i = 0; i < fileNames .length; i++)
{
Bitmap mBitmap = BitmapFactory.decodeFile(path.getPath()+"/"+ fileNames[i]);
///Now set this bitmap on imageview
}
Actually, you are wrong to mention fixed path to access SD-card directory, because in some device it is /mnt/sdcard and in other /sdcard.
so to access root directory of sd-card, use the getExternalStorageDirectory(), it gives you actual path of root directory.
This function will resturn all the files from specific folder you need to pass path till ur folder
public static List getFilesFromDir(File aStartingDir)
{
List result = new ArrayList();
File[] filesAndDirs = aStartingDir.listFiles();
List filesDirs = Arrays.asList(filesAndDirs);
Iterator filesIter = filesDirs.iterator();
File file = null;
while ( filesIter.hasNext() ) {
file = (File)filesIter.next();
result.add(file); //always add, even if directory
if (!file.isFile()) {
//must be a directory
//recursive call!
List deeperList = getFileListing(file);
result.addAll(deeperList);
}
}
Collections.sort(result);
return result;
}
BitmapDrawable d = new BitmapDrawable(getResources(), path+".jpg"); // path is ur resultant //image
img.setImageDrawable(d);
Hope it help u...
You can access your directory using File java class, then iterate through all the files in there, create a bitmap for each file using Bitmapfactory.decodeFile() then add the bitmaps to your gallery.

Categories

Resources