Save images from drawable to internal file storage in Android - 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.

Related

Save an image stored in the resources/drawable folder inot the gallery

I have an image button. When the user clicks on the button, I want to save an image stored in the resources/drawable folder into the gallery of the phone/tablet.
I cannot figure this out! I am a beginning android programmer and I have researched for hours on how to do this. However, I do not understand the many websites that post code without any explanation. I'd appreciate any help.
Get image bitamp :
Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
Store bitamp on given path :
File file = new File( Environment.getExternalStorageDirectory().toString(), "ic_launcher.png");
outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
Add this permission in AndroidManifest.xml :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Image inserted into gallery not the same that is saved to storage

I have been browsing SO for a while now but were not able to find a topic that came up with this problem.
My drawing application should be able to save the drawn images. I want to save them as PNG files, so I use this code:
OutputStream fOut = null;
String dirPath = Environment.getExternalStorageDirectory().toString();
File file = new File(dirPath, "drawing0.png");
fOut = new FileOutputStream(file);
drawView.getDrawingCache().compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
imgSaved = MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(), file.getAbsolutePath(),
file.getName(), file.getName());
Now the image is saved to a directory and it is a PNG file. But the gallery image itself is a JPG and it seems like the gallery is just copying the original file and transforms it into a JPG, because it tells me a different file directory:
gallery path
file path
Now I want to know if this can be avoided, because if the user wants to share the picture e.g. via Dropbox it uploads the gallery JPG image and not the original PNG. I am using the standard Samsung gallery app if it matters.

Need to save image into the gallery folder of Phone Memory(Not SD Card)

I need to create a folder in the default gallery folder of the phone menory(Not SD Card).After that I need to save the Image into a folder(creating it in gallery folder) using the camera.
The problem is that I can find the default galley folder in the SDcard and I can create the folder and save images into it.
But with the phone memory I can't see the default gallery folder .Is there any way to see the gallery folder in the phone memory.And also how to create folder and save images into it.
Try to create folder in internal memory like this
File mydir = context.getDir("mydir", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(mydir, "myfile");
FileOutputStream out = new FileOutputStream(fileWithinMyDir);
And than while saving file on folder,give path to this directory and Check O/P
this line add your image in gallery:
MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);
Note that your must also add
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
to your manifext.xml
You can create Folder and saved images into Internal Memory(Phone Memory). But,any one can't see those folder as well as image too. That is the purpose of internal memory in android
To create and write a private file to the internal storage:
1. Call openFileOutput() with the name of the file and the operating mode. This returns a FileOutputStream.
2. Write to the file with write().
3. Close the stream with close().
For example:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
Refer the link...............
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

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

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