很久以来,我一直在尝试为不同类型的文件提供跨应用程序共享意图从资产文件夹,但是工作总是存在问题。我进行了许多不同类型的解决方案,例如:
现在,这是我的仅将音频文件共享给whatsapp和Messenger的工作解决方案,所有其他应用程序都失败了。]
我在应用程序存储上创建新文件的功能:
public String getNewPathFromSbElem(SbElem sbElem, String fileName) {
AssetManager assetManager = getAssets();
String path = sbElem.soundPath;
String newName = fileName;
String newPath = getExternalFilesDir(null).getAbsolutePath() + File.separator + newName;
InputStream in = null;
OutputStream out = null;
File outFile;
File deleteFile = new File(newPath);
if(deleteFile.exists()) {
deleteFile.delete();
}
try {
in = assetManager.open(path);
outFile = new File(getExternalFilesDir(null), newName);
out = new FileOutputStream(outFile);
copyFile(in, out);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
return newPath;
}
我的2个功能可以在whatsapp或Messenger上共享:
public void shareOnWhatsApp(SbElem sbElem) {
final String newPath = getNewPathFromSbElem(sbElem, "_share_temp_file.mp3");
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.setType("audio/mp3");
whatsappIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(newPath));
whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
Log.d("error", "WhatsApp not installed");
}
}
public void shareOnMessenger (SbElem sbElem) {
final String newPath = getNewPathFromSbElem(sbElem, "_share_temp_file.mp3");
final File newFile = new File(newPath);
final Uri newUri = FileProvider.getUriForFile(this, getString(R.string.file_provider_authority), newFile);
final Integer SHARE_TO_MESSENGER_REQUEST_CODE = 1;
String mimeType = "audio/*";
ShareToMessengerParams shareToMessengerParams = ShareToMessengerParams.newBuilder(newUri, mimeType).build();
MessengerUtils.shareToMessenger(this, SHARE_TO_MESSENGER_REQUEST_CODE, shareToMessengerParams);
}
问题是,我希望能够从资产文件夹中共享.mp3,.jpg,.png,gmail,whatsapp,slack或支持该扩展名的任何类型的应用...
因此,几乎所有有关共享资产的在线1000题/文章都是通过使用自定义内容提供程序回答的,因此我尝试了以下共享功能:
public void shareBasic () {
// I added the test.jpg in the root of my asset folder
// Tried with content / file / 2 or 3 '/', with package name and with assets / ...
Uri theUri = Uri.parse("content:///com.MY_PACKAGE_NAME/test.jpg");
//Uri theUri = Uri.parse("content:///assets/test.jpg");
//Uri theUri = Uri.parse("file:///com.MY_PACKAGE_NAME/test.jpg");
//Uri theUri = Uri.parse("file:///asset.jpg");
Intent theIntent = new Intent(Intent.ACTION_SEND);
// Tried with jpeg / png / jpg ...
theIntent.setType("image/*");
theIntent.putExtra(Intent.EXTRA_STREAM, theUri);
theIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(theIntent);
}
这是我的Android清单:
<provider
android:name="MY_PACKAGE_NAME.MyContentProvider"
android:authorities="MY_PACKAGE_NAME"
android:grantUriPermissions="true"
android:exported="true" />
和文件提供者(每个教程几乎相同)
public class MyContentProvider extends ContentProvider {
@Override
public boolean onCreate() {
return true;
}
@Override
public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
return null;
}
@Override
public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal )
{
// TODO: Implement this method
return super.query( uri, projection, selection, selectionArgs, sortOrder, cancellationSignal );
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, ContentValues values) {
return null;
}
@Override
public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
@Override
public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
AssetManager am = getContext().getAssets();
String fileName = uri.getLastPathSegment();
if(fileName == null)
throw new FileNotFoundException();
AssetFileDescriptor fileDescriptor = null;
try {
fileDescriptor = am.openFd(fileName);
} catch (IOException e) {
e.printStackTrace();
}
return fileDescriptor;
}
}
我想我犯了很大的错误,因为从资产文件夹共享文件不应该这么麻烦,任何想法?
对于您的ContentProvider
解决方案:
您需要让您的query()
功能支持OpenableColumns
[您需要让您的getType()
函数返回实际的MIME类型
您的Intent
需要使用具体的MIME类型,而不是通配符
请参见this old sample project进行演示。