2017-05-23 1 views
0

이미지를 공유하고 안드로이드 공유 메뉴에 "이미지 저장"항목을 추가해야합니다. 9gag 앱에서 이걸 보았고, 공유 메뉴에 "저장"항목이 있고 공유 메뉴가 맨 아래에있는 것으로 보입니다. 그러나 어떻게 달성 될 수 있습니까? 내가 무슨 짓을했는지 enter image description hereandroid에서 공유 메뉴를 맞춤 설정하는 방법은 무엇입니까?

: 나는, 서비스와이 서비스 다운로드 이미지

<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, 다른 응용 프로그램에도 표시되며 안드로이드 갤러리에서도 공유 이미지 메뉴에 내 응용 프로그램의 "저장"항목이 있습니다. 다른 응용 프로그램 공유 메뉴에서 "저장"활동을 제외하고 싶습니다. –

답변

0

좋아, 해결책을 찾았습니다. 첫째, 이미지 저장 의도를 처리 할 활동이 필요합니다.이 활동은 서비스 등을 시작할 수 있습니다. 나는 이런 식으로합니다

<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> 

여기에 텐트 필터는 사용자 지정 작업이 있습니다 SaveImageService은 SD 카드에 두 번째 저장 이미지를 처리하는 정적 메소드를 가지고

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(); 
    } 
} 

, 우리는 매니페스트에 텍스트를 추가 할 필요가 (이것은 중요합니다)이 사용자 지정 작업은 응용 프로그램 패키지가 아닌 일부 문자열입니다 (필자가 좋아하기 때문에 패키지 이름을 사용하고 있습니다).

이 이미지 뷰

에서 다른 응용 프로그램에
// 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); 
    } 
관련 문제