修改android4.4图库系列四(上)——自定义应用选择器,实现单个图片分享功能

图片分享功能原理同上篇文章(修改android4.4图库系列三——自定义应用选择器,实现图片设置功能)。效果图如下:

一、java代码

package com.example.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;

public class ShareActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_share);
		shareEvent("file:///mnt/sdcard/qq/2.jpg");
		//content://media/external/images/media/131  
	}
	
	public void shareEvent(final String imageUri){
		final List<HashMap<String, Object>> shareInfos = getShareApps(this, Intent.ACTION_SEND);
		
    	SimpleAdapter shareAdatper = new SimpleAdapter(this, 
    			                                       shareInfos, 
    			                                       R.layout.share_list_item, 
    			                                       new String[]{"appName"}, 
    			                                       new int[]{R.id.appName}){
    		@Override
    		public View getView(
    				int position,
    				View convertView,
    				ViewGroup parent) {
    			View view = super.getView(position, convertView, parent);
    			if(shareInfos.get(position).get("appPkgName").toString().equals("com.android.bluetooth")){
    				((ImageView)view.findViewById(R.id.appImage)).setImageResource(R.drawable.bluetooth);
    			} else{
    				((ImageView)view.findViewById(R.id.appImage)).setImageDrawable((Drawable)shareInfos.get(position).get("appImage"));
    			}
    			return view;
    		}
    	};
    	
        View popupWindowView = LayoutInflater.from(getApplicationContext())  
                .inflate(R.layout.view_popwindow, null);  
        final ListView menuList = (ListView)popupWindowView.findViewById(R.id.menuList);
        menuList.setAdapter(shareAdatper);
        
        final PopupWindow popupWindow = new PopupWindow(popupWindowView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        
        findViewById(R.id.shareImageView).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				popupWindow.setFocusable(true);
				popupWindow.setBackgroundDrawable(new BitmapDrawable()); 
				popupWindow.showAsDropDown(findViewById(R.id.shareImageView)); 
			}
		});
        
        menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				popupWindow.dismiss();
				Intent shareIntent = new Intent(Intent.ACTION_SEND);
				shareIntent.setClassName(
						shareInfos.get(position).get("appPkgName").toString(),
						shareInfos.get(position).get("appLauncherClassName").toString());
				shareIntent.setType("image/*");  
				shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(imageUri)); 
				
				startActivity(shareIntent);
			}
		});
	}
	
	public List<HashMap<String, Object>> getShareApps(Context context, String action) {
		List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();
		Intent intent = new Intent(action).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
		intent.setType("image/*");
		PackageManager pManager = context.getPackageManager();
		mApps = pManager.queryIntentActivities(intent,
				PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
		
		List<HashMap<String, Object>> appInfos = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < mApps.size(); i++) {
			ResolveInfo resolveInfo=(ResolveInfo) mApps.get(i);
			
			HashMap<String, Object> appInfo = new HashMap<String, Object>();
			appInfo.put("appImage", resolveInfo.loadIcon(pManager));
			appInfo.put("appName", resolveInfo.loadLabel(pManager).toString());
			appInfo.put("appPkgName", resolveInfo.activityInfo.packageName);
			appInfo.put("appLauncherClassName", resolveInfo.activityInfo.name);
			appInfos.add(appInfo);
		}
		
		return appInfos;
	}
}

二、xml布局代码

1、share_list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/appImage"
        android:layout_width="42dp"
        android:layout_height="42dp"
        android:layout_marginLeft="13dp"
        android:layout_marginRight="13dp"/>

    <TextView
        android:id="@+id/appName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:textSize="16dp" />

</LinearLayout>

2、view_popwindow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:background="@drawable/menu_dropdown_panel_holo_dark">
    <ListView 
        android:id="@+id/menuList"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:divider="#474747"
        android:dividerHeight="1dp"/>
</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值