'Failed to find configured root that contains /data/data/com.example.bluetoothdemo/
I'm trying to send a simple text file via Bluetooth using Android's default functionality. I've been trying to wrap my head around using Content Provider and Uri but keep encountering this error.
java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.example.bluetoothdemo/app_MyFiles/bluetooth.txt
This is my AndroidManifext.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetoothdemo">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
...
</application>
</manifest>
My Activity file
public class MyBluetoothActivity extends Activity {
public void clickToTransfer(View view) {
File directory = new File(this.getExternalFilesDir("") + "/MyFiles");
if (!directory.exists()) {
directory.mkdirs();
}
File dir = getApplicationContext().getDir("MyFiles", MODE_PRIVATE);
File file = new File(dir, "bluetooth.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
fos = new FileOutputStream(file, true);
osw = new OutputStreamWriter(fos);
osw.write("abc \n");
osw.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
osw.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.setComponent(new ComponentName("com.android.bluetooth", "com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
Uri uri = FileProvider.getUriForFile(MyBluetoothActivity.this, this.getApplicationContext().getPackageName() + ".provider", file);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
}
Also, my target sdk is sdk25. Can someone help me?
Edit: My provider_paths.xml file
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-files-path name="external_files" path="."/>
</paths>
Solution 1:[1]
Ok, as suggested, I was being dumb and used getDir() which cannot be used for FileProvider.
I switched back to using getExternalFilesDir() with the <external_file_paths> instead of just <external_paths> in the xml file and everything works now.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Elikill58 |
