如何在Android中手动保存图像(使用相机拍摄)

新媒体

我想知道如何控制何时可以保存从相机拍摄的照片,现在我有以下代码:

File storagePath = new File(Environment.
                    getExternalStorageDirectory() + "/PhotoAR/"); 
      storagePath.mkdirs(); 

      File myImage = new File(storagePath,
                    Long.toString(System.currentTimeMillis()) + ".jpg");

      try
      {
        FileOutputStream out = new FileOutputStream(myImage);
        newImage.compress(Bitmap.CompressFormat.JPEG, 80, out);


        out.flush();
        out.close();
      }
      catch(FileNotFoundException e)
      {
        Log.d("In Saving File", e + "");    
      }
      catch(IOException e)
      {
        Log.d("In Saving File", e + "");
      }

但是现在,预览无法正常工作,我的意思是,图像已保存在PhotoAR文件夹中,但是会自动保存,我想要一个按钮或其他东西,然后保存或放弃选项,有什么方法可以改善它完成这种行为?

也许有什么例子吗?

提前致谢!

卡尔西卡(Karthika)PB

创建一个对话框,要求在代码上方保存或取消照片(保存图片的代码),然后仅在“确定”按钮的onclick侦听器内调用您描述的代码。

在以下代码中,默认的相机应用程序本身会在图像捕获后要求保存或取消

mCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (taken_image == null) {

                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
                    image_uri = fileUri;
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
                    startActivityForResult(intent, 1);
                    mEdit.setVisibility(View.VISIBLE);
                    dialog.cancel();
                }

            }
        });

@Override
    public void onActivityResult(int requestCode, int resultCode,
            final Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK && requestCode == 1) {

            if (image_uri != null)
                try {
                    ExifInterface exif = new ExifInterface(image_uri.getPath()); // Since API Level 5
                    int orientation = exif.getAttributeInt(
                            ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL);
                    // TODO
                    upload_image_hd = BitmapFactory.decodeFile(image_uri
                            .getPath());
                    switch (orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        Rotate90Bitmap(upload_image_hd, 90);
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        Rotate180Bitmap(upload_image_hd, 180);
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        Rotate270Bitmap(upload_image_hd, 270);
                        break;
                    case ExifInterface.ORIENTATION_NORMAL:
                        RotateBitmap(upload_image_hd, 0);
                        break;
                    default:
                        RotateBitmap(upload_image_hd, 0);
                        break;
                    }

                } catch (OutOfMemoryError e) {
                    Toast.makeText(getActivity(),
                            e + "\"memory exception occured\"",
                            Toast.LENGTH_LONG).show();

                    taken_image = null;
                    round_Image = null;
                    upload_image_hd = null;

                } catch (Exception e) {
                    Toast.makeText(getActivity(), e + "\"exception occured\"",
                            Toast.LENGTH_SHORT).show();
                    taken_image = null;
                    round_Image = null;
                    upload_image_hd = null;

                }

public Bitmap RotateBitmap(Bitmap source, float angle) {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);

        round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(),
                source.getHeight(), matrix, true);
        // get the base 64 string
        imgString = Base64.encodeToString(getBytesFromBitmap(round_Image),
                Base64.NO_WRAP);
        uploadProfilePicture_bytes();
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
                source.getHeight(), matrix, true);
    }

    public Bitmap Rotate90Bitmap(Bitmap source, float angle) {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);

        round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(),
                source.getHeight(), matrix, true);
        // get the base 64 string
        imgString = Base64.encodeToString(getBytesFromBitmap(round_Image),
                Base64.NO_WRAP);
        uploadProfilePicture_bytes();
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
                source.getHeight(), matrix, true);
    }

public Bitmap Rotate180Bitmap(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);

    round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(),
            source.getHeight(), matrix, true);
    // get the base 64 string
    imgString = Base64.encodeToString(getBytesFromBitmap(round_Image),
            Base64.NO_WRAP);

    uploadProfilePicture_bytes();
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
            source.getHeight(), matrix, true);
}

public Bitmap Rotate270Bitmap(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);

    round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(),
            source.getHeight(), matrix, true);
    // get the base 64 string
    imgString = Base64.encodeToString(getBytesFromBitmap(round_Image),
            Base64.NO_WRAP);

    uploadProfilePicture_bytes();
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
            source.getHeight(), matrix, true);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在 firebase 中手动拍摄多个快照

来自分类Dev

如何在python中手动处理图像

来自分类Dev

如何在Android Studio中手动运行Proguard?

来自分类Dev

如何在Android中手动暂停活动?

来自分类Dev

如何在Android Studio中手动运行Proguard?

来自分类Dev

如何在相机拍摄的图像上添加相框并保存

来自分类Dev

拍摄一些手动存储在Firebase存储中的图像,并在我的React Native应用中使用它

来自分类Dev

如何在树莓派中设置相机以拍摄黑白图像?

来自分类Dev

Mongoose:如何在保存前手动设置_id?

来自分类Dev

如何使用回形针手动裁剪图像?

来自分类Dev

如何使用图像手动创建Outlook签名?

来自分类Dev

情节:如何在3D图中为相机视图设置手动缩放?

来自分类Dev

如何在Android的Google Analytics(分析)v4中手动结束会话?

来自分类Dev

如何在Camera2,Android中手动控制ISO

来自分类Dev

如何在Android Studio中手动链接react-native-camera?

来自分类Dev

如何在自定义Android XML中手动查找“ @string / ...”样式引用

来自分类Dev

如何在Android Studio中手动导入Gradle依赖项

来自分类Dev

如何在android studio中手动安装gradle-5.5.1-all.zip?

来自分类Dev

如何在 Android 中手动对 ArrayList<Model> 进行排序?

来自分类Dev

如何在SSRS中手动删除订阅?

来自分类Dev

如何在MVC 6中手动验证模型

来自分类Dev

如何在C中手动迭代堆栈帧?

来自分类Dev

如何在PySide中手动触发aboutToQuit信号

来自分类Dev

如何在ReactJS中手动设置输入值?

来自分类Dev

如何在Python中手动创建稀疏矩阵

来自分类Dev

如何在自制软件中手动安装zenity?

来自分类Dev

如何在Magento中手动更新库存数量

来自分类Dev

如何在iOS钥匙串中手动存储?

来自分类Dev

如何在Visual C ++中手动处理名称?

Related 相关文章

  1. 1

    如何在 firebase 中手动拍摄多个快照

  2. 2

    如何在python中手动处理图像

  3. 3

    如何在Android Studio中手动运行Proguard?

  4. 4

    如何在Android中手动暂停活动?

  5. 5

    如何在Android Studio中手动运行Proguard?

  6. 6

    如何在相机拍摄的图像上添加相框并保存

  7. 7

    拍摄一些手动存储在Firebase存储中的图像,并在我的React Native应用中使用它

  8. 8

    如何在树莓派中设置相机以拍摄黑白图像?

  9. 9

    Mongoose:如何在保存前手动设置_id?

  10. 10

    如何使用回形针手动裁剪图像?

  11. 11

    如何使用图像手动创建Outlook签名?

  12. 12

    情节:如何在3D图中为相机视图设置手动缩放?

  13. 13

    如何在Android的Google Analytics(分析)v4中手动结束会话?

  14. 14

    如何在Camera2,Android中手动控制ISO

  15. 15

    如何在Android Studio中手动链接react-native-camera?

  16. 16

    如何在自定义Android XML中手动查找“ @string / ...”样式引用

  17. 17

    如何在Android Studio中手动导入Gradle依赖项

  18. 18

    如何在android studio中手动安装gradle-5.5.1-all.zip?

  19. 19

    如何在 Android 中手动对 ArrayList<Model> 进行排序?

  20. 20

    如何在SSRS中手动删除订阅?

  21. 21

    如何在MVC 6中手动验证模型

  22. 22

    如何在C中手动迭代堆栈帧?

  23. 23

    如何在PySide中手动触发aboutToQuit信号

  24. 24

    如何在ReactJS中手动设置输入值?

  25. 25

    如何在Python中手动创建稀疏矩阵

  26. 26

    如何在自制软件中手动安装zenity?

  27. 27

    如何在Magento中手动更新库存数量

  28. 28

    如何在iOS钥匙串中手动存储?

  29. 29

    如何在Visual C ++中手动处理名称?

热门标签

归档