Saving Images in to local folder in android - 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

Related

android add image to drawable programmatically

I want to generate a qr code image and add it programmatically to the assets drawable folder of the app.
In the mean time, you would add it mannually in eclipse or android studio. Just wonder is there any ways to do it programmatically as well.
Many thanks!
This is simply not possible, you cant't modify/add that folder once you have generated apk and installed app. What you can do is to generate a folder on internal or external storage and save your images there.
It is already disccussed here and here
Asset Folder is used to load our Data with application , it never be changed at run time , AssetManger has method to read Asset Data and there is no way to write within Asset programmatically at Run Time.
Rather if you want to store your data at run time , You may store in Internal Memory like below Code.
Drawable drawable = getResources().getDrawable(R.drawable.demo_img);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
bitmap.compress(Bitmap.CompressFormat.PNG, 60, bytearrayoutputstream);
file = new File( Environment.getExternalStorageDirectory() + "/SampleImage.png");
try
{
file.createNewFile();
fileoutputstream = new FileOutputStream(file);
fileoutputstream.write(bytearrayoutputstream.toByteArray());
fileoutputstream.close();
}
catch (Exception e)
{
e.printStackTrace();
}

Android can't read picture stored in phone memory, only works on pictures stored in sd card

I am creating an app that takes pictures then i read them,
when pictures are stored in SD card i can read them fine but when they are stored in phone memory i can find them (newFile displays correct path) but i don't see them in the image view. In samsung phones they are stored in /storage/emulated/0/Pictures/AppImages in cases like this is when i can't read and display in ImageView
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "AppImages");
final ImageView newImage = new ImageView(this);
File newFile = new File(mediaStorageDir+File.separator+files.getString(2));
//I have tried this 3 options one at a time
final Bitmap bmp = BitmapFactory.decodeFile(newFile.getAbsolutePath());
final Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(newFile));
final Bitmap bmp = BitmapFactory.decodeFile(newFile.getPath());
newImage.setImageBitmap(bmp);
Have you tried MediaStore?
Look at this:
MediaStore.Images.Media.EXTERNAL_CONTENT_URI //from external storage
MediaStore.Images.Media.INTERNAL_CONTENT_URI //from internal storage
Take a look at this link
The problem was i was displaying the full pictures in the gallery, resizing them to thumbnails size fixed the issue.

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.

Save images from drawable to internal file storage in Android

I have some 10 images saved in drawable folder. I want to move them to internal file storage.
Is this possible?
I have searched a lot but, I only found links where we can save image to internal file system from given path, but I want to save images from drawable folder to internal file storage in Android and I want to achieve this through code.
Any help will greatly be appreciated.
Thank you.
Saving image to sdcard from drawble resource:
Say you have an image namely ic_launcher in your drawable. Then get a bitmap object from this image like:
Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher);
The path to SD Card can be retrieved using:
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
Then save to sdcard on button click using:
File file = new File(extStorageDirectory, "ic_launcher.PNG");
outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
Don't forget to add android.permission.WRITE_EXTERNAL_STORAGE permission.

How to make folders of application in SD card are not accessible by file manager and other applications?.

I am creating two folders to store images of my application in SD card.but these folder are visible in file manger.i want to prevent access of my folder from outside.i am saving images in that two folders.i am saving like this please help me any one.
File sdcard = Environment.getExternalStorageDirectory();
File pictureDir = new File(sdcard, "Image_dir");
if (!pictureDir.exists()) {
pictureDir.mkdirs();
}
// saving the image file in the folder Image_dir
File f = null;
f = new File(pictureDir, file_name);
FileOutputStream fos = new FileOutputStream(f.getAbsolutePath());
Bitmap image;
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
normally adding a . in front of the name of the folder which will exclude it from media scanners. But with a file manager can still find it.
If you are working on Api Level-8 then I suggest to use getExternalFilesDir (String type)
instead of Environment.getExternalStorageDirectory() because there are two benefit using this
all images and data will be automatically deleted when your application uninstall by user
images do not visible in the media application (in Gallery).

Categories

Resources