Я пытался заставить пользователя выбрать изображение для шифрования с камеры или галереи. но когда я запускаю код, он заставляет меня выбирать с диска Google, то код шифрования не работает. Мне нужна помощь. Я попробовал каждое решение, которое смог найти.
вот код: выберите кнопку:
private void ChooseImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"),PICK_IMAGE_REQUEST);
}
кнопка шифрования:
btnEncrypt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EncryptImage();
if(isEncrypted){
btnUpload.setEnabled(true);
btnEncrypt.setEnabled(false);
hintMsg.setText("Image Encrypted Successfully! Upload it to Cloud.");
hintMsg.setTextColor(Color.GREEN);
}
}
});
методы шифрования:
private void EncryptImage() {
//Convert drawable to Bitmap
Drawable drawable = ContextCompat.getDrawable(EncryptImageActivity.this, R.drawable.ic_lock_outline_black_24dp);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100,stream);
InputStream is = new ByteArrayInputStream(stream.toByteArray());
//Create file
File outputFileEnc = new File(myDir,UUID.randomUUID().toString()+FILE_NAME_ENC);
try {
// Encrypt image
AESEncrypter.encryptToFile(my_key,my_spec_key,is, new FileOutputStream(outputFileEnc));
//Uri encFile = Uri.parse(myDir.toString());
//filePath = Uri.parse(outputFileEnc.toString());
isEncrypted = true;
imageView.setImageDrawable(drawable);
Toast.makeText(this, "Image Encrypted!", Toast.LENGTH_SHORT).show();
//UploadEncrypted(outputFileEnc);
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
}
private void UploadEncrypted(File outputFileEnc){
// storageReference = storage.getReferenceFromUrl(filePath).child("");
StorageReference ref = storageReference.child("images/"+ UUID.randomUUID().toString());
//Upload input stream to Firebase
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading...");
progressDialog.setMessage("Uploading Encrypted Image ...");
progressDialog.show();
InputStream stream = null;
try {
stream = new FileInputStream(outputFileEnc);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
UploadTask uploadTask = ref.putStream(stream);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
exception.printStackTrace();
progressDialog.dismiss();
Toast.makeText(EncryptImageActivity.this, "Upload Failed!", Toast.LENGTH_SHORT).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(EncryptImageActivity.this, "Upload successful!", Toast.LENGTH_SHORT).show();
}
});
}