非法参数异常:找不到已配置的根目录

Illegal argument exception: Failed to find configured root

我试图分享一张从滑行加载到图像视图中的图像 guide,它给了我一个非法参数异常。我已经在下面发布了代码和堆栈跟踪,

MainAcitvity.xml

public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private ShareActionProvider myShareActionProvider;
    //private Bitmap bitmap;
    private Uri uri;
    private Intent shareIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageView imageView = (ImageView) findViewById(R.id.imageView);

        String hi = "http://37n98a43dqtb4bua9n28nidp.wpengine.netdna-cdn.com/wp-content/uploads/2016/09/MyFriendPikachu.jpg";

        Glide
                .with(this)
                .load(hi)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        prepareShareIntent(((GlideBitmapDrawable) resource).getBitmap());
                        attachShareIntentAction();
                        return false;
                    }
                })
                .placeholder(R.drawable.ic_action_name)
                .error(R.drawable.ic_img_error)
                .centerCrop()
                .into(imageView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.img_menu, menu);

        MenuItem item = menu.findItem(R.id.action_share);

        myShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
        attachShareIntentAction();

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        return super.onOptionsItemSelected(item);


    }

    public void prepareShareIntent(Bitmap drawableImage) {

        Uri bmpUri = getBitmapFromDrawable(drawableImage);

        shareIntent = new Intent();

        shareIntent.setAction(Intent.ACTION_SEND);

        shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);

        shareIntent.setType("image/*");

    }


    public void attachShareIntentAction() {

        if (myShareActionProvider != null && shareIntent != null)

            myShareActionProvider.setShareIntent(shareIntent);

    }


    public Uri getBitmapFromDrawable(Bitmap bmp) {


        Uri bmpUri = null;

        try {

            //also tried getExternalDir(Environment.DIRECTORY_PICTURES)
            File file = new File(Environment.getExternalStorageDirectory(), "images" + System.currentTimeMillis() + ".png");

            FileOutputStream out = new FileOutputStream(file);

            bmp.compress(Bitmap.CompressFormat.PNG, 90, out);

            out.close();


            bmpUri = FileProvider.getUriForFile(MainActivity.this, "com.example.imnobody.sampleprojectnetwork.fileprovider", file);  // use this version for API >= 24

            // **Note:** For API < 24, you may use bmpUri = Uri.fromFile(file);

        } catch (IOException e) {

            e.printStackTrace();

        }

        return bmpUri;

    }

}

Androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.imnobody.sampleprojectnetwork">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />





    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider

            android:name="android.support.v4.content.FileProvider"

            android:authorities="com.example.imnobody.sampleprojectnetwork.fileprovider"

            android:exported="false"

            android:grantUriPermissions="true">

            <meta-data


                android:name="android.support.FILE_PROVIDER_PATHS"

                android:resource="@xml/fileprovider" />

        </provider>
    </application>

</manifest>

fileprovider.xml

<?xml version="1.0" encoding="utf-8"?>

<paths>

    <external-file-path

        name="images"

        path="Pictures"  />

</paths>

堆栈跟踪

java.lang.IllegalArgumentException: 找不到包含 /storage/sdcard0/images1502651207040.png 的已配置根目录 在 android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711) 在 android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400) 在 com.example.imnobody.sampleprojectnetwork.MainActivity.getBitmapFromDrawable(MainActivity.java:134) 在 com.example.imnobody.sampleprojectnetwork.MainActivity.prepareShareIntent(MainActivity.java:95) 在 com.example.imnobody.sampleprojectnetwork.MainActivity$1.onResourceReady(MainActivity.java:59) 在 com.example.imnobody.sampleprojectnetwork.MainActivity$1.onResourceReady(MainActivity.java:51) 在 com.bumptech.glide.request.GenericRequest.onResourceReady(GenericRequest.java:522) 在 com.bumptech.glide.request.GenericRequest.onResourceReady(GenericRequest.java:507) 在 com.bumptech.glide.load.engine.EngineJob.handleResultOnMainThread(EngineJob.java:158) 在 com.bumptech.glide.load.engine.EngineJob.access100 美元(EngineJob.java:22) 在 com.bumptech.glide.load.engine.EngineJob$MainThreadCallback.handleMessage(EngineJob.java:202) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:193) 在 android.app.ActivityThread.main(ActivityThread.java:5292) 在 java.lang.reflect.Method.invokeNative(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 在 dalvik.system.NativeS

首先,在您的元数据中,将 external-file-path 更改为 external-files-path,添加缺少的 s

其次,/storage/sdcard0/images1502651207040.png 将不匹配已修复的元数据。元数据期望您使用 getExternalFilesDir(),特别是那里的 Pictures 目录。您的路径与此不同。

根据 documentation:

<external-path name="name" path="path" />

Represents files in the root of the external storage area. The root path of this subdirectory is the same as the value returned byEnvironment.getExternalStorageDirectory().

您正在从外部存储 root (/storage/sdcard0) 调用引用,但您需要引用您的 package root在 /storage/sdcard0/Android/data/your.package.name/Pictures 代替。