Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Good Noon To all

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 issue.

Thanks in advance
Posted

1 solution

Try this...
Bitmap bitmap = drawable.getBitmap();

String mBaseFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath();
                            
if (!new File(mBaseFolderPath).exists()) {
   new File(mBaseFolderPath).mkdir();
   }

String mFilePath = mBaseFolderPath + "/image.png";

File file = new File(mFilePath);

FileOutputStream stream = new FileOutputStream(file);

if (!file.exists()){
    file.createNewFile();
}

bitmap.compress(CompressFormat.PNG, 100, stream);

bitmap.recycle();
                            
stream.flush();
stream.close();  
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900