2017-01-19 10 views
5

Dahili depolama biriminde dosya alabilen bir uygulama oluşturdum. Harici bir uygulamaya (örneğin PF görüntüleyici veya Fotoğraflar) sahip bir dosyayı açmak için şu kılavuzları takip etmeyi denedim: the official guide, topic1, topic2, topic3 ve topic4 ancak başarı olmadan. Benim tezahürNeden bir FileProvider kullanıyorsunuz Harici uygulamalar ile INTERNAL STORAGE dosyasından dosya açılamıyor?

<provider 
    android:name="android.support.v4.content.FileProvider" 
    android:authorities="com.myapp.chatcher" 
    android:exported="false" 
    android:grantUriPermissions="true"> 
    <meta-data 
     android:name="android.support.FILE_PROVIDER_PATHS" 
     android:resource="@xml/file_paths" /> 
</provider> 

paketim değerindeki

: package="com.myapp.catcher"

benim file_paths.xml

<paths 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <files-path name="projection" path="." /> 
</paths> 

kodum

İşte

benim kodudur örneğin

PRIVATE -> shelf1 -> my files 

     -> shelf2 -> my files 

     -> shelfN -> my files 

: data/user/0/com.myapp.chatcher/files/PRIVATE/testshelf/Screenshot_2017-01-04-09-45-13.png

newFile.getAbsolutePath() baskı sonucu bu kod içinde seçici açar

/data/user/0/com.myapp.chatcher/files/PRIVATE/bogl/imagetest.jpg 

olduğunu

String fileName = path.substring(path.lastIndexOf("/") + 1); 
String shelf = path.substring(path.lastIndexOf("PRIVATE") + 8, path.lastIndexOf("/")); 
File filePath = new File(mContext.getFilesDir(), "PRIVATE".concat("/").concat(shelf).concat("/")); 
File newFile = new File(filePath, fileName); 
Uri contentUri = FileProvider.getUriForFile(mContext, "com.myapp.chatcher", newFile); 

Intent myIntent = new Intent(); 
myIntent.setAction(Intent.ACTION_VIEW); 
myIntent.setData(contentUri); 
myIntent.setType(mimeType); 
myIntent.setFlags(FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION); 
mContext.startActivity(myIntent); 
böyle bir hiyerarşi yarattı "Fotoğrafları" na tıklayabildiğim ve daha sonra "Photos app" ı görüntülemeden önce imagetest.jpg 'i, ancak tüm resimlerin olduğu klasörü gösterir. Bir pdf dosyası ile çalışırsanız, pdf açılmaz ve "ortam yok" iletisiyle birlikte bir tost görünür.

Kodumdaki sorun nedir?

+0

Yorumlar genişletilmiş bir tartışma değildir; Bu konuşma [sohbet etmek için taşındı] (http://chat.stackoverflow.com/rooms/133634/discussion-on-question-by-machoprogrammer-why-using-a-fileprovider-i-cant-open). –

cevap

4

Bir android uzmanı olan @greenapps'e teşekkürler, sorunun sağlayıcıda değil, amacında olduğunu gördüm. Bunun yerine

:

Intent myIntent = new Intent(); 
myIntent.setAction(Intent.ACTION_VIEW); 
myIntent.setData(contentUri); 
myIntent.setType(mimeType); 
myIntent.setFlags(FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION); 
mContext.startActivity(myIntent); 

bunu yapmak gerekir:

Intent myIntent = new Intent(Intent.ACTION_VIEW); 
myIntent.setDataAndType(contentUri, mimeType); 
myIntent.setFlags(FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION); 
mContext.startActivity(myIntent); 
+0

Sorunum bununla çözülmedi. – SimpleCoder

+0

2 arasındaki fark nedir ??? Tam olarak aynı olmalı. – Hrk

+0

https://developer.android.com/guide/components/intents-filters.html#Types _HRI ve MIME türünü ayarlamak isterseniz, setData() ve setType() işlevlerini çağırmayın çünkü her biri değeri geçersiz kılar diğerinin. Her zaman URI ve MIME türünü ayarlamak için setDataAndType() kullanın. Neden olduğunu bilmiyorum ama bunu resmi belgede okuyabilirsiniz. – michoprogrammer

İlgili konular