1

My API Version is 24

I'm using Camera and Bluetooth. And my device communicating other device.

The first function is, I'm going to call up the camera app from my app, take a picture and send it to other device.

The second function is, I want to import the file I want to send and send it to another device through Bluetooth.

These two can be run on different projects, but two cannot be executed within one project.

this is my error "Caused by: android.os.FileUriExposedException: file:///sdcard/Download/example.txt exposed beyond app through ClipData.Item.getUri()"

I'll put a piece of the code up.

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

        }
    }
}

Bluetooth.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

<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" />

If you need any more code, please request it.

  • Does this answer your question? [android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()](https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed) – Ryan1729 Apr 16 '20 at 22:23

1 Answers1

0

I know this answer is quite late but let's hope it helps someone in future.
First of all in your CameraActivity.class you need to concatenate ".providers" with your package name (it should look like this)
photoUri = FileProvider.getUriForFile(this, getPackageName() + ".providers", photoFile);

And you'll have to do the same inside Manifest.xml (add .providers with your package name)
android:authorities="com.tech.www.communicatever_110.providers"

Thirdly, inside res/xml/file_paths.xml you can use name="my_images" only if you're using Context.getFilesDir() (i.e. scoped storage) but if you're using Environment.getExternalStorageDirectory() then you must use name="external_files".

Last but not least, you might not need to put the whole path inside file_paths.xml
path="Android/data/com.tech.www.communicatever_110/files/Pictures"
Try replacing it with: path="."