Amirouche
Amirouche

Reputation: 3766

How to save images from bitmap into Storage in android

I saved images in a storage succesfull, but I cann't read those images from storage, And this my code:-

String dir = Environment.getExternalStorageDirectory()+ File.separator+"DCIM/stegano";

String dir = Environment.getExternalStorageDirectory()+ File.separator+"DCIM/stegano";

        //createfolder
        File folder = new File(dir);
if(!folder.exists()){
folder.mkdirs();
Toast.makeText(getApplicationContext(),"I am in if",
Toast.LENGTH_LONG).show();
        }
        //creatname file
        String simpleDate=new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String nameImage="STE_"+simpleDate;

        //create file
        Bitmap bitmap;
OutputStreamoutputStream;
        bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.grass);//I have image in drawble
        File file = new File(dir,nameImage+".png");

try {
outputStream=new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);

outputStream.flush();
outputStream.close();
        } catch (FileNotFoundException e) {
e.printStackTrace();
        } catch (IOException e) {
e.printStackTrace();
        }

Upvotes: 0

Views: 4163

Answers (1)

A-Droid Tech
A-Droid Tech

Reputation: 2321

Amirouche,Here is the sample code for saving bitmap to file :

public static File savebitmap(Bitmap bmp) throws IOException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 60, bytes);
    File f = new File(Environment.getExternalStorageDirectory()
            + File.separator + "testimage.jpg");
    f.createNewFile();
    FileOutputStream fo = new FileOutputStream(f);
    fo.write(bytes.toByteArray());
    fo.close();
    return f;
}

Now call this function to save the bitmap to internal memory.

File newfile = savebitmap(bitmap);

I hope it will help you. Happy codeing life.

Upvotes: 2

Related Questions