2017-10-08 15 views
0

문제 1 : 같이 나는 PDF 나는 정상적인 방법을 사용하여 보내고있다공유 문서/Fileprovider

Environment.getExternalStorageDirectory() --> /storage/emulated/0/appname/downloads/sample.pdf 

에 저장된 :

File iconsStoragePath = Environment.getExternalStorageDirectory(); 
final String selpath = iconsStoragePath.getAbsolutePath() + "/appname/downloads/"; 

Intent intent = new Intent(Intent.ACTION_SEND); 
Uri selectedUri = Uri.parse(selpath + "/" + item.getFilename()); 
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString()); 
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension); 
intent.setType(mimeType); 
intent.putExtra(Intent.EXTRA_STREAM, selectedUri); 
startActivity(Intent.createChooser(intent, "Share File")); 

지금 결과는

입니다

enter image description here

파일은 파일 이름없이 공유됩니다.

문제 2 :

Unable to share. Please try again

Environment.getFilesDir() --> /data/data/com.myapp.name/files/myapp/downloads/sample.pdf 

매니페스트 파일 :

<application> 
<provider 
      android:name="android.support.v4.content.FileProvider" 
      android:authorities="${applicationId}.fileprovider" 
      android:exported="false" 
      android:grantUriPermissions="true"> 
      <meta-data 
       android:name="android.support.FILE_PROVIDER_PATHS" 
       android:resource="@xml/nnf_provider_paths" /> 
     </provider> 
</application> 

XML/nnf_provider_paths 나는 PDF 파일이 공유가 나에게 오류를 제공하지 않는 파일 공급자를 사용하려고 해요 때 .xml

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

코드 :

File path = new File(getFilesDir(), "downloads"); 
File file = new File(documentsPath + "/" + filename); 
        Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), AUTHORITY, file); 
Intent intent = new Intent(Intent.ACTION_SEND); 
intent.putExtra(Intent.EXTRA_STREAM, contentUri); 
intent.setType("application/pdf"); 
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
startActivity(intent); 

더 좋은 방법은 무엇입니까?

답변

0

저는 개인적으로 # 2를 선호합니다.

이것은을 통해 이미지를 공유하기 위해 나를 위해 작동하는 샘플 코드 조각입니다 : 접근 # 2 문제에 대해서는 ShareIntents

을 만들 ShareCompat를 사용하는 방법에 this 기사를 읽고 난 당신이 .setData 또는 .setDataAndUri을 놓치고있는 것 같아요 FileProviders :

public static void shareImage(Activity context) { 

    Log.d(TAG, "initializing share image"); 
    File imgPath = new File(context.getCacheDir(), DEFAULT_TEMP_DIR_NAME); 
    File newFile = new File(imgPath, DEFAULT_TEMP_FILENAME); 
    String authority = context.getPackageName() + ".fileprovider"; 
    Log.d(TAG, "authority: " + authority); 
    Uri contentUri = FileProvider.getUriForFile(context, authority, newFile); 

    if (contentUri != null) { 
     Intent shareIntent = ShareCompat.IntentBuilder.from(context) 
       .setType("image/png") 
       .setStream(contentUri) 
       .setSubject(context.getString(R.string.share_subject)) 
       .setText(context.getString(R.string.share_message)) 
       .setEmailTo(new String[]{"[email protected]"}) 
       .getIntent(); 
     shareIntent.setData(contentUri); 
     shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
     context.startActivity(Intent.createChooser(
       shareIntent, context.getString(R.string.share_chooser_title))); 
    } 
}