2016-11-01 6 views
0

다음 설정이 있지만 타사 앱에서 파일을 열 때 파일을 찾을 수 없습니다.Android Java : 다른 앱에서 FileProvider를 사용하여 다운로드 한 파일을 여는 방법

파일을 다운로드 한 후 (바이트 단위로) 저장합니다.

public File storeFile(byte[] b, String ref) throws IOException { 
     String   path = String.valueOf(ref).replaceAll("/+", "_"); 
     final File  f = new File(A.getDir(path, Context.MODE_PRIVATE), "downloads"); 
     FileOutputStream out = new FileOutputStream(f); 
     out.write(b); 
     out.flush(); 
     out.close(); 
     return f; 
    } 

는 다음을 읽고 그것을 열어보십시오.

try { 
    Uri uri    = Uri.fromFile(storeFile(bytes, ref)); 
    MimeTypeMap mime  = MimeTypeMap.getSingleton(); 
    Intent  i   = new Intent(Intent.ACTION_VIEW); 
    String  type  = mime.getMimeTypeFromExtension(ext); 
    File  imagePath = new File(getFilesDir(), "downloads"); 
    File  file = new File(imagePath, uri.getPath()); 
    Uri   newUri  = FileProvider.getUriForFile(context, "com.do.app.fileprovider", file); 

    i.setDataAndType(uriContent, type); 
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    A.startActivity(i); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

그리고

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

매니페스트에서 경로

<?xml version="1.0" encoding="utf-8"?> 
<paths> 
    <files-path name="cloud_files" path="downloads"/> 
</paths> 

내가 누락 찾을 수없는 것. 그냥 imagePath.getAbsolutePath()f.getAbsolutePath() 비교

File  f = new File(A.getDir(path, Context.MODE_PRIVATE), "downloads"); 

에서 사용되는 완전히 다른 디렉토리입니다

+0

'FileProvider.getUriForFile()' 괜찮습니까? uri.toString()의 값을 말해주세요. – greenapps

답변

0
File  imagePath = File(getFilesDir(), "downloads"); 

;

동일한 경로 또는 File 인스턴스를 두 번 구성하지 말고 대신 하나의 변수를 사용하는 것이 좋습니다.

업데이트.

이제 코드가 더 엉망이라는 것을 알았습니다.

Uri uri    = Uri.fromFile(storeFile(bytes, ref)); 
Uri   newUri  = FileProvider.getUriForFile(context, "com.do.app.fileprovider", file); 

당신은 모든 대신 contentUriuri 또는 newUri을 사용하고 있지 않습니다. 그리고 왜 2/3 우 리가? 하나로 변경하십시오. 다음

Uri uri = FileProvider.getUriForFile(context 
     , "com.do.app.fileprovider" 
     , storeFile(bytes, ref)); 

그리고

는 사용 목적에 setDataAndType()와 그 URI를 넣어.

+0

괜찮 았어. 이제이 오류가 있습니다. IllegalArgumentException : android.support.v4.content.FileProvider에서 /data/data/com.do.app/imagename.jpg/downloads가 포함 된 구성된 루트를 찾지 못했습니다. $ SimplePathStrategy.getUriForFile – Relm

+0

파일 이름 뒤에 foldername이있는 경로! 당신은 보지 않습니까? 그리고 내가 제안한 코드를 사용하지 않았다. – greenapps

+0

storeFile()은 IOException을 발생시킵니다. 그러나 코드가 엉망이기 때문에 그것을 보지 못하고 있습니다. ref 매개 변수를 호출 할 때 그 값은 무엇입니까? – greenapps