Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 如何實現本地圖片直接分享到微信、來往、QQ等,直接分享圖片到指定應用

Android 如何實現本地圖片直接分享到微信、來往、QQ等,直接分享圖片到指定應用

編輯:關於Android編程

例如:要在app中直接分享圖片到微信、來往等,而不調用他們的sdk

直接貼代碼如下;

 

 

	public void shareImage(Bitmap bitmap) {
		try {
			Uri uriToImage = Uri.parse(MediaStore.Images.Media.insertImage(
					mContext.getContentResolver(), bitmap, null, null));
			Intent shareIntent = new Intent();
			shareIntent.setAction(Intent.ACTION_SEND);
			shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
			shareIntent.setType("image/*");
			// 遍歷所有支持發送圖片的應用。找到需要的應用
			PackageManager packageManager = mContext.getPackageManager();
			List resolveInfoList = packageManager
					.queryIntentActivities(shareIntent,
							PackageManager.GET_INTENT_FILTERS);
			ComponentName componentName = null;
			for (int i = 0; i < resolveInfoList.size(); i++) {
				if (TextUtils.equals(
						resolveInfoList.get(i).activityInfo.packageName,
						YouNeedAppPackageName)) {
					componentName = new ComponentName(
							resolveInfoList.get(i).activityInfo.packageName,
							resolveInfoList.get(i).activityInfo.name);
					break;
				}
			}
			// 已安裝**
			if (null != componentName) {
				shareIntent.setComponent(componentName);
				mContext.startActivity(shareIntent);
			} else {
				ContextUtil.getInstance().showToastMsg("請先安裝**");
			}
		} catch (Exception e) {
			ContextUtil.getInstance().showToastMsg("分享圖片到**失敗");
		}
	}


 

 

 

參考的文章如下:

 

android設置應用程序默認值

Android中有時打開一個文件,系統會彈出一個應用程序選擇列表供你選擇.如果你把某個應用程序設為默認值,則不會彈出選擇框,而會直接調用該程序.

設置方法如下:

PackageManager pm = getPackageManager();
IntentFilterfilter = new IntentFilter();
filter.addCategory("android.intent.category.DEFAULT");
ComponentName component = newComponentName("YourClearAppPakageName","YourClearAppClassName");
ComponentName[] components = new ComponentName[]{new ComponentName("YourPreferAppPakageName","YourPreferAppClassName"), component};

pm.clearPackagePreferredActivities("YourClearAppPakageName");
pm.addPreferredActivity(filter,IntentFilter.MATCH_CATEGORY_EMPTY, components, component);

列入修改系統默認Launhcer但不是唯一的Launcher

PackageManager pm = getPackageManager();  
IntentFilter filter = new IntentFilter(); 
filter.addAction("android.intent.action.MAIN");
 filter.addCategory("android.intent.category.HOME"); 
filter.addCategory("android.intent.category.DEFAULT");
  Context context = getApplicationContext();
 ComponentName component = new ComponentName(context.getPackageName(), TestReplaceHomeAppActivity.class.getName());  ComponentName[] components = new ComponentName[] {new ComponentName("com.android.launcher", "com.android.launcher.Launcher"), component};  
pm.clearPackagePreferredActivities("com.android.launcher"); pm.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_EMPTY, components, component);


修改默認浏覽器
PackageManager packageManager =context.getPackageManager(); String str1 = "android.intent.category.DEFAULT"; String str2 = "android.intent.category.BROWSABLE"; String str3 = "android.intent.action.VIEW";
//設置默認項的必須參數之一,用戶的操作符合該過濾器時,默認設置起效 IntentFilter filter = new IntentFilter(str3); filter.addCategory(str1); filter.addCategory(str2); filter.addDataScheme("http"); //設置浏覽頁面用的Activity ComponentName component = new ComponentName(context .getPackageName(), BrowserActivity.class.getName());
Intent intent = new Intent(str3); intent.addCategory(str2); intent.addCategory(str1); Uri uri = Uri.parse("http://"); intent.setDataAndType(uri, null);
//找出手機當前安裝的所有浏覽器程序 List resolveInfoList = packageManager .queryIntentActivities(intent, PackageManager.GET_INTENT_FILTERS);
int size = resolveInfoList.size(); ComponentName[] arrayOfComponentName = newComponentName[size]; for (int i = 0; i < size; i++) { ActivityInfo activityInfo =resolveInfoList.get(i).activityInfo; String packageName = activityInfo.packageName; String className = activityInfo.name; //清除之前的默認設置 packageManager.clearPackagePreferredActivities(packageName); ComponentName componentName = newComponentName(packageName, className); arrayOfComponentName[i] = componentName; } packageManager.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_SCHEME,arrayOfComponentName, component);

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved