0

Hello as you can see below I m trying to make a (android) app which check md5 hash of file this code works only for small files can someone help me?

final TextView informations = (TextView) findViewById(R.id.Informations);
            final EditText input = (EditText) findViewById(R.id.ToCrack);
            String filepath = data.getDataString();
            String rawtext;
            String hash;
            StringBuilder text = new StringBuilder();
            filepath = filepath.split("//")[1];
            File file = new File(filepath);
            Toast.makeText(getApplicationContext(),"Loading: "+filepath,Toast.LENGTH_LONG).show();
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            DataInputStream dis = null;
            try{
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                dis = new DataInputStream(bis);
                while (dis.available() != 0){
                    text.append(dis.readLine()+"\n");
                }
            }
            catch (IOException e){
                e.printStackTrace();
            }
            finally {
                try{
                    fis.close();
                    bis.close();
                    dis.close();
                }
                catch (IOException ex){
                    ex.printStackTrace();
                }
            }
            rawtext = text.toString().substring(0, text.length()-1);
            hash = new MD5(rawtext).hexdigest();
            if (hash.equals(input.getText().toString())){
                informations.setText("Hash correspond with the file!");
            }
            else{
                informations.setText("File hash= "+hash+"\nHashes does not correspond :(");
            }
            Toast.makeText(getApplicationContext(),"Copied file hash to clipboard.",Toast.LENGTH_LONG).show();
4

1 回答 1

2

Mobile device environments such as Android have limitations w.r.t the amount of memory that can be consumed by an application. Hence, reading large files to an in-memory data store as done in your code (using StringBuffer) is going to throw an OutOfMemory error.

Take a look at this question to know how you can overcome the problem.

于 2013-05-18T19:18:17.677 回答