无法保存Android的整个图片

时间:2017-09-29 06:46:45

标签: java android xml android-camera android-gallery

我对Android开发还是比较新的但我在学习的过程中学习了很长时间,而且我正在上课。我有一个应用程序做了一些事情,但我坚持的部分是处理相机和拍照。这个想法是应用程序将使用相机拍照并将图像设置为"来源"对于ImageView。如果我为ImageView使用位图,我可以做到这一点。但是,用户在使用相机并将其保存后,也应该能够将照片从其图库导入应用程序。这就是我被困的地方。我一直在关注如何设置并使其正常工作的教程,但它给了我一个奇怪的错误。 https://developer.android.com/training/camera/photobasics.html

我能够通过"获取缩略图"部分但在那之后我卡住了。我不确定为什么它不会将我的照片保存到任何位置。

代码中写着:

<meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"></meta-data>

我意识到我需要在名为res的{​​{1}}文件夹中创建一个文件夹,并在其中放置一个名为xml的文件。那就是我放的地方

file_paths

非常感谢任何帮助

由于

我的(相关)Java代码:

<Paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="Android/data/com.example.android.postvu/files/pictures" />
</Paths>

我的Manifest.XML文件:

String mCurrentPhotoPath;

    private File createImageFile() throws IOException
    {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
            imageFileName,
            ".jpg",
            storageDir
        );
        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }

    static final int REQUET_TAKE_PHOTO = 1;

    private void dispatchTakePictureIntent()
    {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null)
        {
            File photoFile = null;
            try
            {
                photoFile = createImageFile();
            }
            catch (IOException ex)
            { }

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

    private void galleryAddPic()
    {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(mCurrentPhotoPath);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
    }

2 个答案:

答案 0 :(得分:0)

裁剪或相机活动在指定路径上无权访问。 您正在启动剪裁图像的裁剪意图,它不在您的应用程序中。但是在您在应用程序路径中指定的文件提供程序中。

答案 1 :(得分:0)

Storagedir与您在xml文件中提到的外部路径不同。改为路径=&#34;。&#34;。