> 文章列表 > android ContentObserver实时监测媒体图片增删改,java(1)

android ContentObserver实时监测媒体图片增删改,java(1)

android ContentObserver实时监测媒体图片增删改,java(1)

android ContentObserver实时监测媒体图片增删改,java(1)

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
    implementation 'com.github.bumptech.glide:glide:4.15.0'annotationProcessor 'com.github.bumptech.glide:compiler:4.15.0'implementation "androidx.documentfile:documentfile:1.0.1"
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.documentfile.provider.DocumentFile;import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;import com.bumptech.glide.Glide;import java.util.Collection;public class MainActivity extends AppCompatActivity {private String TAG = "fly";private ImageView image;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);image = findViewById(R.id.image);findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {dataChanged();}});readAllPhoto();Log.d(TAG, "-----" + Thread.currentThread().getId());Handler handler = new Handler(Looper.getMainLooper()) {@Overridepublic void handleMessage(@NonNull Message msg) {super.handleMessage(msg);Log.d(TAG, msg.toString());}};MyContentObserver contentObserver = new MyContentObserver(handler);getContentResolver().registerContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true, contentObserver);}private class MyContentObserver extends ContentObserver {public MyContentObserver(Handler handler) {super(handler);}@Overridepublic void onChange(boolean selfChange, @Nullable Uri uri) {super.onChange(selfChange, uri);Log.d(TAG, "tid: " + Thread.currentThread().getId());Log.d(TAG, "onChange " + uri.toString());Glide.with(getApplicationContext()).load(uri).into(image);Log.d(TAG, DocumentFile.fromSingleUri(getApplicationContext(), uri).getName());Log.d(TAG, getPathFromUri(getApplicationContext(), uri));}@Overridepublic void onChange(boolean selfChange, @NonNull Collection<Uri> uris, int flags) {super.onChange(selfChange, uris, flags);Log.d(TAG, "onChange Size:" + uris.size());}}private String getPathFromUri(Context context, Uri contentUri) {Cursor cursor = null;try {String[] proj = {MediaStore.Images.Media.DATA};cursor = context.getContentResolver().query(contentUri, proj, null, null, null);cursor.moveToFirst();int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);return cursor.getString(column_index);} finally {if (cursor != null) {cursor.close();}}}private void readAllPhoto() {Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);while (cursor.moveToNext()) {//图片名称String name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME));// 图片绝对路径int data_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);String path = cursor.getString(data_index);Log.d(TAG, "name= " + name + "  path= " + path);}cursor.close();}private void dataChanged() {Log.d(TAG, "dataChanged");this.getContentResolver().notifyChange(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null);}
}

Android 13根据Uri反向解析图片真实文件路径,Java_zhangphil的博客-CSDN博客Android 13根据Uri反向解析图片真实文件路径,Java。https://blog.csdn.net/zhangphil/article/details/129710413

android 13读取本机所有图片名和存储绝对路径,Java_zhangphil的博客-CSDN博客【Android设置头像,手机拍照或从本地相册选取图片作为头像】像微信、QQ、微博等社交类的APP,通常都有设置头像的功能,设置头像通常有两种方式:1,让用户通过选择本地相册之类的图片库中已有的图像,裁剪后作为头像。Android设置头像,手机拍照或从本地相册选取图片作为头像_android 头像拍照_zhangphil的博客-CSDN博客。Android图片添加文字水印并保存水印文字图片到指定文件_zhangphil的博客-CSDN博客。https://blog.csdn.net/zhangphil/article/details/129639271

Android 13手机图片存储File路径转Uri,Java_zhangphil的博客-CSDN博客android根据图片资源的drawable id转化为Uri,java_zhangphil的博客-CSDN博客。android根据图片资源的drawable id转化为Uri的工具方法。Android 13手机图片存储File路径转Uri,Java。https://blog.csdn.net/zhangphil/article/details/129640812

android根据图片资源的drawable id转化为Uri,java_android drawable转uri_zhangphil的博客-CSDN博客android根据图片资源的drawable id转化为Uri的工具方法。https://blog.csdn.net/zhangphil/article/details/129431755