I'm trying to convert an image to uri and save it to a folder with the camera, as far as taking pictures on the camera is not a problem but when you want to convert and save it the application will exit by itself. anyone please dear with some sample script code below.
ACTIVITY
ActivityResultLauncher<Intent> activityResultLauncher;
buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent ix = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
activityResultLauncher.launch(ix);
}
});
VIEW
activityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
Bundle exstra = result.getData().getExtras();
Uri imageUri;
Bitmap bitmap = (Bitmap) exstra.get("data");
WeakReference<Bitmap> result1 = new WeakReference<>(Bitmap.createScaledBitmap(bitmap,
bitmap.getHeight(),bitmap.getWidth(),false).copy(Bitmap.Config.RGB_565,true));
Bitmap bmP = result1.get();
imageUri = saveImage(bmP, UploadCamera.this);
imgT.setImageURI(imageUri);
lv_txt.setText(""+imageUri);
}
});
SAVE IMAGE
private Uri saveImage(Bitmap img, Context context) {
File imagesFolder = new File(context.getCacheDir(),"photome");
Uri uris = null;
try{
imagesFolder.mkdir();
File file = new File(imagesFolder,"captureimage.jpg");
FileOutputStream stream = new FileOutputStream(file);
img.compress(Bitmap.CompressFormat.JPEG,100,stream);
stream.flush();
stream.close();
uris = FileProvider.getUriForFile(context.getApplicationContext(),"",file);
} catch (FileNotFoundException e){
} catch (IOException e){
}
return uris;
}
I tried to add a provider in manifests but there was an error the application exited early and could not be run.
adding provider requests in manifests
<provider
android:authorities="${applicationId}.provider"
android:name="androidx.core.content.FileProvider"
android:exported="true"
android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"></meta-data>
</provider>
I hope you can help me with this problem.
Start camera
Read pictures and display them to ImangView