Android 获取手机中的图片信息的两种方法

1,  

Android 使用ContentProvider扫描手机中的图片


// 必须在查找前进行全盘的扫描,否则新加入的图片是无法得到显示的(加入对sd卡操作的权限)//todo 仅限于android4.2.2(API 17)以下(不包括17)可以正常使用。。。。
public static void allScanBeforeSearchRes(Context context) {


    if (Build.VERSION.SDK_INT >= 17) { // 判断SDK版本是不是4.2.2或者高于4.2.2.//todo useless...
        String[] paths = new String[]{Environment.getExternalStorageDirectory().toString()};
        MediaScannerConnection.scanFile(context, paths, null, null);
    } else {
        Intent intent;
        intent = new Intent(Intent.ACTION_MEDIA_MOUNTED);
        intent.setClassName("com.android.providers.media", "com.android.providers.media.MediaScannerReceiver");
        intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
        context.sendBroadcast(intent);
    }

}
 
private void getAllPicturesInTheDevice() {

    CommonUtil.allScanBeforeSearchRes(this);//对android 4.4 以下有用。。。

    String[] projection = {
            MediaStore.Images.Media.DATA,
            MediaStore.Images.Media.DATE_TAKEN,
    };

    //全部图片
    String where = MediaStore.Images.Media.MIME_TYPE + "=? or "
            + MediaStore.Images.Media.MIME_TYPE + "=?";
    String[] whereArgs = {"image/jpeg", "image/png"};

    Cursor cursor = getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
            where, whereArgs, MediaStore.Images.Media.DATE_MODIFIED + " desc");
   
    while (cursor.moveToNext()) {
        String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
        Long takeDate = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN));

	......
    }
    cursor.close();

}

2, 通过遍历找到所有的图片格式的文件。强烈推荐使用第二种方法,准确。
private static List<File> fileList = new ArrayList<File>();
private static String[] img = new String[]{".jpg", ".png", ".gif", ".bmp"};

/**
 * 遍历sdcard 找到某找类型的file放到list中。
 * 比较耗时 建议放在线程中做
 * @param file
 */
private static void checkFile(File file) {// 遍历文件,在这里是遍历sdcard

    if (file.isDirectory()) {// 判断是否是文件夹

        File[] files = file.listFiles();// 以该文件夹的子文件或文件夹生成一个数组

        if (files != null) {// 如果文件夹不为空

            for (int i = 0; i < files.length; i++) {

                File f = files[i];

                checkFile(f);// 递归调用

            }

        }

    } else if (file.isFile()) {// 判断是否是文件

        int dot = file.getName().lastIndexOf(".");

        if (dot > -1 && dot < file.getName().length()) {

            String extriName = file.getName().substring(dot,

                    file.getName().length());// 得到文件的扩展名

            if (extriName.equals(img[0]) || extriName.equals(img[1])

                    || extriName.equals(img[2]) || extriName.equals(img[3])) {// 判断是否是图片文件 www.it165.net

                fileList.add(file);

            }

        }

    }

}

 
相关链接:
http://blog.csdn.net/xiaanming/article/details/18730223
 
http://www.it165.net/pro/html/201304/5483.html
 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值