2017-05-23 108 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); 
    }