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

来源:互联网 发布:java continue label 编辑:程序博客网 时间:2024/05/22 13:58
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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();setListener();}private void initView() {// TODO Auto-generated method stubcontext = 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 stubiv_toAdd.setOnClickListener(this);}@Overridepublic void onClick(View view) {// TODO Auto-generated method stubswitch (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}}}@Overrideprotected 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