Home > Blockchain >  Using JSON to validate if a file already exists on a device or not
Using JSON to validate if a file already exists on a device or not

Time:06-17

I've got an assignment for school this week where we need to build an app in Android studio that endlessly queries a website that displays random cat pictures, validate if that picture is already downloaded on the device using JSON to check it's ID, download it if it's not already downloaded, and then display them one after another in an endless loop in a slideshow like setting. The app is being built with Android Studio, but to be honest the resources for this week's project are a bit on the light side.

From what I can gather, I need to establish an inputsteam to the network, build a JSON reader to scan the information, check the android device to make sure that the file isn't already there based on it's JSON ID, download it to the phone, then display it in a bitmap.

Problem is, I have no idea how to validate JSON data to determine duplicates, in this way or any other. Does anyone have any advice on how to do this? Bonus points if you can help me clarify the rest of the process as well, as I'm piece-mealing like crazy from different sources and don't have a very good grasp on how to even do this or create a proper JSON query.

Thank you all in advance!

CodePudding user response:

In case that you don`t have any ids, possible solution would be to read image as base64 string format, create new dictionary and set base64 as key.. in case that key is not in dictionary set id, else fetch id that was set before.

Each image has unique base64 string, so if two identical images appear they would have identical base64 string, in other case strings would differ- we can use it as unique identifier to determine if we have seen that image before or not (check if hashmap or. dictionary)

HashMap<String, Integer> dict = new HashMap<String, Integer>();
int id = 0


String base64Str = convertImageToBase64Str(imagePath)

Integer id = null;
if(dict.containsKey(key)) {
  id = dict.get(key);
}
else {
  dict.put(key, id);
  id  ;
}

public String convertImageToBaseStr(String imagePath) {
        File f = new File(imagePath);
        FileInputStream fin = new FileInputStream(f);
        byte imagebytearray[] = new byte[(int)f.length()];
        fin.read(imagebytearray);
        String imagetobase64 = Base64.encodeBase64String(imagebytearray);
        fin.close();
        return imagetobase64
}

CodePudding user response:

  1. you get some random pictures from website
  2. for each picture, you can calculate its base64 or hash by ite bytes data
  3. save this picture as a file in your device, and also save the mapping: base64/hash -> file_path
  4. when you get some new pictures, you also calculate their base64/hash, and use the mapping above to check if this picture already exists.
  • Related