相关描述

已经完成发送彩信功能之后,就来继续向前走一步,来到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

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

Android笔记--通过MediaStore查询图片的更多相关文章

  1. Android学习笔记进阶之在图片上涂鸦(能清屏)

    Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...

  2. Android笔记之使用Glide加载网络图片、下载图片

    Glide简介 不想说太多,真的很方便:P)可以节省我不少时间 GitHub地址:https://github.com/bumptech/glide 加载网络图片到ImageView Glide.wi ...

  3. Android笔记(二十八) Android中图片之简单图片使用

    用户界面很大程度上决定了APP是否被用户接收,为了提供友好的界面,就需要在应用中使用图片了,Android提供了丰富的图片处理功能. 简单使用图片 使用Drawable对象 为Android应用增加了 ...

  4. Android笔记——Android中数据的存储方式(二)

    我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...

  5. Android相机、相册获取图片显示并保存到SD卡

    Android相机.相册获取图片显示并保存到SD卡 [复制链接]   电梯直达 楼主    发表于 2013-3-13 19:51:43 | 只看该作者 |只看大图  本帖最后由 happy小妖同学 ...

  6. (转)Android学习-使用Async-Http实现图片压缩并上传功能

    (转)Android学习-使用Async-Http实现图片压缩并上传功能 文章转载自:作者:RyaneLee链接:http://www.jianshu.com/p/940fc7ba39e1 让我头疼一 ...

  7. Android 笔记之 Android 系统架构

    Android笔记之Android系统架构 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: ...

  8. Android 使用GridView+仿微信图片上传功能(附源代码)

    由于工作要求最近在使用GridView完成图片的批量上传功能,我的例子当中包含仿微信图片上传.拍照.本地选择.相片裁剪等功能,如果有需要的朋友可以看一下,希望我的实际经验能对您有所帮助. 直接上图,下 ...

  9. Android笔记--自定义控件仿遥控器的圆形上下左右OK圆盘按钮

    原文:Android笔记--自定义控件仿遥控器的圆形上下左右OK圆盘按钮 上面就是几张预览图!代码在最底下 主要就两个步骤,画图.监听点击 1.整个控件基本上是一步步画出来的,重写onDraw方法开始 ...

  10. Qt for Android (三) 打开Android相册并选一个图片进行显示

    Qt for Android (三) 这两天弄了一下android相册的相关功能.还是花了挺长时间的,这里总结一下,避免以后再踩坑.同时也在这篇文章里面补齐一些android开发的基础支持 打开And ...

随机推荐

  1. B. Going to the Cinema

    https://codeforces.com/contest/1782/problem/B 题目大意就是给定n个人,每个人有一个除自己之外的最少陪同人数,选一部分人去电影院,要求去的人人数大于等于去的 ...

  2. linux安装jdk脚本

    使用Shell远程给Linux安装JDK   #1.下载JDK(不同版本JDK更换链接即可) wget命令下载的文件会在当前目录下,所以如果在下载前最好先 cd 到想把JDK安装的位置,如果没有,可以 ...

  3. SQL server 清空数据库所有表

    use [数据库名]GOdeclare @sql varchar(8000)while (select count(*) from sysobjects where type='U')>0beg ...

  4. 求两个自然数之间的最大公约数C++实现

    1 #include "pch.h" 2 #include <iostream> 3 using namespace std; 4 5 int main() 6 { 7 ...

  5. CVPixelBufferRef

    在iOS里,我们经常能看到 CVPixelBufferRef 这个类型,在Camera 采集返回的数据里得到一个CMSampleBufferRef,而每个CMSampleBufferRef里则包含一个 ...

  6. Mixly智能门禁(物联网)

    智能门禁arduino rc522读卡器  sg90舵机 校园卡我的校园卡号识别为 30fcb4a8d #include <RFID.h>#include <SPI.h>#in ...

  7. C语言II—作业03

    1.作业头 这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/SE2020-3 这个作业要求在哪里 https://edu.cnblogs.com/cam ...

  8. P2212 Watering the Fields S

    题目描述 给定n个点,第i个点的坐标为(xi,yi)(xi​,yi​),如果想连通第i个点与第j个点,需要耗费的代价为两点的距离.第i个点与第j个点之间的距离使用欧几里得距离进行计算,即:(xi-xj ...

  9. CentOS 7(Linux)安装Docker

    CentOS 7(Linux)安装Docker 一.分别执行下列命令添加并更新yum源 yum update yum install epel-release -y yum clean all yum ...

  10. Pytorch slp singleLayerPerceptron 单层感知机

    单层感知机 \[\begin{aligned} & y = XW + b \\ & y = \sum x_i*w_i+b\\ \end{aligned} \] Derivative \ ...