1

I am working on app for Android versions 26 through 29. The code for 29, using mediaStore.insert is working fine and I won't show it here.

The problem occurs when I branch in code for < Q. The app takes photos and uploads them via a web api. The photos are also written to the DCIM directory and can be later selected and also uploaded.

Here is the code I am using for <Q to save the image in to DCIM

                String imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString();
                File image = new File(imagesDir, imageFileNameToUseAtWebServerEnd);
                try
                {

                    OutputStream fos = new FileOutputStream(image);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                    Objects.requireNonNull(fos).close();

                } catch (IOException e)
                {
                    Log.d(TAG, e.toString());
                }

and here is the code to select from DCIM (or any gallery) and generate a file object

    if (originalUri != null)
    {
        Log.d(TAG, "At top of handleChosenPhotoV2 - about to setPicV2");
        ContentResolver resolver = getContentResolver();
        Cursor returnCursor =
                resolver.query(originalUri, null, null, null, null);
        assert returnCursor != null;
        returnCursor.moveToFirst();

        int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        String name = returnCursor.getString(nameIndex);
        nameIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
        int intSize = returnCursor.getInt(nameIndex);

        String dateStamp = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String timeStamp = new SimpleDateFormat("HHmmSS").format(new Date());
        timeStamp = timeStamp.substring(0, 6);
        imageFileNameToUseAtWebServerEnd = strTrial + "_" + dateStamp + "_" + timeStamp + "_" + strUserId + ".jpg";


        try {
            InputStream stream = resolver.openInputStream(originalUri);
            File[] storageDir = getExternalMediaDirs();
            File image = File.createTempFile(
                    imageFileName,  /* prefix */
                    ".jpg",         /* suffix */
                    storageDir[0]     /* directory */
            );
            OutputStream outputStream = new FileOutputStream(image);

            copyStream(stream, outputStream);
            stream.close();
            outputStream.close();
            mCurrentPhotoPath = image.getAbsolutePath();
            f = new File(mCurrentPhotoPath);


        } catch (IOException e) {
            Log.d(TAG, e.toString());
        }
    }

I then take the retrieved jpg file and apply code to correct the rotation but it does not work. (the same code works fine on the photos when they first come in from the camera)

I can only assume that somewhere along the line I have lost the ExifInterface information.

Many thanks in advance for any suggestions Tony

Tony
  • 33
  • 4
  • `Here is the code I am using for – blackapps Oct 27 '21 at 10:58
  • `and here is the code to select from DCIM (or any gallery) and generate a file object` That code does not select anything. What i see is that it looks like a copy file function starting with a uri for the file to be copied. – blackapps Oct 27 '21 at 11:04
  • Thanks for the advice - this means I need to perform the rotation on the camera image before I save it to DCIM. For version Q the mediaStore.insert appears to take care of this for us - which is very nice. – Tony Oct 27 '21 at 11:26
  • If you let the Camera app take a picture it can be stored in DCIM directly by the app. I wonder why you wanna make a copy. I also wonder why you would rotate anything as all apps that handle or display images can handle rotation. So you can upload the file just as it is made by a Camera app. What are you doing? What we still dont know is where you got that bitmap from. – blackapps Oct 27 '21 at 11:34
  • Here's my first posting when I started on this: https://stackoverflow.com/questions/69675358/android-10-write-file-to-public-dcim-directory-non-deprecated-method/69677155#69677155. If you start reading where it says "Following the suggestion to use media store" you will see where I edited my original question. I couldn't see how to write the camera photo directly to DCIM under Android 10 and instead worked with a local app private copy for rotation, resizing, display and for the user to hit a button to upload to a web api – Tony Oct 27 '21 at 12:42
  • Android version does not matter. You can let the Camera app write the picture directly to DCIM using File class and FileProvider. And also directly to DCIM if you use MediaStore.insert() to obtain an uri. And dont rotate the image for uploading. The web can handle rotation. Any program can. – blackapps Oct 27 '21 at 16:23
  • Many thanks blackapps - I will do some more reading. (though I might come back to you in a few days and beg you for a little code snippet if I can't work it out on my own) – Tony Oct 28 '21 at 04:10

0 Answers0