如何与ShareCompat共享多个文件

时间:2017-12-25 16:18:09

标签: android android-intent

我的问题是如何在android中与import os, sys, math, pygame, pygame.mixer from pygame.locals import * class Unit(): def __init__(self): self.last = pygame.time.get_ticks() self.cooldown = 3000 def wait(self): now = pygame.time.get_ticks() if now - self.last >= self.cooldown: screen.fill(black) black = (0,0,0) red = (255,0,0) green = (0,255,0) blue = (0,0,255) white = (255,255,255) run_me = True stage1 = 1 stage2 = 1 stage3 = 1 stage4 = 1 st1 = True st2 = True st3 = True st4 = True screen_size = screen_width, screen_height = 600, 400 screen = pygame.display.set_mode(screen_size) pygame.display.set_caption('ha ha ha ha ha') clock = pygame.time.Clock() fps_limit = 60 while run_me: clock.tick(fps_limit) if stage1 == 1: screen.fill(red) wait(self) for event in pygame.event.get(): if event.type == pygame.QUIT: run_me = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: screen.fill(red) stage2 - 1 if event.type == pygame.KEYDOWN: if event.key == pygame.K_RIGHT: screen.fill(black) stage1 - 1 if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: screen.fill(blue) stage1 - 1 if event.type == pygame.KEYDOWN: if event.key == pygame.K_DOWN: screen.fill(green) stage1 - 1 if stage1 == 0: run_me = False pygame.display.flip() pygame.quit() 共享多个文件。

我的代码抛出异常:

  

android.content.ActivityNotFoundException:找不到要处理的Activity   意图{act = android.intent.action.SEND_MULTIPLE flg = 0x80001   pkg = com.google.android.gm(有剪辑)(有额外内容)}

我的代码是:

ShareCompat intentBuilder

每个应用程序(包名称)都会抛出错误。

3 个答案:

答案 0 :(得分:1)

经过艰苦的搜索之后,像stackoverflow这样的网站。我在@Ian Lake的博客文章中找到了一种对我有用的解决方案,也很有意义。

 ShareCompat.IntentBuilder shareIntent = ShareCompat.IntentBuilder.from((Activity) context)
                .setType("video/image/*");
        String[] files = parentDirectory.list();
        if (files != null) {
            for (String address : files) {
                File file = new File(address);
                File filePath = new File(context.getExternalFilesDir(null), "vids");  //vids is a directory local to my app in i.e storage/0/emulated/Android/data/com.myapp.videoeditor/vids
                File newFile = new File(filePath, file.getName());
                if (address.contains("cut_video")) {
                    Uri uri = FileProvider.getUriForFile(
                            context,
                            "com.myapp.videoeditor.fileprovider",
                            newFile);
                    shareIntent.addStream(uri);
                }
            }
        }
        Intent intent = shareIntent.getIntent();
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.startActivity(Intent.createChooser(intent, "Choose..."));

res / xml / file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-files-path name="external_files" path="vids/"/>
</paths>

清单

<application
....
....>
 <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.myapp.videoeditor.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <!-- ressource file to create -->
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths">
            </meta-data>
...
...
</application>

答案 1 :(得分:0)

您是否在AndroidManifest.xml中为应该接收文件的活动添加了意图过滤器?

<intent-filter>
    <action android:name="android.intent.action.SEND_MULTIPLE" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="image/*" />
</intent-filter>

答案 2 :(得分:0)

以下对我有用的东西

ShareCompat.IntentBuilder shareIntent = ShareCompat.IntentBuilder.from(activity).setType("image/*");
    for(String address : addresses ) {
        File file = new File(address);
        Uri imageUri = FileProvider.getUriForFile(
                activity,
                "app.signature.fileprovider",
                file);
        shareIntent.addStream(imageUri);
    }
    Intent intent=shareIntent.getIntent();
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(intent, "Choose Application"));

您应该使用addStream()添加多个文件。