码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-06-05 10:23:24      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:动态添加拍照图片本地图片以及文件

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

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

标签:动态添加拍照图片本地图片以及文件

原文地址:http://blog.csdn.net/wuxin782515516/article/details/46367517

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!