0

i am making an app and i have done this till now.

public class TrafficAdd extends Activity{
Button take,upload;
EditText sub,details;
Bitmap bmp;
Intent i;
ImageView iv;
final static int cameraData=0;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.trafficadd);
InputStream is=getResources().openRawResource(R.drawable.green);
bmp=BitmapFactory.decodeStream(is);
take=(Button) findViewById(R.id.trafficcamera);
upload=(Button) findViewById(R.id.trafficupload);
sub=(EditText) findViewById(R.id.eTtrafficSub);
details=(EditText) findViewById(R.id.eTtrafficDetails);
iv=(ImageView) findViewById(R.id.trafficpic);

take.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i,cameraData);


    }
});

 }
  @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
    Bundle extras=data.getExtras();
    bmp=(Bitmap)  extras.get("data");
    iv.setImageBitmap(bmp);
}
   }

 }

I am taking the picture from camera and i am able to set it as Bitmap bmp. Now i need to save this bmp somewhere in SD card and retrieve it later it to view. Please help!!!

Darshan Gowda
  • 4,956
  • 3
  • 19
  • 27
  • possible duplecate of [this](http://stackoverflow.com/questions/11846108/android-saving-bitmap-to-sd-card) and I guess there are a lot more similar type questions. – Kunu Mar 20 '14 at 16:13

1 Answers1

0

To store image on SD card:

public boolean storeImage(Bitmap imageData, String filename) {
    //get path to external storage (SD card)
    String iconsStoragePath = Environment.getExternalStorageDirectory() + "/myAppDir/myImages/"
    File sdIconStorageDir = new File(iconsStoragePath);

    //create storage directories, if they don't exist
    sdIconStorageDir.mkdirs();

    try {
        String filePath = sdIconStorageDir.toString() + filename;
        FileOutputStream fileOutputStream = new FileOutputStream(filePath);

        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

        //choose another format if PNG doesn't suit you
        imageData.compress(CompressFormat.PNG, 100, bos);

        bos.flush();
        bos.close();

    } catch (FileNotFoundException e) {
        Log.w("TAG", "Error saving image file: " + e.getMessage());
        return false;
    } catch (IOException e) {
        Log.w("TAG", "Error saving image file: " + e.getMessage());
        return false;
    }

    return true;
}

Since this operation saves data on external memory, it requires AndroidManifest.xml permissions:

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

To retrieve image from sdcard :

File f = new File(android.os.Environment.getExternalStorageDirectory()+ "/myAppDir/myImages/"+your image file name);
ImageView mImgView = (ImageView)findViewById(R.id.imageView);
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
mImgView.setImageBitmap(bmp);
Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72