【问题标题】:How to save the image automatically when using camera intent from my app?从我的应用程序使用相机意图时如何自动保存图像?
【发布时间】:2017-07-28 13:15:08
【问题描述】:

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

使用相机意图也会打开相同的内置相机应用程序,那么为什么会有双重行为?

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

【问题讨论】:

  • 你想把它保存在哪里?

标签: android android-camera


【解决方案1】:

试试这个

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    相关资源
    最近更新 更多