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

时间:2015-03-31 04:51:56

标签: android android-camera android-camera-intent

我想知道如何控制从相机中取出的相机可以保存,现在我有这个代码:

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文件夹中,但它会自动保存,我想要一个按钮或其他东西,并保存或放弃选项,有没有办法改进它来完成这种行为?

也许是任何一个例子?

提前感谢!

1 个答案:

答案 0 :(得分:2)

创建一个对话框,要求保存或取消代码上方的照片(保存图片的代码),并仅在ok按钮的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);
}