android开发-图片库或sd卡中未显示照片

svgewumm  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(390)

我目前正在遵循这个指南https://developer.android.com/training/camera/photobasics#taskpath.
我正在尝试拍摄一张照片,并将其添加到我的图库中,但每当我拍摄一张照片时,任何内容都不会添加到图库或存储它的sd卡中。下面是我用java编写的使用相机拍照的代码。

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.example.android.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;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == Activity.RESULT_OK) {
        switch (requestCode) {
            case REQUEST_IMAGE_CAPTURE:
                galleryAddPic();
        }
    }
}
private void galleryAddPic() {
    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);
}

我认为在这里生成uri有问题,或者没有正确地放置额外的uri。有什么建议要解决吗?

if (photoFile != null) {
    Uri photoURI = FileProvider.getUriForFile(this,
        "com.example.android.fileprovider",
        photoFile);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
4xrmg8kj

4xrmg8kj1#

由用户blackapps指定
file storagedir=getexternalfilesdir(environment.directory\u pictures)应用程序特定getexternalfilesdir()中的图片永远不会被媒体商店扫描,因此永远不会被gallery应用程序显示blackapps 2小时前
我试着使用这里的公共存储: File storageDir = Environment.getExternalStoragePublicDirectory() 我遇到了一个从未请求过写入权限的问题。我在这里找到了解决方案,我只需要把这个添加到我的清单中 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:node="replace" />

相关问题