Question

If I take a picture it is shown in the image view.This works well. But how can I save the photo additional on the hard drive? Take he the size of the camera setting?

public class Note  extends Activity {

TextView t;
ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.note);
   iv=(ImageView) findViewById(R.id.imageView);

    Button btn = (Button) findViewById(R.id.photo);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, 0);
        }
    });


}
public void button (View view){
    Intent intent = new Intent(view.getContext(),Note2.class);
    startActivityForResult(intent, 0);
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode==0)
    {
    Bitmap theImage = (Bitmap) data.getExtras().get("data");
        iv.setImageBitmap(theImage);

    }}
}

I thank you in advance for your help.

Was it helpful?

Solution

make uri and file

public File mediaFile;
public Uri fileUri;

put these 2 methods in your file

/** Create a file Uri for saving an image or video */
    private static Uri getOutputMediaFileUri(int type){
          return Uri.fromFile(getOutputMediaFile(type));
    }

    /** Create a File for saving an image or video */
    private static File getOutputMediaFile(int type){
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.

        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                  Environment.DIRECTORY_PICTURES), "YourFolderName");
        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Toast.makeText(null, "failed to create directory", Toast.LENGTH_SHORT).show();
                return null;
            }
        }




        if (type == 1){
              Long tsLong = System.currentTimeMillis()/1000;
              String ts = tsLong.toString();
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
            "photo"+ ts + ".jpg");

        } 
         else {
            return null;
        }

        return mediaFile;
    }

then before you startActivityForResult(intent, 0); put:

fileUri = getOutputMediaFileUri(1);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            intent.putExtra("return-data", true);

make sure in your manifest you put:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top