我是靠谱客的博主 魁梧蜗牛,最近开发中收集的这篇文章主要介绍[Android 笔记四 ] 使用MediaProvider找到系统中的图片文件,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1.MainActivity
package com.example.mymediaprovider;
import android.Manifest;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS;
public class MainActivity extends AppCompatActivity {
private ContentResolver mProvider;
//Don't ask again
private static final int NOT_ASK_AGAIN = 2;
private AlertDialog mDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myRequestPermission();
}
//get user permission
private void myRequestPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
1);
else {
run();
Toast.makeText(this, R.string.toast_alreadyPermiss, Toast.LENGTH_SHORT).show();
}
}
//After having permission, then to get internal data
private void run() {
ImageView imageView = findViewById(R.id.iv_imageview);
String filePath;
//get data
mProvider = getContentResolver();
Cursor cursor = mProvider.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Images.Media.DATA},
null,
null,
null,
null);
int columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
imageView.setImageBitmap(bitmap);
}
cursor.close();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == NOT_ASK_AGAIN) {
myRequestPermission();
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions,
int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
for (int i = 0; i < permissions.length; i++) {
//User selected - "always allow"
if (grantResults[i] == PERMISSION_GRANTED) {
run();
Toast.makeText(this,
"" + getString(R.string.premiss) + permissions[i] + getString(R.string.request_success),
Toast.LENGTH_SHORT).show();
} else {
//User selected - do not ask again
if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
permissions[i])) {
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.builder_title)
.setMessage(R.string.builder_msg)
.setPositiveButton(R.string.builder_btn,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (mDialog != null && mDialog.isShowing()) {
mDialog.dismiss();
}
Intent intent;
intent = new Intent(
ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri;
uri = Uri.fromParts("package",
getPackageName(),
null);
intent.setData(uri);
startActivityForResult(intent, NOT_ASK_AGAIN);
}
});
mDialog = builder.create();
mDialog.setCanceledOnTouchOutside(false);
mDialog.show();
} else {
//User selected - Disable
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setTitle(R.string.builder_title)
.setMessage(R.string.builder_msg)
.setPositiveButton(R.string.builder_btn, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (mDialog != null && mDialog.isShowing()) {
mDialog.dismiss();
}
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE},
1);
}
});
mDialog = builder.create();
mDialog.setCanceledOnTouchOutside(false);
mDialog.show();
}
}
}
}
}
}

2.Layout

<?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=".MainActivity">
<ImageView
android:id="@+id/iv_imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

3.String.xml

<resources>
<string name="app_name">MyMediaProvider</string>
<string name="toast_alreadyPermiss">您已经申请了权限!</string>
<string name="premiss">权限</string>
<string name="request_success">申请成功</string>
<string name="builder_msg">点击允许才可以使用我们的app哦</string>
<string name="builder_title">permission</string>
<string name="builder_btn">去允许</string>5.实现效果:保证模拟器里有照片
</resources>

4.配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mymediaprovider">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

5.实现效果:保证模拟器里有照片

 

最后

以上就是魁梧蜗牛为你收集整理的[Android 笔记四 ] 使用MediaProvider找到系统中的图片文件的全部内容,希望文章能够帮你解决[Android 笔记四 ] 使用MediaProvider找到系统中的图片文件所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部