ANDROID笔记:通过ContentProvider得到SD卡的图片
activity:
package com.example.android_contentprovider; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.content.Intent; import android.database.Cursor; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.provider.MediaStore.Audio.Media; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { // 图片数据集 List<ImagePro> list = new ArrayList<ImagePro>(); LayoutInflater inflater; @Override protected void onCreate(Bundle savedInstancetate) { super.onCreate(savedInstancetate); setContentView(R.layout.activity_main); inflater = LayoutInflater.from(MainActivity.this); // 重新扫描图片,添加新图片 allScan(); initData(); GridView gridView = (GridView) findViewById(R.id.grid); gridView.setAdapter(adapter); gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { View view = getLayoutInflater().inflate(R.layout.grid_item, null); ImageView imageView = (ImageView) view .findViewById(R.id.imageView1); TextView textView = (TextView) view.findViewById(R.id.text); imageView.setImageBitmap(BitmapFactory.decodeFile(list .get(arg2).getData())); textView.setText(list.get(arg2).getName()); // 弹出对话框 new AlertDialog.Builder(MainActivity.this).setView(view) .setPositiveButton("ok", null).show(); } }); } private void initData() { // 判断SD卡是否存在 if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { // 获得存储卡的路径 String sdpath = Environment.getExternalStorageDirectory() + "/"; Toast.makeText(MainActivity.this, sdpath, Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this, "null=========", Toast.LENGTH_LONG).show(); } // 使用MediaStore.Images.Media.EXTERNAL_CONTENT_URI,疯狂android讲义463页的Media.EXTERNAL_CONTENT_URI查找不到图片 Cursor cursor = getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null); while (cursor.moveToNext()) { ImagePro imagePro = new ImagePro(); // 得到图片名称 imagePro.setName(cursor.getString(cursor .getColumnIndex(Media.DISPLAY_NAME))); // 得到图片尺寸 imagePro.setDesc(cursor.getString(cursor.getColumnIndex(Media.SIZE))); // 得到图片路径 byte[] path = cursor.getBlob(cursor.getColumnIndex(Media.DATA)); imagePro.setData(new String(path, 0, path.length - 1)); list.add(imagePro); } } /** * 扫描SD卡,添加新图片 */ public void allScan() { sendBroadcast(new Intent( Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); } private BaseAdapter adapter = new BaseAdapter() { @Override public View getView(int arg0, View arg1, ViewGroup arg2) { // 不能直接显示全部图片,会内存溢出(OOM) View view = inflater.inflate(R.layout.grid_item, null); TextView textView = (TextView) view.findViewById(R.id.text); textView.setText(list.get(arg0).getName()); return view; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return list.get(arg0); } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } }; }
pojo类
package com.example.android_contentprovider; public class ImagePro { private String name; private String desc; private String data; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public String getData() { return data; } public void setData(String data) { this.data = data; } public ImagePro() { } public ImagePro(String name, String desc, String data) { super(); this.name = name; this.desc = desc; this.data = data; } }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <GridView android:id="@+id/grid" android:numColumns="2" android:layout_width="fill_parent" android:layout_height="fill_parent" > </GridView> </RelativeLayout>
grid_item.xml
<?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/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0000"/> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android_contentprovider" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.RESTART_PACKAGES" /> <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.android_contentprovider.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
PS:权限需要写在activity前
android系统为其他的应用程序访问多媒体资源提供了ContentProvider
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI:存储在SD卡上的音频文件ContentProvider的URI
MediaStore.Audio.Media.INTERNAL_CONTENT_URI:存储在手机内部存储器卡上的音频文件ContentProvider的URI
MediaStore.Images.Media.EXTERNAL_CONTENT_URI:存储在SD卡上的图像文件文件ContentProvider的URI
MediaStore.Images.Media.INTERNAL_CONTENT_URI:存储在手机内部存储器上图像文件ContentProvider的URI
MediaStore.Video.Media.INTERNAL_CONTENT_URI:存储在手机内部存储器上的视频文件ContentProvider的URI
MediaStore.Video.Media.EXTERNAL_CONTENT_URI:存储在SD卡上的视频文件ContentProvider的URI