分类:
版权声明:本文为博主原创文章,未经博主允许不得转载。
最近看了很多论坛,有许多人需要获取手机所有照片,网上也有很多的源码,但大多经过测试都不能运行,这里我自己整理修改了一份获取手机所有照片信息的代码。闲话不说,呈现给大家!
第一个是需要用到的3个布局文档
- image_show---------------:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/ivImageShow"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:id="@+id/btnClose"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="确定"
- />
- </LinearLayout>
- simple_list_item:--------
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/item_title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
上面一个分别是以:main.xlm, image_show.xml, simple_list_item.xml 为名字的3个xml档。
下面为代码部分:
首先定义一个工具类
- package com.getAllpic;
- import java.io.ByteArrayOutputStream;
- import java.io.InputStream;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- public class FileUtil {
- public FileUtil() {
- }
- //通过传进来的流生成一个Byte数组
- public byte[] readInputStream(InputStream inStream) throws Exception {
- byte[] buffer = new byte[1024];
- int len = -1;
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- while ((len = inStream.read(buffer)) != -1) {
- outStream.write(buffer, 0, len);
- }
- byte[] data = outStream.toByteArray();
- outStream.close();
- inStream.close();
- return data;
- }
- //生成图片的方法
- public Bitmap getBitmapFromBytes(byte[] bytes, BitmapFactory.Options opts) {
- if (bytes != null){
- if (opts != null){
- return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,opts);
- }
- else{
- return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
- }
- }
- return null;
- }
- }
接下来是活动界面:
- package com.getAllpic;
- import java.io.FileNotFoundException;
- import android.app.Dialog;
- import android.content.ContentResolver;
- import android.database.Cursor;
- import android.graphics.Bitmap;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.MediaStore;
- import android.support.v4.app.FragmentActivity;
- import android.support.v4.app.LoaderManager.LoaderCallbacks;
- import android.support.v4.content.CursorLoader;
- import android.support.v4.content.Loader;
- import android.support.v4.widget.SimpleCursorAdapter;
- import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.BaseAdapter;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.TextView;
- public class MyDevicePhotoActivity extends FragmentActivity implements LoaderCallbacks<Cursor>{
- private Bitmap bitmap = null;
- private byte[] mContent = null;
- int num=0;
- private ListView listView = null;
- private SimpleCursorAdapter simpleCursorAdapter = null;
- private static final String[] STORE_IMAGES = {
- MediaStore.Images.Media.DISPLAY_NAME,
- MediaStore.Images.Media.LATITUDE,
- MediaStore.Images.Media.LONGITUDE,
- MediaStore.Images.Media._ID
- };
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- listView = (ListView)findViewById(R.id.list);
- simpleCursorAdapter = new SimpleCursorAdapter(
- this,
- R.layout.simple_list_item,
- null,
- STORE_IMAGES,
- new int[] { R.id.item_title},
- 0
- );
- simpleCursorAdapter.setViewBinder(new ImageLocationBinder());
- Log.i("info", (simpleCursorAdapter==null)+"");
- listView.setAdapter(simpleCursorAdapter);
- // 注意此处是getSupportLoaderManager(),而不是getLoaderManager()方法。
- getSupportLoaderManager().initLoader(0, null, this);
- // 单击显示图片
- listView.setOnItemClickListener(new ShowItemImageOnClickListener());
- }
- @Override
- public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
- // TODO Auto-generated method stub
- // 为了查看信息,需要用到CursorLoader。
- CursorLoader cursorLoader = new CursorLoader(
- this,
- MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
- STORE_IMAGES,
- null,
- null,
- null);
- return cursorLoader;
- }
- @Override
- public void onLoaderReset(Loader<Cursor> arg0) {
- simpleCursorAdapter.swapCursor(null);
- }
- @Override
- public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
- // 使用swapCursor()方法,以使旧的游标不被关闭.
- simpleCursorAdapter.swapCursor(cursor);
- }
- // 将图片的位置绑定到视图
- private class ImageLocationBinder implements ViewBinder{
- @Override
- public boolean setViewValue(View view, Cursor cursor, int arg2) {
- Log.i("info", "setViewValue");
- //此处arg2代表的是STORE_IMAGES数组的索引
- if(arg2==0){
- long id=cursor.getLong(3);
- Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.buildUpon().appendPath(Long.toString(id)).build();
- FileUtil file = new FileUtil();
- ContentResolver resolver = getContentResolver();
- try {
- mContent = file.readInputStream(resolver.openInputStream(Uri.parse(uri.toString())));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- if(mContent!=null)
- bitmap = file.getBitmapFromBytes(mContent, null);
- if(bitmap!=null)
- bitmap=Bitmap.createScaledBitmap(bitmap,getResources().getDisplayMetrics().widthPixels,
- getResources().getDisplayMetrics().heightPixels/2, true);
- ((ImageView)view).setImageBitmap(bitmap);
- return true;
- }
- return false;
- }
- }
- // 单击项显示图片事件监听器
- private class ShowItemImageOnClickListener implements OnItemClickListener{
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position,
- long id) {
- num=parent.getChildCount();
- // TODO Auto-generated method stub
- final Dialog dialog = new Dialog(MyDevicePhotoActivity.this);
- // 以对话框形式显示图片
- dialog.setContentView(R.layout.image_show);
- dialog.setTitle("图片显示");
- ImageView ivImageShow = (ImageView) dialog.findViewById(R.id.ivImageShow);
- Button btnClose = (Button) dialog.findViewById(R.id.btnClose);
- btnClose.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- dialog.dismiss();
- }
- });
- Log.i("info", id+"+++++++id");
- Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.buildUpon().
- appendPath(Long.toString(id)).build();
- FileUtil file = new FileUtil();
- ContentResolver resolver = getContentResolver();
- // 从Uri中读取图片资源
- try {
- mContent = file.readInputStream(resolver.openInputStream(Uri.parse(uri.toString())));
- bitmap = file.getBitmapFromBytes(mContent, null);
- //******************************************设置对话框大小****************
- Window diaWindow=dialog.getWindow();
- WindowManager.LayoutParams lp=diaWindow.getAttributes();
- lp.width=MyDevicePhotoActivity.this.getResources().getDisplayMetrics().widthPixels;
- lp.height=MyDevicePhotoActivity.this.getResources().getDisplayMetrics().heightPixels/2;
- diaWindow.setAttributes(lp);
- bitmap=Bitmap.createScaledBitmap(bitmap,lp.width/2,
- lp.height/2, true);
- ivImageShow.setImageBitmap(bitmap);
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- dialog.show();
- }
- }
- }
好了,以上就是获取手机所有照片的方法。如果还有什么问题,大家可以问我!