问题描述
我尝试下载图像,然后将其保存到图库中,然后在VIEW_ACTION
的帮助下使用FileProvider
进行查看,一切都很好,但是当我尝试查看图像时,显示“照片无法加载”
这是我如何创建文件
File f = new File(Environment.getExternalStorageDirectory(),SUB_DIR);
if (!f.exists()) {
f.mkdirs();
}
String path = f.getAbsolutePath();
DownloadRequestModel model = new DownloadRequestModel(image.id,PrefUtils.getBrandToken(getApplicationContext()));
这是我添加到画廊的方式
public static Uri galleryAddPic(Context context,String currentPhotoPath) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(currentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
return contentUri;
}
这就是我如何建立自己的意图
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri rightUri = BitmapUtils.galleryAddPic(getApplicationContext(),path + "/" + fileName);
Uri uri = FileProvider.getUriForFile(getApplicationContext(),getApplicationContext().getPackageName() + ".provider",f);
intent.setDataAndType(uri,"image/*");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
但是我发现从库生成的uri与从文件提供程序生成的uri不同
摘自画廊:file:///storage/emulated/0/PhlogBusiness/1597680349517.jpg 由文件提供商生成:content://com.example.ddopik.phlogbusiness.provider/external_files/PhlogBusiness
这是我的文件提供者路径
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
所以我不确定我应该更改文件提供者路径是什么问题还是有其他方法可以从图库中打开图像
整个课程代码
public class DownloaderService extends Service {
private static final String SUB_DIR = "PhlogBusiness";
public DownloaderService() {
}
public static final String TAG = UploaderService.class.getSimpleName();
public static final int DOWNLOAD_FILE = 0;
private NotificationFactory notificationFactory = new NotificationFactory();
private int downloading;
private int failedDownloadsCount = 0;
private int totalDownloadCount = 0;
private Messenger messenger = new Messenger(new Handler(message -> {
switch (message.what) {
case DOWNLOAD_FILE:
if (message.obj instanceof BaseImage) {
BaseImage image = (BaseImage) message.obj;
downloading++;
String fileName = new Date().getTime() + "." + image.extension;
File f = new File(Environment.getExternalStorageDirectory()+"/" + SUB_DIR + "/" +fileName);
if (!f.exists()) {
boolean j = f.mkdirs();
int x =9;
}
String path = f.getAbsolutePath();
DownloadRequestModel model = new DownloadRequestModel(image.id,PrefUtils.getBrandToken(getApplicationContext()));
String s = new Gson().toJson(model);
String encryptedS = null;
try {
encryptedS = Utilities.encrypt(s,new SecretKeySpec(BuildConfig.PAYMENT_SECRET.getBytes(),"AES"),BuildConfig.PAYMENT_IV_KEY);
} catch (Exception e) {
e.printStackTrace();
break;
}
String url = String.format(BaseNetworkApi.BASE_IMAGE_DOWNLOAD_URL + "/[%1$s]/download",encryptedS);
totalDownloadCount++;
handleNotification(null);
Rx2AndroidNetworking.download(url,path,fileName)
.setPriority(Priority.HIGH)
.build()
.startDownload(new DownloadListener() {
@Override
public void onDownloadComplete() {
downloading--;
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = FileProvider.getUriForFile(getApplicationContext(),BuildConfig.APPLICATION_ID + ".provider","image/*");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),(int) System.currentTimeMillis(),intent,PendingIntent.FLAG_CANCEL_CURRENT);
handleNotification(pendingIntent);
Toast.makeText(getApplicationContext(),R.string.download_complete,Toast.LENGTH_SHORT).show();
checkAndStop();
}
@Override
public void onError(ANError anError) {
downloading--;
failedDownloadsCount++;
handleNotification(null);
checkAndStop();
}
});
}
break;
}
return true;
}));
private void handleNotification(PendingIntent action) {
String message = getString(R.string.download_complete);
if (downloading == 0) {
if (failedDownloadsCount > 0) {
message = getString(R.string.download_failed,failedDownloadsCount);
}
totalDownloadCount = failedDownloadsCount = 0;
} else {
message = getString(R.string.downloading_n_files,downloading);
}
notificationFactory.changeNotificationPendingIntent(getApplicationContext(),getString(R.string.permanent_notification_channel_id),getString(R.string.permanent_notification_id),action,message,R.drawable.phlog_logo_yellow);
}
private void checkAndStop() {
if (downloading == 0 && bound == 0) {
stopForeground(false);
stopSelf();
}
}
private int bound = 0;
@Nullable
@Override
public IBinder onBind(Intent intent) {
bound++;
return messenger.getBinder();
}
@Override
public boolean onUnbind(Intent intent) {
bound--;
checkAndStop();
return super.onUnbind(intent);
}
@Override
public int onStartCommand(Intent intent,int flags,int startId) {
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
notificationFactory.createNotificationChannel(getApplicationContext(),getString(R.string.permanent_notification_channel_name),getString(R.string.permanent_notification_channel_desc));
Notification notification = notificationFactory.createNotification(getApplicationContext(),R.drawable.phlog_logo_yellow,getString(R.string.phlog_downloading),null,false,null);
startForeground(Integer.valueOf(getString(R.string.permanent_notification_id)),notification);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)