在SDcard Android Studio上保存捕获的图像

时间:2017-02-09 18:23:17

标签: android

这是我的代码我想创建一个应用程序来存储捕获的图片格式相机并将其保存在SD卡上尽管如此,图片保存在手机的内存中但不在SD卡中请帮帮我!

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);
        }
    }
}

}

2 个答案:

答案 0 :(得分:0)

尝试以下方法。

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();
    }

}

并且不要忘记在清单文件中添加权限。

答案 1 :(得分:0)

Environment.getExternalStoragePublicDirectory将始终为您提供主要共享/外部目录的路径。您可以找到有关它的更多信息here

主要共享目录可以是与用户或外部sdcard目录共享的内部目录,具体取决于设备的制造商。

虽然要获得API级别19以上的设备中的共享/外部目录列表,但您可以在上下文类中使用getExternalFilesDirs()方法。