G
G
Greedov2019-10-14 08:25:08
Java
Greedov, 2019-10-14 08:25:08

How to save a picture received from a standard android camera?

I'm trying to save a photo taken from the system camera application, but the application crashes.

final int CAMERA_RESULT = 1;

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        File file = new File("/data/data/com.nexus.foto/Files", "foto.png");

        Uri outputFileUri1 = Uri.fromFile(file);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri1);

The application crashes on this line
startActivityForResult(intent, CAMERA_RESULT);
Logs that fit:
2019-10-14 08:19:00.594 14057-14057/com.nexus.foto E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.nexus.foto, PID: 14057
    android.os.FileUriExposedException: file:///data/data/com.nexus.foto/Files/foto.png exposed beyond app through ClipData.Item.getUri()
        at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
        at android.net.Uri.checkFileUriExposed(Uri.java:2354)
        at android.content.ClipData.prepareToLeaveProcess(ClipData.java:832)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:8929)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:8914)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1519)
        at android.app.Activity.startActivityForResult(Activity.java:4396)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
        at android.app.Activity.startActivityForResult(Activity.java:4355)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
        at com.nexus.foto.MainActivity.onClick(MainActivity.java:41)
        at android.view.View.performClick(View.java:5619)
        at android.view.View$PerformClick.run(View.java:22298)
        at android.os.Handler.handleCallback(Handler.java:754)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:165)
        at android.app.ActivityThread.main(ActivityThread.java:6375)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
2019-10-14 08:19:00.605 14057-14057/com.nexus.foto I/Process: Sending signal. PID: 14057 SIG: 9

I sincerely don't understand what's going on.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2019-10-14
@Greedov

To get a great photo, your application must implement a content provider. In the intent for the request, you pass an indication of where the camera should write the file to you.
YOU WILL NEED THE FOLLOWING FUNCTIONS / ACTIONS
File sharePath = new File(getCacheDir(), "share"); // in the folder allotted to my system . subfolder to share
photoFile = File.createTempFile() ; // the camera will write the snapshot
here photoURI = FileProvider.getUriForFile(); // convert it to stream content
MAIN . ANDROID DOES NOT LIKE WHEN APPLICATIONS CLIMB TO THE FILE SYSTEM OUTSIDE THE FOLDERS ISSUED TO IT
.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question