Can't save and convert bitmap to uri android studio

31 Views Asked by At

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.

1

There are 1 best solutions below

0
岳剑钟 On

Start camera

int currentapiVersion = android.os.Build.VERSION.SDK_INT; 
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
String fileName = System.currentTimeMillis() + ".jpg"; 
tempFile = new File(Environment.getExternalStorageDirectory() + savePath, fileName); 
if (currentapiVersion < 24) { 
    Uri fileUri = Uri.fromFile(tempFile);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); onActivityResult 
} else { 
    ContentValues contentValues = new ContentValues(1);
    contentValues.put(MediaStore.Images.Media.DATA, tempFile.getAbsolutePath());
    Uri fileUri = getApplication().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
}
startActivityForResult(intent, TAKE_PHOTOS_CODE); 

Read pictures and display them to ImangView

imageView.setImageURI(Uri.fromFile(tempFile));