I am currently working on my pet project. The plot is next: user can chose 2 different files and compare them by hashsum. The main problem is that I cannot read file because of this annoying filenames like "image/9", that causes the problem with file reading. I will be very appreciated for our help.
This is main Java code
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MainActivity extends AppCompatActivity {
String current = "file1";
String hash1 = " ";
String hash2 = " ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
}
}
}
private void getHashDigest(String path) throws IOException {
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, path.split(":")[1]);
int k = 0;
byte[] digest = null;
try {
byte fileData;
MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
DataInputStream dis = new DataInputStream(new FileInputStream(file.getAbsolutePath()));
while (dis.available() > 0) {
fileData = dis.readByte();
messageDigest.update(fileData);
k++;
}
dis.close();
digest = messageDigest.digest();
if (this.current == "file1") {
hash1 = digest.toString();
} else {
hash2 = digest.toString();
}
} catch (IOException e) {
Log.e("Error!", e.toString());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
EditText tv = (EditText) findViewById(R.id.file1);
if (this.current == "file2") {
tv = (EditText) findViewById(R.id.file2);
}
if (requestCode == 1 && resultCode == RESULT_OK) {
Uri selectedFile = data.getData();
tv.setText(selectedFile.getPath());
try {
this.getHashDigest(selectedFile.getPath());
} catch (IOException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private void compare(){
EditText tv = (EditText) findViewById(R.id.results);
tv.setText(hash1 == hash2 ? "Matched!" : "Not Matched!");
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.getFile1:
this.getFile("file1");
break;
case R.id.getFile2:
this.getFile("file2");
break;
case R.id.calc:
compare();
break;
}
}
private void getFile(String id) {
Intent intent = new Intent()
.setType("*/*")
.setAction(Intent.ACTION_GET_CONTENT);
this.current = id;
startActivityForResult(Intent.createChooser(intent, "Select file"), 1);
}
}
And this is my XML markup
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="57dp"
tools:layout_editor_absoluteY="229dp"
android:padding="25dp">
<TextView
android:layout_width="wrap_content"
android:text="File 1:"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="15dp">
</TextView>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:inputType="numberDecimal"
android:id="@+id/file1">
</EditText>
<Button
android:layout_height="60dp"
android:layout_width="match_parent"
android:padding="10dp"
android:text="Choose file 1"
android:layout_marginTop="15dp"
android:id="@+id/getFile1"
android:onClick="onClick"
>
</Button>
<TextView
android:layout_width="wrap_content"
android:text="File 2:"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="15dp"
>
</TextView>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:inputType="numberDecimal"
android:id="@+id/file2">
</EditText>
<Button
android:layout_height="60dp"
android:layout_width="match_parent"
android:padding="10dp"
android:text="Choose file 2"
android:layout_marginTop="15dp"
android:id="@+id/getFile2"
android:onClick="onClick"
>
</Button>
<Button
android:layout_height="60dp"
android:layout_width="match_parent"
android:padding="10dp"
android:text="Calculate"
android:layout_marginTop="15dp"
android:id="@+id/calc"
>
</Button>
<TextView
android:layout_width="wrap_content"
android:text="Results:"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="15dp">
</TextView>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:inputType="numberDecimal"
android:id="@+id/results">
</EditText>
</LinearLayout>
Please help me, because I am out of ideas!