1

I used the following code to retrieve an image from a url and display it within an activity.

    InputStream is = (InputStream) new URL(url[0]).getContent();
    Drawable d = Drawable.createFromStream(is, "imagename");
    ImageView... 

Now I want to save this image (Drawable d) locally when a user clicks a button so I can display it again in another activity (along with a few other tasks).

I'd like to store it within the app folder itself rather than on the SD card.

How would I do this?

Thanks! Shannon

Shannon Cole
  • 362
  • 7
  • 21

4 Answers4

4

This will do it for you:

Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
FileOutputStream out = openFileOutput(filename, Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
Damian
  • 8,062
  • 4
  • 42
  • 43
  • Thanks.. This seemed to work. I didn't get an error in the LogCat indicating there was a problem saving the file. If I wanted to access the file in another activity how do I reference this file and where did it save it (drawable, raw, etc)? – Shannon Cole Nov 04 '11 at 18:27
  • Thanks, this code worked perfectly to save the file! I figured how to reference the file once saved by opening it with "InputStream is = openFileInput("imagenamesaved");" and then handling it like I did above in my question. This is working! Awesome Thanks a bunch! – Shannon Cole Nov 04 '11 at 19:07
1

For Drawable save as image, I am doing this,

Bitmap image_saved=BitmapFactory.decodeResource(context.getResources(),R.drawable.icon);

FileOutputStream fOut=new FileOutputStream(path+"/"+fileName); 
// Here path is either sdcard or internal storage
image_saved.compress(Bitmap.CompressFormat.JPEG,100,fOut);
fOut.flush();
fOut.close();
image_saved.recycle(); // If no longer used..

But Actually, I suggest you to Instead of going from an InputStream to a Drawable, go from an InputStream to a File, then load the image out of the file. So you can save first file and use it in loading Image.

And for url Inputstream to write file look at this tutorial Save binary file from URL

user370305
  • 108,599
  • 23
  • 164
  • 151
  • Thanks! I like the suggestion regarding downloading the image locally and just displaying it as needed... The tutorial is great. I have a couple questions... 1) where does the file save itself (drawable, raw, etc). If the answer is where I want... where do you recommend and how do I reference the path to local directories (drawable, raw, etc) 2) How do I reference the file at run time? Do I just have a file already located locally that I overwrite with at runtime? – Shannon Cole Nov 04 '11 at 18:22
  • Nevermind! I got the answer. It was a misunderstanding on my part on how files are handled at runtime! DDMS helped me to under the file structure and where my files were being saved! – Shannon Cole Nov 04 '11 at 19:06
  • Nice, for you got answer of your question by yourelf. :-) – user370305 Nov 04 '11 at 19:09
0

Tested snippet :

InputStream is=null;
            try {
                is = (InputStream) new URL(url).getContent();
            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            Drawable d = Drawable.createFromStream(is, "profile_picture");
            Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
            FileOutputStream out=null;
            try {
                out = getActivity().getApplicationContext().openFileOutput("profile_picture", getActivity().getApplicationContext().MODE_PRIVATE);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
Michael Assraf
  • 111
  • 2
  • 3
0

Please view this documentation for how to save cached files and this documentation for general internal file storage.

Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102