简体   繁体   中英

Reason for not being able to search the app-specific files in Android and solution

I am building a react native app which includes pdf file download and image upload functionality.

For doing file download, I am using react-native-fs and react-native-html-to-pdf and for image upload, I am using the axios and react-native-image-crop-picker .

I have already implemented the image upload functionality long time ago and it worked like charm.

In these days, I have implemented the pdf file download functionality and also it works well.

PROBLEM

  • Image upload does not work anymore.

Error message which is logged by react-native log-android looks like this: Network error _response: 'Could not retrieve file for uri /storage/emulated/0/Android/data/packageName/files/Pictures/75a08b1a-98de-4811-8b44-453f6f2f9bf8.jpg',

  • Downloaded pdf file is exist in app-specific folder but not discoverable in Android file manager.

Download pdf file path is like this: /storage/emulated/0/Android/data/packageName/files/data/user/0/packageName/files/fileName.pdf and I have checked this is definitely exist in exact directory.

  • Other apps(apks) specific files are discoverable in Android file manager.

What I am wondering is

  • Why only my react native package directory is not able to discoverable in file manager while even other app specific files are able?
  • Which setup I have missed or done incorrect?

What I did for test image upload

  • This functionality worked well so I have downloaded old committed version from Github and rebuild all of app. and tried to upload the image, but the same issue appeared.

What I assumed is

  • I can remember I have run command line related with adb with tcp port for fix issues long time ago.(I cannot remember what that command line is, I did google and run command line), I am assuming if that might be a problem.
  • All of the problem files are exist in the same directory: app specific directory which is allocated by android. I have read this android development documentation . so what I think is First, react native request an image to android phone and Second, android phone have no access to the package folder, Third, so android phone could not get that image and return Network error: could not retrieve image file.
  • Android phone has no permission to access the react native package directory so could not find the pdf file in file manager.

Actual codes

  • AndroidMainfest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="packageName">
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.CAMERA"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  <uses-feature android:name="android.hardware.camera" android:required="false"/>
  <uses-feature android:name="android.hardware.camera.front" android:required="false"/>
  <application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="false" android:theme="@style/AppTheme" android:usesCleartextTraffic="true" android:requestLegacyExternalStorage="true">
    <activity android:exported="true" android:name=".MainActivity" android:label="@string/app_name" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode" android:launchMode="singleTask" android:windowSoftInputMode="adjustResize">
      <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
      <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="invitation"/>
      </intent-filter>
      <intent-filter>
          <action android:name="android.intent.action.VIEW"/>
          <category android:name="android.intent.category.DEFAULT"/>
          <category android:name="android.intent.category.BROWSABLE"/>
          <data
              android:host="deeplinkUrl"
              android:scheme="https"/>
      </intent-filter>
    </activity>

    
    <!--  Facebook login  -->
    <activity android:name="com.facebook.FacebookActivity"
        android:configChanges=
                "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name" />
    <activity
        android:name="com.facebook.CustomTabActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="@string/fb_login_protocol_scheme" />
        </intent-filter>
    </activity>
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
    <meta-data android:name="com.facebook.sdk.ClientToken" android:value="@string/facebook_client_token"/>
    <!--  Facebook login  -->

  </application>
  <queries>
    <intent>
      <action android:name="android.intent.action.VIEW"/>
      <data android:mimeType="*/*"/>
    </intent>
  </queries>
</manifest>
  • android\app\src\main\res\xml\filepaths.xml (this is for android 11: this also copied from github issues forum)
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <external-path name="appdownloads" path="Download/" />
  <external-path name="files" path="/" />
  <root-path name="root" path="." />
</paths>

I think this is related to Android and local pc device. it would be nice if you can let us know about your development environment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM