【问题标题】:FileProvider throws IllegalArgumentExceptionFileProvider 抛出 IllegalArgumentException
【发布时间】:2015-02-22 07:22:09
【问题描述】:

我设计了一个 Activity(MainActivity.java),它可以在单击 CaptureButton 时捕获图像,并在单击 ShowFilesButton 时显示所有捕获的图像本身strong> 以 ListView 的形式。 但是我的应用程序在 Emulator/Android 设备中运行时崩溃了,因为我收到了以下 RunTimeError,如 LogCat 中所示。

Logcat的错误截图

android.support.v4.content.FileProvider 也出现在我的 Eclipse 中

AndroidManifest 文件中的Provider 标记-我不确定authorities 属性中要提供什么值。

<provider
            android:name="android.support.v4.content.FileProvider"
            android:exported="false"
            android:authorities="com.example.showinfo.fileprovider"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>
</provider>

@xml/file_paths

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

com.example.showinfo.MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        listfiles=(ListView)findViewById(R.id.listfiles);
        capturebutton=(Button)findViewById(R.id.captureimage);
        showfilesbutton=(Button)findViewById(R.id.showfiles);
        setContentView(R.layout.activity_main);

        capturebutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if(intent.resolveActivity(getPackageManager())!=null)
                {
                    startActivityForResult(intent,RCODE);
                }
            }
        });

showfilesbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                File rootfolder= getFilesDir();
                File folder=new File(rootfolder, "images");
                File[] files=folder.listFiles();
                String[] filenames={};
                for(int i=0;i<files.length;i++)
                    filenames[i]=files[i].getAbsolutePath();
                ArrayAdapter<String> a=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,filenames);
                listfiles.setAdapter(a);

            //Uri contentUri=FileProvider.getUriForFile(getApplicationContext(), "com.example.showinfo.fileprovider", files[0].getAbsoluteFile());

            }
        });
}

【问题讨论】:

    标签: android eclipse illegalargumentexception


    【解决方案1】:

    权限只是一个标识您的文件提供者的字符串。

    FileProvider:

    将 android:authorities 属性设置为基于 您控制的域;例如,如果您控制域 mydomain.com 你应该使用权限 com.mydomain.fileprovider。

    在您的 AndroidManifest.xml 中,根据 FileProvider:

    将 android:exported 属性设置为 false; FileProvider 没有 需要公开。

    你的 file_paths.xml 应该是这样的(你不需要 XML 命名空间或 android: 在你的属性之前):

    <?xml version="1.0" encoding="utf-8"?>
    <paths>
        <files-path path="images/" name="myimages" />
    </paths>
    

    在您的 MainActivity.java 中 - 这应该在 startActivityForResult(intent,RCODE); 之前:

    Uri uri = FileProvider.getUriForFile(this, "com.example.showinfo.fileprovider", yourDirectoryOfFiles);
    intent.setData(uri);
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    

    您的帖子相对较旧。希望你弄清楚了……但这也许会对其他人有所帮助。 :)

    【讨论】:

      猜你喜欢
      • 2014-09-18
      • 2014-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多