Android Studio-努力共享应用资产中的图像/音频/视频文件

问题描述 投票:1回答:1

很久以来,我一直在尝试为不同类型的文件提供跨应用程序共享意图从资产文件夹,但是工作总是存在问题。我进行了许多不同类型的解决方案,例如:

  • 尝试使用自定义内容提供程序(herehere,..]
  • 尝试将文件保存在本地存储中(例如here
  • 和其他人,但由于它不起作用,我不再有链接。。

现在,这是我的仅将音频文件共享给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;
    }
}

我想我犯了很大的错误,因为从资产文件夹共享文件不应该这么麻烦,任何想法?

java android android-intent share android-assets
1个回答
0
投票

对于您的ContentProvider解决方案:

  • 您需要让您的query()功能支持OpenableColumns

  • [您需要让您的getType()函数返回实际的MIME类型

  • 您的Intent需要使用具体的MIME类型,而不是通配符

请参见this old sample project进行演示。

© www.soinside.com 2019 - 2024. All rights reserved.