android 选择本地图片
来源:互联网 发布:全球票房数据库 编辑:程序博客网 时间:2024/04/27 21:52
一、先建第一个activity
1、xml:folder_open.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/white"><Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> <TextView android:id="@+id/fileText" android:gravity="center" android:textSize="15dp" android:textColor="#217ac6" android:layout_width="fill_parent" android:layout_height="80dp" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:textSize="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="照片描述:" /> <EditText android:id="@+id/fileDesc" android:textSize="15dp" android:layout_width="fill_parent" android:layout_height="wrap_content"android:inputType="text" /> </LinearLayout><Button android:id="@+id/upload" android:layout_width="100dp" android:layout_height="40dp" android:layout_marginLeft="100dp" android:text="上传" /> </LinearLayout>
2、建立对应的activity:
public class UploadPicActivity extends Activity{public static final int FILE_RESULT_CODE = 1;private TextView textView;private Button select;private Button upload;private EditText editText;private String filePath;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.folder_open);findViews();select.setText("选择照片");select.setOnClickListener(selector);upload.setOnClickListener(uploader);}private void findViews() {select = (Button)findViewById(R.id.button);textView = (TextView)findViewById(R.id.fileText);upload = (Button)findViewById(R.id.upload);editText = (EditText)findViewById(R.id.fileDesc);}private View.OnClickListener selector = new OnClickListener() {public void onClick(View v) {Intent intent = new Intent(UploadPicActivity.this,FileManager.class);startActivityForResult(intent, FILE_RESULT_CODE);}};private View.OnClickListener uploader = new OnClickListener() {public void onClick(View v) {String fileDesc = editText.getText().toString();//需判断文件是否为照片}};@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {}//消息框private void showDialog(String mess){new AlertDialog.Builder(UploadPicActivity.this).setMessage(mess).setNegativeButton("确定",new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which){}}).show();}}
二、建立第二个activity
1、xml:filelist.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="250dp" android:layout_height="400dp" android:orientation="vertical" android:background="@drawable/white"> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="250dp" > </ListView> <LinearLayout android:gravity="center" android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:background="@drawable/white"><Button android:id="@+id/buttonConfirm" android:layout_width="100dp" android:layout_height="40dp" android:layout_marginLeft="10dp" android:text="确定" /> <Button android:id="@+id/buttonCancle" android:layout_width="100dp" android:layout_height="40dp" android:layout_marginLeft="30dp" android:text="取消" /> </LinearLayout> <ImageView android:id="@+id/imageView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="centerInside" android:contentDescription="@string/pict"/> </LinearLayout>
2、对应的activity:
public class FileManager extends ListActivity {private List<String> items = null;private List<String> paths = null;private String rootPath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();//'/mnt/sdcard';//当前文件路径private String curPath = "/";private ImageView image;private Button buttonConfirm; private MyAdapter adapter;@Overrideprotected void onCreate(Bundle icicle) {super.onCreate(icicle);//设置窗口无标题requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.filelist);findViews();buttonConfirm.setOnClickListener(new OnClickListener() {public void onClick(View v) {Intent data = new Intent(FileManager.this, UploadPicActivity.class);Bundle bundle = new Bundle();bundle.putString("file", curPath);data.putExtras(bundle);setResult(2, data);finish();}});Button buttonCancle = (Button) findViewById(R.id.buttonCancle);buttonCancle.setOnClickListener(new OnClickListener() {public void onClick(View v) {finish();}});getFileDir(rootPath);}private void findViews() {buttonConfirm = (Button) findViewById(R.id.buttonConfirm);image = (ImageView)findViewById(R.id.imageView);}private void getFileDir(String filePath) {items = new ArrayList<String>();paths = new ArrayList<String>();File f = new File(filePath);File[] files = f.listFiles();if (!filePath.equals(rootPath)) {items.add("b1");paths.add(rootPath);items.add("b2");paths.add(f.getParent());}for (int i = 0; i < files.length; i++) {File file = files[i];/*.android_secure 应该文件是个隐藏文件,我每次点击就出错,干脆直接不显示了*/if((rootPath+"/.android_secure").equals(file.getPath())){continue;}items.add(file.getName());paths.add(file.getPath());} adapter = new MyAdapter(this, items, paths);setListAdapter(adapter);}private int save = -1;@Overrideprotected void onListItemClick(ListView parent, View v, int position, long id) {/*设置选中的文件高亮显示if(parent != null && parent.getChildAt(position) != null){parent.getChildAt(position).setBackgroundColor(Color.CYAN);}try{if(save != -1 && save != position){//报异常parent.getChildAt(save).setBackgroundColor(Color.WHITE);}}catch (Exception e) {Log.e("NULL", e.toString());//这边一直报空值。} */File file = new File(paths.get(position));if (file.isDirectory()) {curPath = paths.get(position);getFileDir(paths.get(position));} else {curPath = paths.get(position); //刷新界面,高亮显示adapter = new MyAdapter(this, items, paths);setListAdapter(adapter);}//将选中的图片显示String filename = file.getName();if(filename != null && filename.endsWith(".bmp") || filename.endsWith(".gif") || filename.endsWith(".jpg")|| filename.endsWith(".jpeg") || filename.endsWith(".png")){Bitmap bitmap = getLoacalBitmap(file.getPath());if(bitmap != null){image.setImageBitmap(bitmap);}bitmap = null;}save = position;}//根据图片URL返回Bitmapprivate Bitmap getLoacalBitmap(String url) { try { FileInputStream fis = new FileInputStream(url); return Bitmap.createBitmap(BitmapFactory.decodeStream(fis)); } catch (FileNotFoundException e){ e.printStackTrace(); return null; }}}
三、创建Adapter
1、xml:filerow.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" > <ImageView android:id="@+id/icon" android:layout_width="30dip" android:layout_height="30dip"/> <TextView android:id="@+id/text" android:layout_gravity="center_horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@drawable/darkgray"/> </LinearLayout>
2、创建Adapter:MyAdapter
public class MyAdapter extends BaseAdapter{ private LayoutInflater mInflater; private Bitmap mIcon1; private Bitmap mIcon2; private Bitmap mIcon3; private Bitmap mIcon4; private List<String> items; private List<String> paths; private int position = -1; public MyAdapter(Context context,List<String> it,List<String> pa){ mInflater = LayoutInflater.from(context); items = it; paths = pa; mIcon1 = BitmapFactory.decodeResource(context.getResources(),R.drawable.back01);//返回根目录 mIcon2 = BitmapFactory.decodeResource(context.getResources(),R.drawable.back02);//返回上一目录 mIcon3 = BitmapFactory.decodeResource(context.getResources(),R.drawable.folder);//目录 mIcon4 = BitmapFactory.decodeResource(context.getResources(),R.drawable.doc);//文件 } public void setSelectItem(int position){ this.position = position; } @Override public int getCount(){ return items.size(); } @Override public Object getItem(int position){ return items.get(position); } @Override() public long getItemId(int position){ return position; } @Override public View getView(int position,View convertView,ViewGroup parent){ ViewHolder holder; if(convertView == null){ convertView = mInflater.inflate(R.layout.filerow, null); holder = new ViewHolder(); holder.text = (TextView) convertView.findViewById(R.id.text); holder.icon = (ImageView) convertView.findViewById(R.id.icon); convertView.setTag(holder); } else{ holder = (ViewHolder) convertView.getTag(); } File f = new File(paths.get(position).toString()); if(items.get(position).toString().equals("b1")){ holder.text.setText("返回根目录.."); holder.icon.setImageBitmap(mIcon1); } else if(items.get(position).toString().equals("b2")){ holder.text.setText("返回上一层.."); holder.icon.setImageBitmap(mIcon2); } else{ holder.text.setText(f.getName()); if(f.isDirectory()){ holder.icon.setImageBitmap(mIcon3); } else{ holder.icon.setImageBitmap(mIcon4); } } //设置选中文件的颜色 if (this.position == position) { convertView.setBackgroundColor(Color.LTGRAY); } else { convertView.setBackgroundColor(Color.TRANSPARENT); } return convertView; } private class ViewHolder{ TextView text; ImageView icon; }}
- android 选择本地图片
- Android本地图片选择
- android imageview 选择本地图片
- Android拍照,选择本地图片
- 【Android】本地图片选择(打开媒体库,选择图片)
- 【Android】本地图片选择(打开媒体库,选择图片)
- 【Android】本地图片选择(打开媒体库,选择图片)
- 【Android】本地图片选择(打开媒体库,选择图片)
- 【Android】本地图片选择(打开媒体库,选择图片)
- 【Android】本地图片选择(打开媒体库,选择图片)
- 【Android Intent】本地图片选择(打开媒体库,选择图片)
- 【Android】本地图片选择(打开媒体库,选择图片)
- android本地图片选择【类似于浏览】
- android-显示选择本地图片或拍照
- Android选择本地图片或者照片裁剪
- android拍照和本地选择图片
- android 手机拍照选择本地图片
- android:ImageView选择本地图片并显示
- iPhone入门 - UIAlertView用法
- qt 多线程
- pdf文档转化成图片
- 图像匹配方法
- JavaScript页面跳转技术笔记
- android 选择本地图片
- web.xml 中的listener、 filter、servlet 加载顺序及其详解
- Study《Google Map中的瓦片》
- Qt博客实例
- 实用代码
- iPhone开发:在UIAlertView中显示进度条
- QT基本问题
- Core Animation学习----CAkeyframeAnimation
- 业内人士揭理财潜规则:多赚归银行 亏损买者扛