user1855796
user1855796

Reputation:

store image to SD card

i'm using cropping functionality in my android application and it works fine but i want to save cropped image to my SD Card. For that what steps needs to be followed?

Upvotes: 0

Views: 5482

Answers (3)

sharma_kunal
sharma_kunal

Reputation: 2192

first for get image store in sdcard:-

public static String storeImage(Bitmap bitmap, String filename) {

        String stored = null;

        File sdcard = Environment.getExternalStorageDirectory();
        File file = new File(sdcard, filename + ".png");

        if (file.exists())
            file.delete();

        try {
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
            stored = "success";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stored;
    }

second get image from sdcard:-

public static File getImage(String imagename) {

        File mediaImage = null;
        try {
            String root = Environment.getExternalStorageDirectory().toString();
            File myDir = new File(root);
            if (!myDir.exists())
                return null;

            mediaImage = new File(myDir.getPath() + imagename);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return mediaImage;
    }

3 Convert image from sdcard to bitmap

File file = CommonUtils.getImage("/CoverPic.png");

                String path = file.getAbsolutePath();

                if (path != null)
                    picture = BitmapFactory.decodeFile(path);

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157437

If the Bitmap you cropped is called yourBitmap

File sdcard = Environment.getExternalStorageDirectory();
File f = new File (sdcard, "filename.png");
FileOutputStream out = new FileOutputStream(f);
yourBitmap.compress(Bitmap.CompressFormat.PNG, 90, out)

Upvotes: 0

Pratik Sharma
Pratik Sharma

Reputation: 13415

Try this :

public void saveBitmap(Bitmap bmp)
{
    String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + 
                                "/NewFolder";
        File dir = new File(file_path);
        if(!dir.exists)
           dir.mkdirs();
        File file = new File(dir, "myImage.png");
        FileOutputStream fOut = new FileOutputStream(file);

        bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
        fOut.flush();
        fOut.close();
}

Following permission is required in Manifest file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  

Thanks.

Upvotes: 1

Related Questions