【问题标题】:How can I capture a new image using the camera and after that store it in the external storage?如何使用相机拍摄新图像,然后将其存储在外部存储中?
【发布时间】:2022-03-01 16:59:55
【问题描述】:

我正在尝试使用相机拍摄新图像,然后将其存储在外部存储中。

我按照这个教程Save the full-size photo和这个Add the photo to a gallery,但是运行应用程序后,相机启动并成功拍摄了图像,但是当我去画廊时,找不到通过相机拍摄的图像!

//I changed this from com.example.android.fileprovider to com.test.app.fileprovider
android:authorities="com.test.app.fileprovider"

//I changed this line Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.fileprovider", photoFile); to below code
Uri photoURI = FileProvider.getUriForFile(this, "com.test.app.fileprovider", photoFile);

//I added this permission
<uses-permission android:name="android.permission.CAMERA" />

//I enabled storage permission read and write
//I tested the codes on API 24
//minSdkVersion 21

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.app">

    <uses-feature android:name="android.hardware.camera"
        android:required="true" />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="18" />

    <application>

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.test.app.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

    </application>

</manifest>

file_paths.xml

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

MainActivity

public class MainActivity extends AppCompatActivity {

    String currentPhotoPath;
    int REQUEST_IMAGE_CAPTURE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SplashScreen.installSplashScreen(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dispatchTakePictureIntent();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE_CAPTURE) {
            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            File f = new File(currentPhotoPath);
            Uri contentUri = Uri.fromFile(f);
            mediaScanIntent.setData(contentUri);
            this.sendBroadcast(mediaScanIntent);
        }
    }

    private void dispatchTakePictureIntent() {
        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

            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.test.app.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }

    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = image.getAbsolutePath();
        return image;
    }

}

补充问题:本教程Save the full-size photo和本教程Add the photo to a gallery有什么区别?

如果我达到50分,最好的答案会从这个帐户中取出+50 bounty,否则我会在2天后从我的其他帐户中给他赏金。

【问题讨论】:

  • The best answer will take +50 bounty from this account if I reached 50 points, Otherwise, I'll give him the bounty from my other account after 2 days. 奇怪的是,您从帖子主题中删除了该内容。是一个很好的注意力抽屉;-)
  • @blackapps Marcin Orlowski 修改了问题的标题,但报价仍然有效,我选择它作为正确答案的答案将获得 +50 赏金

标签: android android-camera android-external-storage android-storage


【解决方案1】:

您的文件不会被 MediaStore 扫描,因此不会在 Gallery 应用中可见,因为 Gallery 应用大多从 MediaStore 获取信息。

getExternalFilesDir() 是您应用的私有位置,MediaStore 尊重这一点。

如何使用相机拍摄新图像,然后将其存储在外部存储中?

错误的问题描述。

相机应用拍摄照片后,相机应用会将照片保存到文件提供者指定的文件中。那就没什么可做的了。

所以在启动相机之前,甚至在使用 FileProvider 之前,您应该已经为您的文件确定了一个合适的位置,并为该文件构建了一个不错的 uri。

您至少有两个选择。

使用 MediaStore 获取公共 DCIM 或图片目录中文件的 uri。

使用 getExternalStoragePublicDirectory() 和 FileProvider 来获取相同公共目录中文件的 uri。

【讨论】:

  • 我想在使用相机拍摄新图像后将图像存储在外部存储中并使其对其他应用可见
  • 您能否举例说明如何将捕获的图像保存在名为 TestApp 的文件夹中,并确保该文件夹对其他应用程序是公开的
  • 不需要。你自己几乎没有积分。保留它们。
  • 因为这不是赏金问题,我想知道您是否可以提供赏金。但无论如何不要那样做。你的问题也太初级了,不值得赏金。保持积分。这是命令! ;-)
  • 你是对的。我没有想到。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-19
  • 1970-01-01
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多