IssacZH.
IssacZH.

Reputation: 1477

Unable to store Image into internal storage directory

I wanted to create a folder and store image into the phone's internal storage. I tried using the code below to download an image from the url. It managed to load the image into imageView but unable to store and create folder in the internal storage. Plus I got no warning or error message. Any idea what's wrong code below?

        Bitmap bm = null;
        InputStream in;

            try{

                in = new java.net.URL("http://blogs.computerworld.com/sites/computerworld.com/files/u177/google-nexus-4.jpg").openStream();
                bm = BitmapFactory.decodeStream(new PatchInputStream(in));
                File mydir = this.getDir("mydir", Context.MODE_PRIVATE);
                mydir.mkdirs();
                File fileWithinMyDir = new File(mydir, "myfile");
                FileOutputStream out = new FileOutputStream(fileWithinMyDir);
                bm.compress(Bitmap.CompressFormat.JPEG, 85, out);



            }
            catch(Exception e1){
                e1.printStackTrace();
            }
  ImageView img = (ImageView) findViewById(R.id.image_display);
  img.setImageBitmap(bm);

Upvotes: 0

Views: 1183

Answers (2)

znat
znat

Reputation: 13474

once you have your bitmap bm:

FileOutputStream fos;

try {
    fos = openFileOutput("file_Name", Context.MODE_PRIVATE);
    bm.compress(Bitmap.CompressFormat.PNG, 100, fos);
    fos.close();
}catch (Exception e){
...
}

will save your file to internal storage

Upvotes: 1

Husam A. Al-ahmadi
Husam A. Al-ahmadi

Reputation: 2056

use the following:

String path=Environment.getExternalStorageDirectory()
                                .toString() + File.separator 

to get the directory and save the image.

Upvotes: 1

Related Questions