简体   繁体   中英

save captured image on SDcard Android Studio

this is my code i want to create an aplication to store captured picture form camera and save it on the SDcard althought, the picture is saved in the memory of the phone but not in the SDcard please help me !

private Button takePictureButton;
private ImageView imageView;
private Uri file;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    takePictureButton = (Button) findViewById(R.id.button_image);
    imageView = (ImageView) findViewById(R.id.imageview);

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        takePictureButton.setEnabled(false);
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == 0) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
            takePictureButton.setEnabled(true);
        }
    }
}

public void takePicture(View view) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    file = Uri.fromFile(getOutputMediaFile());
    intent.putExtra(MediaStore.EXTRA_OUTPUT, file);

    startActivityForResult(intent, 100);
}

private static File getOutputMediaFile(){




    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "CameraDemo");

    if (!mediaStorageDir.exists()){
        if (!mediaStorageDir.mkdirs()){
            Log.d("CameraDemo", "failed to create directory");
            return null;
        }
    }




    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    return new File(mediaStorageDir.getPath() + File.separator +
            "IMG_"+ timeStamp + ".jpg");
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 100) {
        if (resultCode == RESULT_OK) {
            imageView.setImageURI(file);
        }
    }
}

}

Try following method.

private void saveImageToExternalStorage(String fileName, Bitmap finalBitmap) {

    String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
    File myPicDirectory = new File(root + "/my_pics");
    myPicDirectory.mkdirs();

    File file = new File(myDir, fileName);
    if (file.exists())
    file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); // 90 refers to quality of image
        out.flush();
        out.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }

}

And don't forget to add permission in manifest file too.

Environment.getExternalStoragePublicDirectory will always give you the path to the primary shared/external directory. You can find more about it here .

The primary shared directory can be internal directory shared to user or external sdcard directory depending upon the manufacture of the device.

Although to get list of shared/external directory in a device above API level 19 you can make use of getExternalFilesDirs() method in context class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM