2017-08-13 285 views
1

我試圖共享一個圖像加載到glide下面這個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異常:無法找到配置的根是包含android.support.v4.content.FileProvider $ SimplePathStrategy.getUriForFile(FileProvider.java /storage/sdcard0/images1502651207040.png : 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) at com.example.imnobody.sampleprojectnetwork.MainActivity $ 1.onResourceReady(MainActivity.java:59) at com.example.imnobody.sampleprojectnetwork.MainActivity $ 1.onResourceReady(MainActivity.java:51) at com.bumptech.glide.request .GenericRequest.onResourceReady(GenericRequest.java:522) at com.bumptech.glide.request.GenericRequest.onResourceReady(GenericRequest.java:507) at com.bumptech.glide.load.engine.EngineJob.handleResultOnMainThread(EngineJob.java :158) at com.bumptech.glide.load.engine.EngineJob.access $ 100(EngineJob.java:22) at com.bumptech.glide.load.engine.EngineJob $ MainThreadCallback.handleMessage(EngineJob.java:202) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:5292) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java :824) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 在dalvik.system.NativeS

+0

根本原因是應用程序未能找到請求的文件:'無法找到包含/storage/sdcard0/images1502651207040.png的配置根...' –

回答

2

首先,在你的元數據,CHA nge external-file-pathexternal-files-path,增加缺少的s

其次,/storage/sdcard0/images1502651207040.png將不匹配修復的元數據。元數據期望您在那裏使用getExternalFilesDir(),特別是Pictures目錄。你的路徑不像那樣。

1

按照documentation

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

表示在根目錄中的外部存儲區域的 文件。此子目錄的根路徑爲 ,與由Environment.getExternalStorageDirectory()返回的值 相同。

要調用從外部存儲(/存儲/ sdcard0)的參考,但你需要在/storage/sdcard0/Android/data/your.package.name/引用您的根改爲圖片。