从我的应用程序使用相机意图时如何自动保存图像?

时间:2017-07-28 02:35:15

标签: android android-camera

当我从我的应用程序使用相机意图时,它正在打开相机,但点击它后要求保存图像,但当我们使用移动相机应用程序点击图像时,它会自动保存。

使用相机意图也会打开相同的内置相机应用程序然后为什么会出现双重行为?

如何在使用我的应用程序中的相机意图时让相机自动保存图像

1 个答案:

答案 0 :(得分:0)

试试这个

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    //Get Image from Camera
    if (requestCode == CAMERA_CLICK_RESULT && resultCode == RESULT_OK) {

        dialog2.dismiss();
        Bitmap photo = null;
        try {
            photo = MediaStore.Images.Media.getBitmap(
                    getContentResolver(), imageUri);
        } catch (IOException e) {
            e.printStackTrace();
        }
        selectedImage = getResizedBitmap(photo, 900)


        try {
            //Write file
            filename = "your file name.extension";
            File file = new File("Directory path where you want to save");
            file.mkdir();
            FileOutputStream fileOutputStream = new FileOutputStream(file + filename);
            selectedImage.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);

            //Cleanup
            fileOutputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
  }
}

//Resize Bitmap
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
    int width = image.getWidth();
    int height = image.getHeight();

    float bitmapRatio = (float) width / (float) height;
    if (bitmapRatio > 1) {
        width = maxSize;
        height = (int) (width / bitmapRatio);
    } else {
        height = maxSize;
        width = (int) (height * bitmapRatio);
    }
    return Bitmap.createScaledBitmap(image, width, height, true);
}