使用内容提供者和Glide加载相册图片,加载不出任何图片怎么回事
这是内容提供者应用主要的代码
2024-03-31 14:03
·
Android / 内容提供者 / ContentResolver
红色圈出来的是我自己的打印log,后面就没有其他属于我的打印log(已经截取了完整的logcat窗口,下面没有其他内容了
1.在写TestProvider时报了个Failed to find provider info for com.example.contentprvider3的异常
Logcat报错部分
2021-11-29 07:31:15.268 5562-5562/com.example.testprovider D/MainActivity: onClick
2021-11-29 07:31:15.271 5562-5562/com.example.testprovider D/MainActivity: cursor------null
2021-11-29 07:31:15.271 5562-5562/com.example.testprovider W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String[] android.database.Cursor.getColumnNames()' on a null object reference
2021-11-29 07:31:15.271 5562-5562/com.example.testprovider W/System.err: at com.example.testprovider.MainActivity.RemoteUser(MainActivity.java:34)
2021-11-29 07:31:15.271 5562-5562/com.example.testprovider W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2021-11-29 07:31:15.271 5562-5562/com.example.testprovider W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
2021-11-29 07:31:15.271 5562-5562/com.example.testprovider W/System.err: at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
2021-11-29 07:31:15.271 5562-5562/com.example.testprovider W/System.err: at android.view.View.performClick(View.java:4780)
2021-11-29 07:31:15.271 5562-5562/com.example.testprovider W/System.err: at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
2021-11-29 07:31:15.271 5562-5562/com.example.testprovider W/System.err: at android.view.View$PerformClick.run(View.java:19866)
2021-11-29 07:31:15.271 5562-5562/com.example.testprovider W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
2021-11-29 07:31:15.271 5562-5562/com.example.testprovider W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
2021-11-29 07:31:15.271 5562-5562/com.example.testprovider W/System.err: at android.os.Looper.loop(Looper.java:135)
2021-11-29 07:31:15.271 5562-5562/com.example.testprovider W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254)
2021-11-29 07:31:15.271 5562-5562/com.example.testprovider W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2021-11-29 07:31:15.271 5562-5562/com.example.testprovider W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
2021-11-29 07:31:15.271 5562-5562/com.example.testprovider W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
2021-11-29 07:31:15.271 5562-5562/com.example.testprovider W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
2.项目ContentProvider部分代码
package com.example.contentprvider3.Provider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.contentprvider3.utils.Constants;
import com.example.contentprvider3.Databasehelper.UserDatabasehelper;
public class UserProvider extends ContentProvider {
private static final String SCHEMA="content://";
private static final String AUTHORITY="com.example.contentprvider3.Provider.UserProvider";
private static Uri uri_provider =Uri.parse(SCHEMA+AUTHORITY);
private UserDatabasehelper muserDatabaseHelper = null;
private static final int User_Match_Code = 1;
private static UriMatcher suriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
suriMatcher.addURI(AUTHORITY,null,User_Match_Code);
}
@Override
public boolean onCreate() {
muserDatabaseHelper = new UserDatabasehelper(getContext());
return false;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
//分配规则
int result = suriMatcher.match(uri);
if (result==User_Match_Code){
SQLiteDatabase db = muserDatabaseHelper.getReadableDatabase();
Cursor cursor = db.query(Constants.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
}else{
//不匹配规则
throw new IllegalArgumentException("参数错误");
}
return null;
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
return null;
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
return 0;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
return 0;
}
}
3.ContentProvider的Manifest文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.contentprvider3">
<permission android:name="contentprvider3._READ_PERMISSION"
android:protectionLevel="normal"/>
<permission android:name="contentprvider3._WRITE_PERMISSION"
android:protectionLevel="normal"/>
<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/Theme.ContentPrvider3">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:exported="true"
android:authorities="com.example.contentprvider3.Provider.UserProvider"
android:grantUriPermissions="true"
android:name=".Provider.UserProvider"/>
</application>
</manifest>
4.TestProvider部分代码
package com.example.testprovider;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private static final String SCHEMA="content://";
private static final String AUTHORITY="com.example.contentprvider3.Provider.UserProvider";
private static Uri uri_provider =Uri.parse(SCHEMA+AUTHORITY);
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void RemoteUser(View view) {
Log.d(TAG, "onClick");
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(uri_provider, null, null, null, null);
try {
Log.d(TAG, "cursor------"+cursor);
String[] columnNames2 = cursor.getColumnNames();
for (String columnName : columnNames2) {
Log.d(TAG, "columnName---" + columnName);
}
while (cursor.moveToNext()) {
Log.d(TAG, "------------");
for (String columnName : columnNames2) {
int columnname = cursor.getColumnIndex(columnName);
String value = cursor.getString(columnname);
Log.d(TAG, columnName + "------" + value);
Log.d(TAG, "----------------");
}
}
cursor.close();
}catch (Exception e){e.printStackTrace();
}
}
}
5.TestProvider的Manifest文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testprovider">
<uses-permission android:name="ContentPrivider3._READ_PERMISSION"/>
<uses-permission android:name="ContentPrivider3._WRITE_PERMISSION"/>
<queries>
<package android:name="com.example.contentprvider3"/>
<provider android:authorities="com.example.contentprvider3.Provider.UserProvider"/>
</queries>
<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/Theme.TestProvider">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

之前学过内容提供者知道
在打印输出图片媒体库表结构过程中,打印部分属性后报错Unable to convert BLOB to string

完整的报错信息如下:

该部分的代码为:
package com.example.imagepicker;
import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.CursorLoader;
import androidx.loader.content.Loader;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.SQLException;
public class PickerActivity extends AppCompatActivity {
private static final String TAG = "PickerActivity";
private static final int LOADER_ID = 1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picker);
ContentResolver contentResolver = getContentResolver();
// Uri imageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
// Log.d(TAG, "imageUri ------> " + imageUri);
Uri imageUri = MediaStore.Files.getContentUri("external");
Cursor cursor = contentResolver.query(imageUri, null, null, null, null);
String[] columnNames = cursor.getColumnNames();
while (cursor.moveToNext()) {
Log.d(TAG, "==============");
for (String columnName : columnNames) {
@SuppressLint("Range") String value = cursor.getString(cursor.getColumnIndex(columnName));
Log.d(TAG, columnName + "------> " + value);
}
Log.d(TAG, "==============");
}
cursor.close();
// initLoadManger();
}
private void initLoadManger() {
LoaderManager loaderManager = LoaderManager.getInstance(this);
loaderManager.initLoader(LOADER_ID, null, new LoaderManager.LoaderCallbacks<Cursor>() {
@NonNull
@Override
public Loader<Cursor> onCreateLoader(int id, @Nullable Bundle args) {
if (id == LOADER_ID) {
return new CursorLoader(PickerActivity.this,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{"_data", "_display_name", "date_modified"},
null, null, "data_added DESC");
}
return null;
}
@SuppressLint("Range")
@Override
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor data) {
if (data != null) {
String[] columnNames = data.getColumnNames();
while(data.moveToNext()) {
Log.d(TAG, "==============");
for (String columnName : columnNames) {
// @SuppressLint("Range") String value = data.getString(data.getColumnIndex(columnName));
Log.d(TAG, columnName + "------> " + data.getString(data.getColumnIndex(columnName)));
}
Log.d(TAG, "==============");
}
data.close();
}
}
@Override
public void onLoaderReset(@NonNull Loader<Cursor> loader) {
}
});
}
}
尝试过网上的一些解决方案,但都没能成功解决这个问题,希望各位大佬指点一二,不甚感激
- 1
- 2
- 3
- 4
- 5
- 6
- 40
Copyright © 阳光沙滩V1.0.3(2014-) 本网站由程序猿(媛)用爱驱动
