Denver Shenal
Denver Shenal

Reputation: 419

Error when sharing a CSV file saved in internal storage

I'm creating a contact tracing application using Java, the results of tracing are saved as detections.csv inside /storage/emulated/0/Android/media/com.idcta.proj.app/Sensor/ folder.

enter image description here

I'm trying to share this file on a button click, and my Manifest looks like this

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.idcta.proj.app">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
        tools:ignore="ScopedStorage" />
   />

    <application
        android:name="com.idcta.proj.app.AppDelegate"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:hardwareAccelerated="true"
        android:requestLegacyExternalStorage="true">
        <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>
.....</appliction>

provider_paths.xml looks like:

  <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-path name="external_files" path="."/>
    </paths>

and inside my Contactlogs.java file inside the onCreate method

//request permissions for storage
requestPermissions();
            
//share button event
        
btn_share =(ImageButton)findViewById(R.id.btn_sharelogs);
btn_share.setOnClickListener(new View.OnClickListener()   {

     public void onClick(View v)  {

        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"detection.csv");
        Uri path = FileProvider.getUriForFile(Contactlogs.this,BuildConfig.APPLICATION_ID+".provider",file);
            
        Intent fileIntent = new Intent(Intent.ACTION_SEND);
         fileIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | 
         Intent.FLAG_GRANT_READ_URI_PERMISSION);
            
         fileIntent.setType("text/*");
         fileIntent.putExtra(Intent.EXTRA_STREAM, path);
         Toast.makeText(Contactlogs.this," "+path,Toast.LENGTH_LONG).show();
         startActivity(Intent.createChooser(fileIntent, "Send"));
                
       }
     });

But I keep getting the error:

E/ContentProviderNative: onTransact error from {P:29044;U:1000}
E/DatabaseUtils: Writing exception to parcel
    java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content://com.idcta.proj.app.provider/external_files/detection.csv from pid=29044, uid=1000 requires the provider be exported, or grantUriPermission()
        at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:873)
        at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:714)
        at android.content.ContentProvider$Transport.query(ContentProvider.java:245)
        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:120)
        at android.os.Binder.execTransactInternal(Binder.java:1165)
        at android.os.Binder.execTransact(Binder.java:1134)

Any idea what I'm doing wrong here?

My Build.Gradle settings:

  compileSdkVersion 30
    buildToolsVersion "30.0.1"

    defaultConfig {
        applicationId 'com.idcta.proj.app'
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.2.0"

Upvotes: 0

Views: 250

Answers (1)

blackapps
blackapps

Reputation: 9292

detections.csv inside /storage/emulated/0/Android/media/com.idcta.proj.app/Sensor/ folder.

That you got using getExternalMediaDirs()[0].

 /storage/emulated/0/Android/media/com.idcta.proj.app/Sensor/detections.csv 

So what did you code to have that path while saving?

Now when you want to serve the file you could use:

File file = new File(getExternalMediaDirs()[0], "Sensor/detections.csv");

Now you are hard coding your package name in the path.

Hardcoding should be avoided.

Upvotes: 1

Related Questions