Application will take a picture, but will not save it

Ethan Moore :

I'm trying to take a picture and save it as a jpeg. For your information, if it helps, this is the third activity in the program that the user would access, and I'm hoping to save the picture into the data/user/0/com.example.app/appdata/inv/inv_pics file. Here's what I have:


    static String currentPhotoPath;

    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = new File (MainActivity.path+"/com.example.app/appdata/inv/inv_pics");
        storageDir.mkdir();
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = image.getAbsolutePath();
        return image;
    }

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                ex.printStackTrace();
                // Error occurred while creating the File
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = Uri.parse((MainActivity.path+"/com.example.app/appdata/inv/inv_pics"));
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }

I'm loosely following this tutorial. I've made the same in my Manifest file, and have created the additional file_paths xml file. I had the Uri in the second method (photoURI) set up exactly how the method had it, with the arguments as it was in the tutorial. This was just producing errors, and so I basically hard-coded the file path to my app in there (which I know isn't "OOP") and now the code almost works.

When I run the app, and get to the activity, it opens the camera. That's great. I click the button to take the picture and the screen freezes for a second, but does not give me the "Retry" or "Ok" buttons at the bottom - the expected result.

Ideal function (again, for your information) :

Get to this activity. It opens a camera. (I am here.) When I take the picture, it would ask me "Retry" or "Ok" (as mentioned above.) If I click "Ok" the file name would be written to a text file where it would later be read in order to set the picture as the image on an Image Button.

Thanks in advance!

Edit :

I don't believe it's a permissions problem, a) I have the permissions declared in the Manifest, and b) the first method does actually create the file, it's just blank, which lets me know the code runs until at least the photoFile = createImageFile(); in the second method.

Since I know you'll probably ask, here's the errors it gives me when I try to use the code provided by the tutorial:

    Process: com.example.app, PID: 4700
    java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.app/files/Pictures/JPEG_20191201_235937_8382995102420149896.jpg
        at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
        at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
        at com.erthad.boutique.InventoryActivity2.dispatchTakePictureIntent(InventoryActivity2.java:59)
        at com.erthad.boutique.InventoryActivity2.access$000(InventoryActivity2.java:21)
        at com.erthad.boutique.InventoryActivity2$1.onClick(InventoryActivity2.java:82)
        at android.view.View.performClick(View.java:7341)
        at android.view.View.performClickInternal(View.java:7307)
        at android.view.View.access$3200(View.java:846)
        at android.view.View$PerformClick.run(View.java:27796)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7156)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)```
Ethan Moore :

The answer came to me after reading into DoFlamingo's comments and answers, so partial credit to him.

The problem was in fact that I was not writing to the right storage. But the solution was something I didn't even think of initially. The tutorial was actually spot on. I tried editing the directory in the paths.xml file to point to a directory I was using when I began writing the code (namely data/data/user/0/com.example.app/appdata/inv/inv_pics) when it should have pointed to exactly what DoFlamingo was saying - and the tutorial (namely storage/emulated/XXX).

So, here's to making things a lot more complicated then they needed to be. I'll leave the post up in case it helps some one.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=357346&siteId=1