因此,当我检查图像是否仍然存在时,我似乎收到了权限错误。在我的应用程序中,我将图像(路径(URI),而不是实际图像)添加到数据库记录。他们来自画廊/相机。在我们与服务器同步之前,我们使用以下方法检查图像是否仍然存在。
似乎没有人在Android 6之后找到该文件。我们在 Images.Media.query 上收到权限错误?我花了很多时间来讨论这个问题,但这一切似乎都围绕着意图。
public boolean doesPhotoExist(final Photo p) {
// checks Image media store for image existence
Cursor c = null;
try {
Log.d("doesPhotoExist", "Photo " + p.getImageId() + " has imageLocation " + p.getImageLocation());
if ((p == null) || ToolBelt.isNullorEmpty(p.getImageLocation())) {
return false;
}
String imgLoc = p.getImageLocation();
File file = new File(imgLoc);
if (file.exists()) {
//file exists on the file system - absolute path
return true;
}
if (isGcMediaImage(p)) {
//should have existed on teh file sytem
return false;
} else {
c = Images.Media.query(mContext.getContentResolver(), Uri.parse(p.getImageLocation()), null);
}
if (c == null || c.getCount() == 0) {
return false;
}
if (c.getCount() != 1) {
throw new AssertionError("More than one object returned from primary key query");
}
c.moveToFirst();
//It looks like for signatures(for Transfer Order questions), the file size is always 0 (even if the file is valid and exists), however,
//the MediaColumns.DATA column has a positive value.So, if we have come this far in this method and there does
//exist a record as per query above, it seems safe to return true in this particular case when actual data size is a positive value.
//Signatures are not displayed in SurveyCheckImage screen.
if (p.isSignature()) {
byte[] data = c.getBlob(c.getColumnIndex(MediaColumns.DATA));
if (data != null && data.length > 0) {
return true;
}
}
return c.getInt(c.getColumnIndex(MediaColumns.SIZE)) > 0;
} catch (SecurityException e) {
Log.e(tag(), "problem accessing media " + e);
return false;
} finally {
if ((c != null) && !c.isClosed()) {
c.close();
}
}
}