动态添加拍照图片、本地图片以及文件
来源:互联网 发布: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
- 动态添加拍照图片、本地图片以及文件
- android 拍照裁剪以及本地图片裁剪工具类使用
- Android 拍照 以及从本地选择图片 上传
- Android 拍照 以及从本地选择图片 上传
- 通过GridView仿微信动态添加本地图片
- 用RecyclerView实现动态添加本地图片
- Android 选取图片、手机拍照以及保存图片到本地、读取图片
- ionic 拍照选择本地图片
- Android拍照,选择本地图片
- Android 拍照获取本地图片
- android获取本地图片或拍照图片
- 获取本地图片或拍照图片
- android获取本地图片或拍照图片
- 获取本地图片或拍照图片
- android获取本地图片或拍照图片
- Android 实例讲解添加本地图片和调用系统拍照图片
- 打开相机拍照和从本地相册获取图片以及图片的剪切
- Android之本地相册图片选取和拍照以及图片剪辑
- ubuntu下mysql中文乱码问题完美解决
- android开发之流量监控
- [部署篇13]VMWare搭建Openstack——Swift的安装与部署
- content-type引发的CDN错误
- 4.19&&4.20
- 动态添加拍照图片、本地图片以及文件
- 图像亮度变换函数
- cms系统是什么意思
- jemalloc的使用
- 求两个整数的最大公约数及其应用
- HDU 1285 【简单拓扑】
- 信号量与管程
- JavaScript位操作及其简单应用
- Web应用中大部分跳转路径如何书写