我想通过FileProvider共享/ Internal Storage / aaa / bbb.dat文件到另一个应用程序。但是它出错了。
我已将路径放入provider_path.xml,并将提供者放入清单文件。
// share intent
private void initShareIntent(String type) {
boolean found = false;
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("*/*");
// gets the list of intents that can be loaded.
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
if (!resInfo.isEmpty()){
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase().contains(type) ||
info.activityInfo.name.toLowerCase().contains(type) ) {
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/aaa/bbb.dat");
// wrap File object into a content provider. NOTE: authority here should match authority in manifest declaration
Uri uri = FileProvider.getUriForFile(MainActivity.this, "org.bramantya.news.copygame.cod.FileProvider", file);
share.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
share.putExtra(Intent.EXTRA_STREAM, uri); // Optional, just if you wanna share an image.
share.setPackage(info.activityInfo.packageName);
found = true;
break;
}
}
if (!found)
return;
startActivity(Intent.createChooser(share, "Select"));
}
}
final Button button = findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
//initShareIntent("midrop");
initShareIntent("mail");
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
});
我希望gmail或midrop / shareme打开并附加bbb.dat文件。但是它出错了
2019-10-26 10:47:27.582 25559-25584/? E/FilePathConverter: resolveFilePath uri = content://org.bramantya.news.copygame.cod.FileProvider/oblehbleh/aaa/bbb.daterror!
java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:465)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at android.database.CursorWrapper.getString(CursorWrapper.java:137)
at com.xiaomi.midrop.sender.c.e.a(Unknown Source:31)
at com.xiaomi.midrop.sender.c.e.c(Unknown Source:0)
at com.xiaomi.midrop.sender.c.e.a(Unknown Source:44)
at com.xiaomi.midrop.sender.ui.TransmissionActivity.a(Unknown Source:61)
at com.xiaomi.midrop.sender.ui.TransmissionActivity$a.doInBackground(Unknown Source:12)
at android.os.AsyncTask$3.call(AsyncTask.java:362)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.example.app"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="@mipmap/copygame"
android:label="@string/app_name"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
/>
<activity
android:name="com.example.app.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false"
android:authorities="org.bramantya.news.copygame.cod.FileProvider">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>
</manifest>
provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="oblehbleh" path="/"/>
</paths>
我按照这些帖子制作了代码
Android : FileProvider on custom external storage folder https://developer.android.com/training/sharing/send https://developer.android.com/reference/android/support/v4/content/FileProvider
还有很多我不记得的东西(我是编程的业余爱好者)
谢谢
答案 0 :(得分:0)
最后,通过将提供程序xml更改为此
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<paths>
<external-path name="provider" path="/" />
</paths>
</paths>