我的API版本是24
我正在使用相机和蓝牙。我的设备与其他设备通信。
第一个功能是,我将从我的应用程序中调用相机应用程序,拍照并将其发送到其他设备。
第二个功能是,我想导入我要发送的文件,并通过蓝牙发送到另一个设备。
这两个可以在不同的项目中运行,但两个不能在一个项目内执行。
这是我的错误“由以下原因引起:android.os.FileUriExposedException:file:///sdcard/Download/example.txt 通过 ClipData.Item.getUri() 暴露在应用程序之外”
我会把一段代码贴出来。
CameraActivity.class
private void sendTakePhotoIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
if (photoFile != null) {
photoUri = FileProvider.getUriForFile(this, getPackageName(), photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
蓝牙.class
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == DISCOVER_DURATION && requestCode == REQUEST_BLU) {
Intent i = new Intent();
i.setAction(Intent.ACTION_SEND);
i.setType("*/*");
File file = new File(exist);
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
PackageManager pm = getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(i, 0);
if (list.size() > 0) {
String packageName = null;
String className = null;
boolean found = false;
for (ResolveInfo info : list) {
packageName = info.activityInfo.packageName;
if (packageName.equals("com.android.bluetooth")) {
className = info.activityInfo.name;
found = true;
break;
}
}
if (!found) {
Toast.makeText(this, "Bluetooth not been found", Toast.LENGTH_LONG).show();
} else {
i.setClassName(packageName, className);
startActivity(i);
}
}
} else {
Toast.makeText(this, "Bluetooth is cancelled", Toast.LENGTH_LONG).show();
}
}
Manifests.xml - 提供者
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.tech.www.communicatever_110"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
res/xml/file_paths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/com.tech.www.communicatever_110/files/Pictures" />
如果您需要更多代码,请索取。
我知道这个答案已经很晚了,但我们希望它对将来的人有所帮助。
首先在您的 CameraActivity.class 中,您需要将“.providers”与您的包名称连接起来(它应该如下所示)
photoUri = FileProvider.getUriForFile(this, getPackageName() + ".providers", photoFile);
.providers
)android:authorities="com.tech.www.communicatever_110.providers"
name="my_images"
(即范围存储)时才可以使用 Context.getFilesDir()
,但如果您使用 Environment.getExternalStorageDirectory()
,则必须使用 name="external_files"
。 path="Android/data/com.tech.www.communicatever_110/files/Pictures"
path="."