来自媒体商店的图像的字符串文件路径并不总是相同的
String file path of image from media Store is not always the same
我正在尝试创建一个自定义照片库,用户可以在其中存储来自 MediaStore 照片库的图片,
我的目标是用户只能在数据库中存储不同的图像,如果它是重复的图像,它将不会存储。所以,我想嘿,我可以使用文件路径作为一种 ID 来检查重复的图像。但是每次我重新启动应用程序并重新选择相同的图片时,Uri/filepath 并不相同,特别是 filePath/Uri
的最后一部分
我检查重复的奇怪实现:
public void checkAndSetPhotoGalleryData() throws IOException {
RoomDB db = RoomDB.getInstance(this);
PhotoGalleryData photoGalleryData = new PhotoGalleryData();
String stringUri = String.valueOf(imageUri);
String lastPartFile = stringUri.substring(stringUri.lastIndexOf('/')+1);
TextView testThis = findViewById(R.id.testName);
testThis.setText(db_path);
if(photoGarList.isEmpty()) {
try {
InputStream iStream = getContentResolver().openInputStream(imageUri);
byte[] inputData = getBytes(iStream);
photoGalleryData.setKey_Value_Quiz(String.valueOf(lastPartFile));
photoGalleryData.setPhoto(inputData);
db.questionDao().insert(photoGalleryData);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}if(!photoGarList.isEmpty())
{for(int i = 0; i<photoGarList.size();i++){
if (photoGarList.get(i).getKey_Value_Quiz().length() > 0 && !photoGarList.isEmpty() && photoGarList.get(i).getKey_Value_Quiz().equals(String.valueOf(lastPartFile))) {
showLongToast("Are Identical");
}
if (photoGarList.get(i).getKey_Value_Quiz().length() > 0 && !photoGarList.isEmpty() && !photoGarList.get(i).getKey_Value_Quiz().equals(String.valueOf(lastPartFile)) && i == photoGarList.size()-1) {
try {
InputStream iStream = getContentResolver().openInputStream(imageUri);
byte[] inputData = getBytes(iStream);
photoGalleryData.setKey_Value_Quiz(String.valueOf(lastPartFile));
photoGalleryData.setPhoto(inputData);
db.questionDao().insert(photoGalleryData);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
文件路径的最后一部分:
///both of this are the same picture:
KeyValue:924270434
KeyValue:239256090
我想出于安全原因,他们每次都会更改最后一部分,所以我想知道除了 byte[] 或 Bitmap 之外,图片是否有任何常数因子(一次存储的大小太大了,这个事实并非如此。
您可以对图像进行简单的逐像素比较。
尝试使用ImageMagic:
ImageMagic
或者您可以尝试 Java OpenCV:How to compare two images using Java OpenCV library
我正在尝试创建一个自定义照片库,用户可以在其中存储来自 MediaStore 照片库的图片, 我的目标是用户只能在数据库中存储不同的图像,如果它是重复的图像,它将不会存储。所以,我想嘿,我可以使用文件路径作为一种 ID 来检查重复的图像。但是每次我重新启动应用程序并重新选择相同的图片时,Uri/filepath 并不相同,特别是 filePath/Uri
的最后一部分我检查重复的奇怪实现:
public void checkAndSetPhotoGalleryData() throws IOException {
RoomDB db = RoomDB.getInstance(this);
PhotoGalleryData photoGalleryData = new PhotoGalleryData();
String stringUri = String.valueOf(imageUri);
String lastPartFile = stringUri.substring(stringUri.lastIndexOf('/')+1);
TextView testThis = findViewById(R.id.testName);
testThis.setText(db_path);
if(photoGarList.isEmpty()) {
try {
InputStream iStream = getContentResolver().openInputStream(imageUri);
byte[] inputData = getBytes(iStream);
photoGalleryData.setKey_Value_Quiz(String.valueOf(lastPartFile));
photoGalleryData.setPhoto(inputData);
db.questionDao().insert(photoGalleryData);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}if(!photoGarList.isEmpty())
{for(int i = 0; i<photoGarList.size();i++){
if (photoGarList.get(i).getKey_Value_Quiz().length() > 0 && !photoGarList.isEmpty() && photoGarList.get(i).getKey_Value_Quiz().equals(String.valueOf(lastPartFile))) {
showLongToast("Are Identical");
}
if (photoGarList.get(i).getKey_Value_Quiz().length() > 0 && !photoGarList.isEmpty() && !photoGarList.get(i).getKey_Value_Quiz().equals(String.valueOf(lastPartFile)) && i == photoGarList.size()-1) {
try {
InputStream iStream = getContentResolver().openInputStream(imageUri);
byte[] inputData = getBytes(iStream);
photoGalleryData.setKey_Value_Quiz(String.valueOf(lastPartFile));
photoGalleryData.setPhoto(inputData);
db.questionDao().insert(photoGalleryData);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
文件路径的最后一部分:
///both of this are the same picture:
KeyValue:924270434
KeyValue:239256090
我想出于安全原因,他们每次都会更改最后一部分,所以我想知道除了 byte[] 或 Bitmap 之外,图片是否有任何常数因子(一次存储的大小太大了,这个事实并非如此。
您可以对图像进行简单的逐像素比较。
尝试使用ImageMagic: ImageMagic
或者您可以尝试 Java OpenCV:How to compare two images using Java OpenCV library