2017-05-23 107 views
0

我需要共享图像,并在android共享菜单中添加“保存图像”项目,我在9gag应用程序中看到类似这样的内容,它们在共享菜单中具有“保存”项目,共享菜单似乎是底部表单。但是如何实现? enter image description here如何在android中自定义共享菜单?

我做了什么: 我加在清单意图过滤空活动,其推出的服务,这项服务下载图像

<activity 
      android:name=".model.services.ShareActivity" 
      android:icon="@drawable/download_icon" 
      android:label="Save"> 
      <intent-filter 
       android:label="Save" 
       android:icon="@drawable/download_icon"> 
       <action android:name="android.intent.action.SEND" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <data android:mimeType="image/*"/> 
      </intent-filter> 
     </activity> 

现在我已经在共享菜单这个图标,它的工作原理,但这个图标也出现在其他应用程序的共享菜单中,我只需要在我的应用程序中显示它,我如何才能使它变得私人或什么?

+0

该图标是否也出现在其他应用程序中?你不想在其他活动中表示意思吗? –

+0

Yeap,它出现在其他应用程序中,即使android画廊有我的应用程序的共享图像菜单中的“保存”项目,我想排除我的“保存”活动从其他应用程序共享菜单 –

回答

0

好的,我找到了一个解决方案。首先 - 我们需要处理图像保存意图的活动,此活动可以启动服务或其他内容。我做这样的:

public class ShareActivity extends Activity { 
    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Bundle extras = getIntent().getExtras(); 
     String url = extras.getString("url"); 
     String name = extras.getString("name"); 
     String description = extras.getString("description"); 
     SaveImageService.downloadFile(url, name, description); 
     finish(); 
    } 
} 

SaveImageService具有处理图像保存到SD卡 二静态方法,我们需要添加一些文字在清单:

<activity 
     android:name=".model.services.ShareActivity" 
     android:icon="@drawable/download_icon" 
     android:label="Save"> 
     <intent-filter 
      android:label="Save" 
      android:icon="@drawable/download_icon"> 
      <action android:name="com.my_app.random_text.SAVE_IMAGE" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <data android:mimeType="image/*"/> 
     </intent-filter> 
    </activity> 

这里意图过滤器自定义操作(这很重要),这个自定义操作只是一些字符串,而不是应用程序包或什么(我只是因为喜欢它而使用包名)。 接下来,我们需要添加这个活动,分享菜单列表:

这将让位乌里从ImageView的

在其它应用程序共享
// Returns the URI path to the Bitmap displayed in specified ImageView 
    static public Uri getLocalBitmapUri(ImageView imageView) { 
     // Extract Bitmap from ImageView drawable 
     Drawable drawable = imageView.getDrawable(); 
     Bitmap bmp = null; 
     if (drawable instanceof BitmapDrawable) { 
      bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); 
     } else { 
      return null; 
     } 
     // Store image to default external storage directory 
     Uri bmpUri = null; 
     try { 
      File file = new File(Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png"); 
      file.getParentFile().mkdirs(); 
      FileOutputStream out = new FileOutputStream(file); 
      bmp.compress(Bitmap.CompressFormat.JPEG, 80, out); 
      out.close(); 
      bmpUri = Uri.fromFile(file); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return bmpUri; 
    } 

,这将收集可以共享图像加上我们保存所有的应用程序图像意图

public void shareExcludingApp(Context ctx, PhotoView snapImage) { 
     // Get access to the URI for the bitmap 
     Uri bmpUri = ShareTool.getLocalBitmapUri(snapImage); 
     if (bmpUri == null) return; 

     List<Intent> targetedShareIntents = new ArrayList<>(); 
     //get all apps which can handle such intent 
     List<ResolveInfo> resInfo = ctx.getPackageManager().queryIntentActivities(createShareIntent(bmpUri), 0); 
     if (!resInfo.isEmpty()) { 
      for (ResolveInfo info : resInfo) { 
       Intent targetedShare = createShareIntent(bmpUri); 
       //add all apps excluding android system and ourselves 
       if (!info.activityInfo.packageName.equals(getContext().getPackageName()) 
         && !info.activityInfo.packageName.contains("com.android")) { 
        targetedShare.setPackage(info.activityInfo.packageName); 
        targetedShare.setClassName(
          info.activityInfo.packageName, 
          info.activityInfo.name); 
        targetedShareIntents.add(targetedShare); 
       } 
      } 
     } 
     //our local save feature will appear in share menu, intent action SAVE_IMAGE in manifest 
     Intent targetedShare = new Intent("com.my_app.random_text.SAVE_IMAGE"); //this is that string from manifest! 
     targetedShare.putExtra(Intent.EXTRA_STREAM, bmpUri); 
     targetedShare.setType("image/*"); 
     targetedShare.setPackage(getContext().getPackageName()); 
     targetedShare.putExtra("url", iSnapViewPresenter.getSnapUrlForSave()); 
     targetedShare.putExtra("name", iSnapViewPresenter.getSnapNameForSave()); 
     targetedShare.putExtra("description", iSnapViewPresenter.getSnapDescriptionForSave()); 
     targetedShareIntents.add(targetedShare); 

     //collect all this intents in one list 
     Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), 
       "Share Image"); 

     chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, 
       targetedShareIntents.toArray(new Parcelable[targetedShareIntents.size()])); 

     ctx.startActivity(chooserIntent); 
    }