Skip to content Skip to sidebar Skip to footer

Android Q: Get Image From Gallery And Process It

I know it seems like a very basic question, but it's specifically for Android Q. I just want to get an image from Gallery and compress it and send to the server. But because of the

Solution 1:

Following things I've tried:

There is no possible reliable getImagePathFromUri() implementation.

In this custom logic, I have a method to handle sampling and rotation of the image as follows:

You do not need a File in that function. After all, your very first statement in that function goes and creates a Uri from that File. So, replace the File parameter with the Uri that you have, and skip the Uri.fromFile() call.

how can I use the image from external storage as a file in Android 10.

You can't. And, as demonstrated above, you do not need it for what you are doing.

If you find yourself in some situation where you are stuck using some library or API that absolutely positively must have a File:

  • Open an InputStream on the content, using contentResolver.openInputStream(), as you are doing today
  • Copy the bytes from that InputStream to some FileOutputStream on a file that you can read/write (e.g., getCacheDir() on Context)
  • Use your copy with the library or API that requires a File

Solution 2:

Create a Directory for data to be stored in Android/data/package name by:

privatevoidcreateDir() {
    StringtimeStamp= utils.currentTimeStamp();
    FilestorageDir= getExternalFilesDir(null);
    File image;
    try {
        image = File.createTempFile(timeStamp, ".png", storageDir);
        Log.i("SANJAY ", "createDir: " + image.getPath());
    } catch (IOException e) {
        e.printStackTrace();
        Log.i("SANJAY ", "createDir: " + e.getMessage());

    }
}

now call the gallery intent:

 intent = newIntent();
 intent.setAction(Intent.ACTION_GET_CONTENT);
 intent.addCategory(Intent.CATEGORY_OPENABLE);
 intent.setType("image/*");

 startActivityForResult(intent, 100);

In onActivityResult():

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == 100) {
           UrimediaUri= data.getData();
            
            //display the imagetry {
                InputStreaminputStream= getBaseContext().getContentResolver().openInputStream(mediaUri);
                Bitmapbm= BitmapFactory.decodeStream(inputStream);

                ByteArrayOutputStreamstream=newByteArrayOutputStream();
                byte[] byteArray = stream.toByteArray();

                bind.photo.setImageBitmap(bm);
                //Log.i("SANJAY ", "onActivityResult: " + saveBitMap(this, bm));
                uri = Uri.fromFile(saveBitMap(this, bm));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        } 
    }
}

the get the Uri from File using this method:

private File saveBitMap(Context context, Bitmap Final_bitmap) {
    FilepictureFileDir=newFile(Environment.getExternalStorageDirectory()
            + "/Android/data/"
            + getApplicationContext().getPackageName()
            + "/"/*Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), ""*/);
    if (!pictureFileDir.exists()) {
        booleanisDirectoryCreated= pictureFileDir.mkdirs();
        if (!isDirectoryCreated)
            Log.i("SANJAY ", "Can't create directory to save the image");
        returnnull;
    }
    Stringfilename= pictureFileDir.getPath() + File.separator + System.currentTimeMillis() + ".jpg";
    FilepictureFile=newFile(filename);
    try {
        pictureFile.createNewFile();
        FileOutputStreamoStream=newFileOutputStream(pictureFile);
        Final_bitmap.compress(Bitmap.CompressFormat.PNG, 18, oStream);
        oStream.flush();
        oStream.close();
        Log.i("SANJAY ", "saveBitMap :: Save Image Successfully..");

    } catch (IOException e) {
        e.printStackTrace();
        Log.i("SANJAY", "There was an issue saving the image.");
        Log.i("SANJAY", "Error :: " + e.getLocalizedMessage());
    }
    return pictureFile;
}

Post a Comment for "Android Q: Get Image From Gallery And Process It"