Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何配置ACTION_IMAGE_CAPTURE将照片存储在公共外部存储器中?_Android_Android Image Capture - Fatal编程技术网

Android 如何配置ACTION_IMAGE_CAPTURE将照片存储在公共外部存储器中?

Android 如何配置ACTION_IMAGE_CAPTURE将照片存储在公共外部存储器中?,android,android-image-capture,Android,Android Image Capture,建议将设备摄像头拍摄的图像存储为“应保存在设备的公共外部存储器中”。但如何做到这一点呢?提供的示例演示了如何保存到私人文件夹 提供的示例演示了如何保存到私人文件夹 不,没有。它显示了如何保存到getExternalFilesDir()。这是的一部分,用户可以访问 如果您愿意,还可以在环境.getExternalStoragePublicDirectory(Environment.DIRECTORY\u DCIM)下创建一个子目录。不过,您需要在所有API级别上使用WRITE\u EXTERNAL

建议将设备摄像头拍摄的图像存储为“应保存在设备的公共外部存储器中”。但如何做到这一点呢?提供的示例演示了如何保存到私人文件夹

提供的示例演示了如何保存到私人文件夹

不,没有。它显示了如何保存到
getExternalFilesDir()
。这是的一部分,用户可以访问

如果您愿意,还可以在
环境.getExternalStoragePublicDirectory(Environment.DIRECTORY\u DCIM)
下创建一个子目录。不过,您需要在所有API级别上使用
WRITE\u EXTERNAL\u STORAGE
,包括需要在Android 6.0+上设置运行时权限

提供的示例演示了如何保存到私人文件夹

不,没有。它显示了如何保存到
getExternalFilesDir()
。这是的一部分,用户可以访问


如果您愿意,还可以在
环境.getExternalStoragePublicDirectory(Environment.DIRECTORY\u DCIM)
下创建一个子目录。不过,您需要在所有API级别上编写外部存储,包括需要在Android 6.0+上设置运行时权限。

查看此代码它肯定会帮助您:

私有void selectImage(){


请参阅此代码,它肯定会帮助您:

私有void selectImage(){

试试这个

//Click Picture from camera
        camera_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.TITLE, "New Picture");
                values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
                imageUri = getContentResolver().insert(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, CAMERA_CLICK_RESULT);

            }


        });
结果是这样的

 @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) {

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


        try {
            //Write file
            String filename = "/file_name";
            String dir_path = "Directory_Path";
            File file = new File(dir_path)
            file.mkdir();
            FileOutputStream fileOutputStream = new FileOutputStream(dir_path + 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);
}
试试这个

//Click Picture from camera
        camera_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.TITLE, "New Picture");
                values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
                imageUri = getContentResolver().insert(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, CAMERA_CLICK_RESULT);

            }


        });
结果是这样的

 @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) {

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


        try {
            //Write file
            String filename = "/file_name";
            String dir_path = "Directory_Path";
            File file = new File(dir_path)
            file.mkdir();
            FileOutputStream fileOutputStream = new FileOutputStream(dir_path + 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);
}

动作图像捕获方法:

// OPEN CAMERA & TAKE PIC
private void dispatchTakePictureIntent() {

    try {

        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {

            // Create the File where the photo should go
            //File photoFile = null;

            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                ex.printStackTrace();
                Log.e("takePic_IO_EX", ex + "");
            }

            //photoFile = createImageFile();

            // Continue only if the File was successfully created
            if (photoFile != null) {

                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.example.appname.fileprovider",
                        photoFile);


                List<ResolveInfo> resInfoList = this.getPackageManager().queryIntentActivities(takePictureIntent, PackageManager.MATCH_DEFAULT_ONLY);
                for (ResolveInfo resolveInfo : resInfoList) {
                    String packageName = resolveInfo.activityInfo.packageName;
                    this.grantUriPermission(packageName, photoURI, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                }
                /*takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                this.setResult(RESULT_OK, takePictureIntent);*/


                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
        Log.e("takePic_main_EX", e + "");
    }

}
并在
res
文件夹中创建一个名为
xml
的文件夹。在该
xml
文件夹中,创建一个
xml文件
并将其命名为
file\u path.xml
。在
文件路径.xml中提及此路径

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="Images" path="Android/data/" />
</paths>

全局声明
文件photoFile

祝您一切顺利!

动作图像捕获方法:

// OPEN CAMERA & TAKE PIC
private void dispatchTakePictureIntent() {

    try {

        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {

            // Create the File where the photo should go
            //File photoFile = null;

            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                ex.printStackTrace();
                Log.e("takePic_IO_EX", ex + "");
            }

            //photoFile = createImageFile();

            // Continue only if the File was successfully created
            if (photoFile != null) {

                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.example.appname.fileprovider",
                        photoFile);


                List<ResolveInfo> resInfoList = this.getPackageManager().queryIntentActivities(takePictureIntent, PackageManager.MATCH_DEFAULT_ONLY);
                for (ResolveInfo resolveInfo : resInfoList) {
                    String packageName = resolveInfo.activityInfo.packageName;
                    this.grantUriPermission(packageName, photoURI, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                }
                /*takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                this.setResult(RESULT_OK, takePictureIntent);*/


                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
        Log.e("takePic_main_EX", e + "");
    }

}
并在
res
文件夹中创建一个名为
xml
的文件夹。在该
xml
文件夹中,创建一个
xml文件
并将其命名为
file\u path.xml
。在
文件路径.xml中提及此路径

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="Images" path="Android/data/" />
</paths>

全局声明
文件photoFile

祝您一切顺利!

虽然保留文件对象后,我不需要在onActivityResult.ya中搜索它,因为您知道代码的流程。尽管保留文件对象后,我不需要在onActivityResult.ya中搜索它,因为您知道代码的流程。这同样有效,但不是使用此方法可以获取旋转信息。此方法也有效,但无法使用此方法获取旋转信息。