gpt4 book ai didi

android - 通过 intent/Fileprovider 共享文档

转载 作者:行者123 更新时间:2023-11-30 00:19:08 24 4
gpt4 key购买 nike

问题1:我有一个pdf存储在

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:当我尝试使用文件提供程序时,pdf 未共享给我一个错误:

Unable to share. Please try again

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

list 文件:

<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.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);

哪种方法更好?

最佳答案

我个人更喜欢#2。阅读this有关如何使用 ShareCompat 创建 ShareIntents

的文章

关于方法 #2 的问题,我猜你缺少 .setData.setDataAndUri:

这是一个示例 fragment ,适用于我通过 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[]{"example@example.com"})
.getIntent();
shareIntent.setData(contentUri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(
shareIntent, context.getString(R.string.share_chooser_title)));
}
}

关于android - 通过 intent/Fileprovider 共享文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46633710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com