如何解决尝试在 imageview 中显示位图适用于应用程序但不适用于小部件
我已经从带有 glide 库的链接下载了一个图像,并尝试在小部件上显示它,所以首先在 mainActivity 中尝试它,它在那里工作正常。这是代码:
public class MainActivity extends AppCompatActivity {
String link = "";
String imageUrl = "";
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.testImage);
}
像这样验证写权限:
public Boolean verifyPermissions() {
int permissionExternalMemory = ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionExternalMemory != PackageManager.PERMISSION_GRANTED) {
String[] STORAGE_PERMISSIONS = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
ActivityCompat.requestPermissions(this,STORAGE_PERMISSIONS,1);
return false;
}
return true;
}
以位图形式下载带有滑动的图像:
void downloadImage(String imageLink) {
Context context = getApplicationContext();
if (!verifyPermissions()) {
return;
}
Glide.with(context)
.asBitmap()
.load(imageLink)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource,@Nullable Transition<? super Bitmap> transition) {
Toast.makeText(MainActivity.this,"Saving Image",Toast.LENGTH_SHORT).show();
try {
saveImage(resource,dir,fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
super.onLoadFailed(errorDrawable);
Toast.makeText(MainActivity.this,"Failed to Download image.",Toast.LENGTH_SHORT).show();
}
});
}
将图像保存到图片文件夹,然后在活动中将其显示为空图像视图:
private void saveImage(Bitmap image,File storageDir,String imageFileName) throws IOException {
Context context = getApplicationContext();
SharedPreferences setting = getSharedPreferences("PlaylistWidgetPreferences",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = setting.edit();
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE,"any_picture_name");
values.put(MediaStore.Images.Media.DESCRIPTION,"test Image taken");
values.put(MediaStore.Images.Media.MIME_TYPE,"image/jpeg");
Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
prefEditor.putString("thumbnailPath",getFullPathFromContentUri(context,uri));
prefEditor.apply();
Toast.makeText(getApplicationContext(),uri.toString(),Toast.LENGTH_SHORT).show();
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(uri);
image.compress(Bitmap.CompressFormat.JPEG,100,outstream);
outstream.close();
Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
imageView.setImageBitmap(getBitmapFromUri(uri));
}
我从json获取链接后调用downloadImage。
到这里一切都很好,但是在 Widget 上从 SharedPreferences 获取位图的 URI,然后尝试通过 setImageViewBitmap 方法将位图设置为 remoteViews,但没有用,然后尝试通过 setImageViewUri 通过获取图像的真实地址来设置它,然后一个 mediastore 提供但是仍然没有工作。它给了我这些错误 E/libEGL: Invalid file path for libcolorx-loader.so 和 E/OplusCustomizeRestrictionManager: sInstance is null,start a new sInstance
这是小部件代码:
public class Playlist extends AppWidgetProvider {
public static String thumbnailPath;
@Override
public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds) {
Log.d("1","1In OnUpdate.");
SharedPreferences settings = context.getSharedPreferences("PlaylistWidgetPreferences",Context.MODE_PRIVATE);
thumbnailPath = settings.getString("thumbnailPath","Hi");
Log.d("URI : ",thumbnailPath);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.playlist);
//remoteViews.setImageViewBitmap(R.id.videoThumbnail,BitmapFactory.decodeFile(thumbnailPath));
remoteViews.setImageViewUri(R.id.videoThumbnail,Uri.parse(thumbnailPath));
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context,appWidgetManager,appWidgetId);
}
}
我已为应用程序的清单文件添加了写入权限。活动和小部件上的图像视图都没有给出任何默认源文件。任何帮助将不胜感激,谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。