首页 > 其他分享 >Android笔记--通过MediaStore查询图片

Android笔记--通过MediaStore查询图片

时间:2023-02-25 10:22:17浏览次数:33  
标签:String -- cursor Images import Android MediaStore android

相关描述

已经完成发送彩信功能之后,就来继续向前走一步,来到MediaStore查询图片界面啦!

具体步骤实现

1、简简单单地一个界面

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击下方图片发送:">

    </TextView>

    <GridLayout
        android:id="@+id/gl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </GridLayout>

</LinearLayout>

2、后台代码

package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageDecoder;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.GridLayout;
import android.widget.ImageView;

import java.util.ArrayList;

public class check extends AppCompatActivity {

    private static final String[] PERMISSIONS=new String[]{
            Manifest.permission.READ_EXTERNAL_STORAGE
    };

    private static final int REQUEST_ALL_CODES=1;


    private ArrayList<ImageInfo> list=new ArrayList<>();

    private GridLayout gl;

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

        //手动扫描图片入库
        MediaScannerConnection.scanFile(this,new String[]{
                Environment.getExternalStorageState().toString()
        },null,null);

//        gl=findViewById(R.id.gl);
//
//
//        //加载图片列表
//        loadImageList();
//
//        //显示图像网络
//        showImageGrid();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if(requestCode==REQUEST_ALL_CODES){
            //加载图片列表
            loadImageList();

            //显示图像网络
            showImageGrid();
        }
    }

    //显示图像网络
    private void showImageGrid() {
        //清空
        gl.removeAllViews();

        for(ImageInfo info:list){
            ImageView view=new ImageView(this);

            Bitmap bitMap=BitmapFactory.decodeFile(info.path);
            view.setImageBitmap(bitMap);

            //设置图片的缩放类型
            view.setScaleType(ImageView.ScaleType.FIT_CENTER);

            //设置点击事件
            view.setOnClickListener(v->{

            });
            //将图片添加到网络布局
            gl.addView(view);

        }

    }

    //加载图片列表
    @SuppressLint("Range")
    private void loadImageList() {
        //MediaStore
        String[] columns=new String[]{
                MediaStore.Images.Media._ID,//图片编号
                MediaStore.Images.Media.TITLE,//图片标题
                MediaStore.Images.Media.SIZE,//图片大小
                MediaStore.Images.Media.DATA//图片路径
        };

        Cursor cursor=getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                columns,
                "_size < 307200",
                null,
                "_size DESC"
        );

        int count=0;
        if(cursor!=null){
            while (cursor.moveToNext() && count < 6) {
                ImageInfo info=new ImageInfo();
                info.id=cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media._ID));
                info.name=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.TITLE));
                info.size=cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.SIZE));
                info.path=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));

                count++;
                list.add(info);
                Log.d("ning","images:"+info.toString());

            }
        }

    }
}

3、实体类代码

package com.example.myapplication;

public class ImageInfo {
    @Override
    public String toString() {
        return "ImageInfo{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", size='" + size + '\'' +
                ", path='" + path + '\'' +
                '}';
    }

    public long id;
    public String name;
    public long size;
    public String path;
}

4、特殊情况说明

扫描完成再去请求权限:

这里就直接用null代替了那个callback

我们需要保证在出现图片的时候,图片能够进入数据库,但是他需要我们手动调节,不,手动扫描入库:

标签:String,--,cursor,Images,import,Android,MediaStore,android
From: https://www.cnblogs.com/liuzijin/p/17153741.html

相关文章

  • MyBatis 优秀的轻量级持久层框架从入门到应用实战
    课程简介和目标学习过JDBC的同学都会有所感受,原生JDBC技术实现对持久层数据操作有着诸多问题.1编码繁琐,步骤多2手动处理结果集和实体类映射关系比较麻烦,尤其是多表......
  • 学习中设计模式的总结
    首先要了解什么是设计模式呢?设计模式:可以简单的理解为两个字“经验”,就是码农们在各种需求中间,总结出来的一种最优的解法称之为设计模式;1,单例模式......
  • Oracle计算对公天数
    createorreplacefunctioncalTwoDays(startDateindate,endDateindate)returnnumberisdtStaDatdate;dtEndDatdate;start_date_str......
  • #68. 「NOIP2004」津津的储蓄计划 题解
    #68.「NOIP2004」津津的储蓄计划题解题目传送门题目知识点模拟题目分析非常的“明显”,这是一道模拟题。题意说明有可能在某个月的月初,津津手中的钱加上这个月妈妈......
  • #119. 最大整数 题解
    #119.最大整数题解题目传送门题目知识点字符串+贪心题意说明设有n个正整数(n<=20),将它们连接成一排,组成一个最大的多位整数。(题目简介明了,一看就是出题人懒得写题目背......
  • #160. 「NOIP2004 普及组」不高兴的津津 题解
    #160.「NOIP2004普及组」不高兴的津津题解题目传送门题目知识点枚举题意说明津津如果一天上课超过八个小时就会不高兴,而且上得越久就会越不高兴。假设津津不会因为......
  • 小程序项目结构
                    ......
  • 《黑马旅游网》综合案例六 BaseServlet抽取
    BaseServlet抽取减少Servlet的数量,现在是一个功能一个Servlet,将其优化成一个模块一个Servlet,相当于在数据库一张表对应一个Servlet,在Servlet中提供不同的方法,完成用户的......
  • nvm 一个nodejs版本管理工具!
    转发:https://nvm.uihtm.com/各位不好意思!!这上面很详细了,我就不写了,简单记录下 nvm是什么nvm全英文也叫node.jsversionmanagement,是一个nodejs的版本管理工具。nvm......
  • Android Studio 设置文本内容
    首先在Androidstudio中设置文本内容有两种方式1、在XML文件中通过属性android:text设置文本2、在java代码中调用文本视图的对象的setText方法设置文本具体如下1、......