Convert Uri to Bitmap and save the image on the device

0

I try to convert Uri to Bitmap with this method:

try {
          Uri uri = FileProvider.getUriForFile(PostDetailActivity.this, getApplicationContext().getPackageName() + ".fileprovider", new File(Uri.parse(mPost.getPhoto()).getPath()));
          Bitmap bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), uri);
          saveImageToExternalStorage(bitmap);
       } catch (IOException e) {
          e.printStackTrace();
          Toast.makeText(PostDetailActivity.this, "Error during download", Toast.LENGTH_SHORT).show();
    }

But, I get the following error:

  

java.lang.IllegalArgumentException: Failed to find set root   that contains   /v0/b/example-app-android.appspot.com/o/photos/ZdOLPeM2k4fSimyfzyySRSkfiaU2_20180911_011804.jpg           at android.support.v4.content.FileProvider $ SimplePathStrategy.getUriForFile (FileProvider.java:738)           at android.support.v4.content.FileProvider.getUriForFile (FileProvider.java:417)

To save the image on the device I have this:

private void saveImageToExternalStorage(Bitmap finalBitmap) {
        String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
        File myDir = new File(root + "/saved_images_1");
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-" + n + ".jpg";
        File file = new File(myDir, fname);
        if (file.exists())
            file.delete();
        try {
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


        // Tell the media scanner about the new file so that it is
        // immediately available to the user.
        MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        Log.i("ExternalStorage", "Scanned " + path + ":");
                        Log.i("ExternalStorage", "-> uri=" + uri);
                    }
                });

    }

Provider:

<manifest>
    <application>
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>
    </application>
</manifest>

provider_paths:

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

Any idea of the problem?

Thanks in advance.

    
asked by Robert Gomez 12.09.2018 в 06:52
source

0 answers