Android-获取手机上所有图片

核心代码:


 Cursor cursor = getContentResolver()
         .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
                while (cursor.moveToNext()) {
                    //获取图片的名称
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
                    //获取图片的生成日期
byte[] data = cursor.getBlob(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                    //获取图片的详细信息
String desc = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DESCRIPTION));
                    names.add(name);
                    descs.add(desc);
                    fileNames.add(new String(data, 0, data.length - 1));
                }

demo如下:

1、权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

java类:

package com.example.administrator.downloadimgdemo;

import android.app.AlertDialog;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/5.
 */

public class GetAllImg extends AppCompatActivity {

    //查看图片按钮
    private Button look;
    private Button add;
    //显示图片名称的list
    ListView show_list;
    ArrayList names = null;
    ArrayList descs = null;
    ArrayList fileNames = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        look = (Button) findViewById(R.id.look);
        add = (Button) findViewById(R.id.add);
        show_list = (ListView) findViewById(R.id.show_list);

        names = new ArrayList();
        descs = new ArrayList();
        fileNames = new ArrayList();

        look.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Cursor cursor = getContentResolver().query(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
                while (cursor.moveToNext()) {
                    //获取图片的名称
                    String name = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
                    //获取图片的生成日期
                    byte[] data = cursor.getBlob(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                    //获取图片的详细信息
                    String desc = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DESCRIPTION));
                    names.add(name);
                    descs.add(desc);
                    fileNames.add(new String(data, 0, data.length - 1));
                }
                List<Map<String, Object>> listItems = new ArrayList<>();
                for (int i = 0; i < names.size(); i++) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("name", names.get(i));
                    map.put("desc", descs.get(i));
                    listItems.add(map);
                }
                //设置adapter
                SimpleAdapter adapter = new SimpleAdapter(GetAllImg.this, listItems,
                        R.layout.line, new String[]{"name", "desc"}, new int[]{R.id.name, R.id.desc});

                show_list.setAdapter(adapter);
            }
        });

        ///list的点击事件
        show_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                View viewDiag = getLayoutInflater().inflate(R.layout.view, null);

                ImageView image = (ImageView) viewDiag.findViewById(R.id.image);
                image.setImageBitmap(BitmapFactory.decodeFile((String) fileNames.get(i)));

                new AlertDialog.Builder(GetAllImg.this).setView(viewDiag)
                        .setPositiveButton("确定", null).show();
            }
        });
    }
}

布局:

line.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">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="name" />

    <TextView
        android:id="@+id/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="desc" />

</LinearLayout>

view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>

</LinearLayout>

参考:

Android实现获取手机里面的所有图片

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_31057219/article/details/71213996
个人分类: 图片
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭