gpt4 book ai didi

android - FileProvider 抛出 IllegalArgumentException

转载 作者:行者123 更新时间:2023-11-30 03:02:23 25 4
gpt4 key购买 nike

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

Logcat错误截图

Error Screenshot of Logcatandroid.support.v4.content.FileProvider 也存在于我的 Eclipse 中

android.support.v4.content.FileProvider

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());

}
});
}

最佳答案

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

根据 FileProvider :

Set the android:authorities attribute to a URI authority based on a domain you control; for example, if you control the domain mydomain.com you should use the authority com.mydomain.fileprovider.

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

Set the android:exported attribute to false; the FileProvider does not need to be public.

您的 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);

您的帖子相对较旧。希望您能解决问题……但也许这会对其他人有所帮助。 :)

关于android - FileProvider 抛出 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22348766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com