Appdev
Appdev

Reputation: 299

How do I...Store Image from imageview To sd card.on a button click

I was Developing an application which will have image on image view .

My need:

What i need is When i click the button then it should store the image that exist in the image view to the sd card(emulator).

Here is how i used:(but no expected results)

Button btnWriteSDFile = (Button) findViewById(R.id.btnWriteSDFile);
btnWriteSDFile.setOnClickListener(new OnClickListener() {
public void onClick(View v) {

ImageView myImage = (ImageView) findViewById(R.id.imageView1);
BitmapDrawable drawable = (BitmapDrawable) myImage.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File sdCardDirectory = Environment.getExternalStorageDirectory();
File image = new File(sdCardDirectory, "image.png");
boolean success = false;

// Encode the file as a PNG image.
FileOutputStream outStream;
try {

outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);


outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Image saved with success",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
}
});

In my above code i didnt get any error

It simply Shows that "save file in mnt/sd/image.png" but no images found.

It would be appreciable if some one helps me to get me out from this rid.

**

"Found out where the exact issues": My program was running perfectly @ 1st time if i click button and check in gallery means there is no image but once i close and open the emulator then there is a image.But i need to see the image as soon as updated how to do this any ideas?

**

Upvotes: 0

Views: 129

Answers (3)

Sanket Shah
Sanket Shah

Reputation: 4382

Try below code to take a screenshot

private static Bitmap takeScreenShot()
    {
        Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
        return bitmap;
    }

For saving

    private File saveBitmap(Bitmap bitmap) 
    {
        File snapShot=null;
        try 
        {
             ByteArrayOutputStream bytes = new ByteArrayOutputStream();
              bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
              File f = new File(Environment.getExternalStorageDirectory()
                                + File.separator + "test.jpg");
              f.createNewFile();
              FileOutputStream fo = new FileOutputStream(f);
              fo.write(bytes.toByteArray()); 
              fo.close();
        } catch (Exception e) 
        {
               e.printStackTrace();
        }
        return snapShot;
    }

Need to give permission

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

Upvotes: 1

V.P.
V.P.

Reputation: 612

if you use some thing like

ImageView myImage = (ImageView) findViewById(R.id.imageView1);
myImage .setDrawingCacheEnabled(true);
myImage .buildDrawingCache();
Bitmap bitmap = myImage .getDrawingCache();

File sdCardDirectory = Environment.getExternalStorageDirectory();
File image = new File(sdCardDirectory, "image.png");
boolean success = false;

// Encode the file as a PNG image.
FileOutputStream outStream;
try {

outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);


outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Image saved with success",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
}
});

and please set the following permission in androidManifest.xml

android.permission.WRITE_EXTERNAL_STORAGE

Upvotes: 1

Albert Sadowski
Albert Sadowski

Reputation: 642

Your code looks good, but in order to write something to external storage, you should have the following permission declared in the manifest:

android.permission.WRITE_EXTERNAL_STORAGE

Upvotes: 1

Related Questions