问题描述
我已经从带有 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));
}
到这里一切都很好,但是在 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 (将#修改为@)