2017-11-19 37 views
0

내 응용 프로그램간에 파일을 공유하고 싶습니다. File Provider를 사용하려고했지만 작동하지 않았습니다. 내 filepaths.xml이FileProvider를 사용하여 내부 저장소 공유

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.mdl.test.plug"> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".ModuleActivity" 
     android:label="@string/title_activity_module" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 

    </activity> 

    <provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="com.mdl.test.plug.fileprovider" 
     android:grantUriPermissions="true" 
     android:exported="false"> 
     <meta-data 
      android:name="android.support.FILE_PROVIDER_PATHS" 
      android:resource="@xml/filepaths" /> 
    </provider> 

</application> 

:

<paths> 
    <files-path path="/" name="allfiles" /> 
</paths 

과 핵심 응용 프로그램에서

, I가 시도 내 플러그 응용 프로그램에서 첫 번째

, 나는 파일 제공자를 설정 한 test.txt를 읽으십시오 :

Uri uri = Uri.parse("content://com.mdl.test.plug.fileprovider/allfiles/test.txt"); 
    InputStream is = null; 
    StringBuilder result = new StringBuilder(); 
    try { 
     is = getApplicationContext().getContentResolver().openInputStream(uri); 
     BufferedReader r = new BufferedReader(new InputStreamReader(is)); 
     String line; 
     while ((line = r.readLine()) != null) { 
      result.append(line); 
     } 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { if (is != null) is.close(); } catch (IOException e) { } 
    } 

그러나 그것은 권한 오류를 반환 :

Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord 

내가 원하는 것은 코어 플러그에서 파일을 읽을 대신 플러그의 코어 파일을 보낼 수 있습니다. 어떤 해결책?

감사

핵심 응용 프로그램은 그 Uri에 대한 권한이 없기 때문입니다
+0

test.txt 파일의 위치를 ​​알려주지 않았습니다. 전체 경로를 알려주십시오. 또한 getUriForFile()을 사용하여 URI를 설정해야합니다. – greenapps

답변

0

But It return a permission error

. 플러그인 앱에서 일부 Intent 기반 IPC (startActivity(), startService(), sendBroadcast() 등)를 사용하여 의 "데이터"패싯에 Uri을 포함시키고 사용하려면 플러그인 앱에서 권한을 부여 받아야합니다. FLAG_GRANT_READ_URI_PERMISSION.

다른 앱이 플러그인 앱에서 파일을 읽지 못하게하는 것은 어렵습니다.

minSdkVersion이 21 이상인 경우 자신의 ContentProvider을 내 보낸 다음 사용자 지정 권한으로 자체를 방어 할 수 있습니다. 핵심 앱은 해당 권한에 대해 <uses-permission> 요소를 가지고 있으며 원하는 경우 언제든지 ContentProvider의 요청을 할 수 있습니다. 사용자 정의 권한을 android:protectionLevel (signature)으로 지정하면 핵심 앱과 플러그인 앱에 동일한 서명 키로 서명해야하지만 통신은 안전해야합니다. 그러나 minSdkVersion이 21 세 미만인 경우 사용자 지정 사용 권한에 몇 가지 보안 문제가 있습니다.