动态添加拍照图片、本地图片以及文件

package com.example.filebrowser.activity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URI;
import java.util.ArrayList;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ImageView.ScaleType;

import com.example.filebrowser.R;
import com.example.filebrowser.application.IApplication;
import com.example.filebrowser.beans.FileBean;
import com.example.filebrowser.beans.ImageBean;
import com.example.filebrowser.file.ExDialog;

public class MainActivity extends BaseActivity implements OnClickListener {
	Context context;
	private LinearLayout ly_main;

	private ImageView iv_toAdd;
	private PopupWindow popupWindow = null;

	// �ϴ�ͼƬ
	private static final int SEL_PIC = 1;
	private static int count = 1;
	private LinearLayout linearLayout = null;
	private ArrayList<ImageBean> imageBeans;

	// �ϴ�����
	private static final String TAG = "AddFile";
	private static final int REQUEST_EX = 2;
	private ArrayList<FileBean> fileBeans;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		setListener();
	}

	private void initView() {
		// TODO Auto-generated method stub
		context = this;
		ly_main = (LinearLayout) findViewById(R.id.ly_main);
		iv_toAdd = (ImageView) findViewById(R.id.iv_toAdd);

		imageBeans = new ArrayList();
		fileBeans = new ArrayList();
		linearLayout = (LinearLayout) findViewById(R.id.ly_addFile);

	}

	private void setListener() {
		// TODO Auto-generated method stub
		iv_toAdd.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.iv_toAdd:
			new PopupWindowSex(context, ly_main);
			break;
		default:
			break;
		}
	}

	public class PopupWindowSex extends PopupWindow {
		public PopupWindowSex(final Context mContext, View parent) {
			super(mContext);
			try {
				View view = View.inflate(mContext, R.layout.pop_select, null);
				view.startAnimation(AnimationUtils.loadAnimation(mContext,
						R.anim.fade_ins));
				setWidth(LayoutParams.FILL_PARENT);
				setHeight(LayoutParams.WRAP_CONTENT);
				setBackgroundDrawable(new BitmapDrawable());
				// setFocusable(true);
				setOutsideTouchable(true);
				setContentView(view);
				showAtLocation(parent, Gravity.BOTTOM, 0, 0);
				update();
				LinearLayout ly_photo = (LinearLayout) view
						.findViewById(R.id.ly_photo);// ����
				LinearLayout ly_file = (LinearLayout) view
						.findViewById(R.id.ly_file);// ��ʿ
				LinearLayout ly_pop_del = (LinearLayout) view
						.findViewById(R.id.ly_pop_del);// ȡ��
				/**
				 * �ϴ�ͼƬ
				 */
				ly_photo.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						// �������ѡȡ
						if (count > 5) {
							Toast.makeText(context, "����ϴ�5��",
									Toast.LENGTH_SHORT).show();
							return;
						} else {
							count++;
							Intent intent = new Intent(
									Intent.ACTION_GET_CONTENT); // "android.intent.action.GET_CONTENT"
							// intent.addCategory(Intent.CATEGORY_OPENABLE);
							intent.setType("image/*");
							startActivityForResult(intent, SEL_PIC);
						}
						dismiss();
					}
				});
				/**
				 * �ϴ��ļ�
				 */
				ly_file.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						Intent intent = new Intent();
						intent.putExtra("explorer_title",
								getString(R.string.dialog_read_from_dir));
						intent.setDataAndType(
								Uri.fromFile(new File("/sdcard")), "*/*");
						intent.setClass(MainActivity.this, ExDialog.class);
						startActivityForResult(intent, REQUEST_EX);
						dismiss();
					}
				});

				/**
				 * ȡ��
				 */
				ly_pop_del.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {

						dismiss();
					}
				});

			} catch (Exception e) {
				// TODO: handle exception
			}

		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		String path = "";
		if (resultCode == RESULT_OK) {
			if (requestCode == SEL_PIC) {
				Uri uri = data.getData();
				String[] proj = { MediaStore.Images.Media.DATA };
				Cursor cursor = managedQuery(uri, proj, null, null, null);
				// ���Ҹ������ ����ǻ���û�ѡ���ͼƬ������ֵ
				int column_index = cursor
						.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
				// �����������ͷ ���������Ҫ����С�ĺ���������Խ��
				cursor.moveToFirst();
				// ���������ֵ��ȡͼƬ·��
				path = cursor.getString(column_index);
				ImageBean bean = new ImageBean();
				bean.setPath(path);
				imageBeans.add(bean);
				updateLayout();
			}
			if (requestCode == REQUEST_EX) {
				Uri uri = data.getData();
				//String[] proj = new String[]{MediaStore.Files.};
				Cursor cursor = managedQuery(uri, proj, null, null, null);
				// ���Ҹ������ ����ǻ���û�ѡ����ļ�������ֵ
				int column_index = cursor
						.getColumnIndexOrThrow("_data");
				// �����������ͷ ���������Ҫ����С�ĺ���������Խ��
				cursor.moveToFirst();
				// ���������ֵ��ȡ�ļ�·��
				path = cursor.getString(column_index);
				Log.e("MainActivity", path);
				FileBean fileBean = new FileBean();
				fileBean.setPath(path);
				fileBeans.add(fileBean);
				updateLayout2();
			}
		}

	}

	/**
	 * ����ͼƬ����
	 */
	private void updateLayout() {
		LinearLayout ll_horizontal = null;
		linearLayout.removeAllViews();
		for (int i = 0; i < imageBeans.size(); i++) {
			if (i % 4 == 0) {
				ll_horizontal = new LinearLayout(context);
				ll_horizontal.setOrientation(LinearLayout.HORIZONTAL);
				linearLayout.addView(ll_horizontal);
			}
			ImageView img = new ImageView(context);

			setImgLayoutParams(img);
			setImageBitmap(img, imageBeans.get(i).getPath());
			ll_horizontal.addView(img);
		}
	}
	/**
	 * ����ͼƬ����
	 */
	private void updateLayout2() {
		LinearLayout ll_horizontal = null;
		linearLayout.removeAllViews();
		for (int i = 0; i < fileBeans.size(); i++) {
			if (i % 4 == 0) {
				ll_horizontal = new LinearLayout(context);
				ll_horizontal.setOrientation(LinearLayout.HORIZONTAL);
				linearLayout.addView(ll_horizontal);
			}
			ImageView img = new ImageView(context);

			setImgLayoutParams(img);
			setImageBitmap(img, fileBeans.get(i).getPath());
			ll_horizontal.addView(img);
		}
	}

	/**
	 * ����imageview�ijߴ�
	 */
	private void setImgLayoutParams(ImageView img) {
		ViewGroup.LayoutParams lps = new android.view.ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		lps.width = (int) ((IApplication.getScreenWidth(context) - 20
				* IApplication.getScreenDensity(context) - 3 * 5 * IApplication
				.getScreenDensity(context)) / 4);
		lps.height = lps.width;
		img.setLayoutParams(lps);
		img.setScaleType(ScaleType.CENTER_CROP);
	}

	/**
	 * Ϊimageview����ͼƬ
	 */
	private void setImageBitmap(ImageView img, String url) {
		try {
			FileInputStream fis = new FileInputStream(url);
			img.setImageBitmap(BitmapFactory.decodeStream(fis));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值