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