Create a File from a Photo Uri on Android

Convert file: Uri to File in Android

What you want is...

new File(uri.getPath());

... and not...

new File(uri.toString());
Notes
  1. For an android.net.Uri object which is named uri and created exactly as in the question, uri.toString() returns a String in the format "file:///mnt/sdcard/myPicture.jpg", whereas uri.getPath() returns a String in the format "/mnt/sdcard/myPicture.jpg".
  2. I understand that there are nuances to file storage in Android. My intention in this answer is to answer exactly what the questioner asked and not to get into the nuances.

Create a file from a photo URI on Android

To allow the user to select the pictures I have the following in my Fragment:

This code is using ACTION_GET_CONTENT. Particularly on Android 7.0+, generally that (and ACTION_OPEN_DOCUMENT) will return Uri values with a content scheme. Your code assumes that you are getting Uri values with a file scheme, where the path actually has meaning. Moreover, your code assumes that the user is picking files on the filesystem that you can access, and there is nothing that forces the user to do that. ACTION_GET_CONTENT can be supported by apps where their content is:

  • A local file on external storage
  • A local file on internal storage for the other app
  • A local file on removable storage
  • A local file that is encrypted and needs to be decrypted on the fly
  • A stream of bytes held in a BLOB column in a database
  • A piece of content on the Internet that needs to be downloaded by the other app first
  • Content that is generated on the fly
  • ...and so on

Instead of using RequestBody.create(), use the InputStreamRequestBody from this OkHttp issue comment. You provide the same media type as before, but instead of a File (that you are incorrectly creating), you provide a ContentResolver (from getContentResolver() on a Context) and the Uri.

This blog post demonstrates how to use InputStreamRequestBody (specifically a Kotlin port of the original) to upload content in this fashion. This blog post provides another look at the same problem and a similar solution.

URI Location to save a photo

I created PictureUtils class for capture image from camera and Gallery. Here is below code

PictureUtils.java

public class PictureUtils {

private static String fileName;
public Activity activity;

public PictureUtils(Activity activity){
this.activity = activity;
}

public Uri openCameraIntent(int requestCode) {
Uri file = null;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
file = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", getOutputMediaFile());
} else {
file = Uri.fromFile(getOutputMediaFile());
}

intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
activity.startActivityForResult(intent, requestCode);

return file;
}

public void openGalleryIntent(int requestCode) {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickPhoto.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivityForResult(pickPhoto, requestCode);
}

private static File getOutputMediaFile() {

File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/RVRB");

if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
String timeStamp = String.valueOf(System.currentTimeMillis());
fileName = timeStamp + ".jpg";

return new File(mediaStorageDir.getAbsolutePath() + File.separator + fileName);
}

public ImagesData resultFromCamera(Intent data) {

File imageFile = null;
float rotationDegree = 0;
String exifOrientation;
ExifInterface exif = null;

Bitmap rotatedBitmap = null;

File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/RVRB");
boolean success = true;
if (!storageDir.exists()) {
success = storageDir.mkdirs();
}
if (success) {
imageFile = new File(storageDir, fileName);
}

try {
exif = new ExifInterface(imageFile.getAbsolutePath());
exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);

if (Integer.parseInt(exifOrientation) >= 0 && Integer.parseInt(exifOrientation) <= 1) {
rotationDegree = 0;
} else if (Integer.parseInt(exifOrientation) >= 2 && Integer.parseInt(exifOrientation) <= 4) {
rotationDegree = 180;
} else if (Integer.parseInt(exifOrientation) >= 7 && Integer.parseInt(exifOrientation) >= 8) {
rotationDegree = 270;
} else {
rotationDegree = 90;
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
Bitmap thumbnail = null;
if (data != null && data.getData() != null) {
try {
thumbnail = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), data.getData());
Matrix matrix = new Matrix();
matrix.postRotate(rotationDegree);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(thumbnail, thumbnail.getWidth(), thumbnail.getHeight(), true);
rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);

if (imageFile != null) {
OutputStream fOut = new FileOutputStream(imageFile);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 70, fOut);
fOut.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {

thumbnail = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), Uri.fromFile(imageFile));

Matrix matrix = new Matrix();
matrix.postRotate(rotationDegree);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(thumbnail, thumbnail.getWidth(), thumbnail.getHeight(), true);
rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);

if (imageFile != null) {
OutputStream fOut = new FileOutputStream(imageFile);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 70, fOut);
fOut.close();
}

} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
try {
Bitmap bitmap = null;
imageFile = new File(storageDir, fileName);

bitmap = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), Uri.fromFile(imageFile));

if (bitmap != null && (bitmap.getHeight() < bitmap.getWidth())) {
Matrix matrix = new Matrix();
matrix.postRotate(rotationDegree);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);

if (imageFile != null) {
OutputStream fOut = new FileOutputStream(imageFile);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 70, fOut);
fOut.close();
}
} else {
rotatedBitmap = bitmap;
}

} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

return new ImagesData(imageFile, rotatedBitmap);
}

public ImagesData imageFromGallery(Intent data) {

File imageFile = null;
float rotationDegree = 0;
Bitmap rotatedBitmap = null;
try {
Bitmap bm = null;
Uri selectedImage = null;
if (data != null && data.getData() != null) {
try {
selectedImage = data.getData();
bm = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), selectedImage);

} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
bm = (Bitmap) data.getExtras().get("data");
} catch (Exception e) {
e.printStackTrace();
}
}

String timeStamp = String.valueOf(System.currentTimeMillis());
String fileName = timeStamp + ".jpg";

File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/RVRB");
boolean success = true;
if (!storageDir.exists()) {
success = storageDir.mkdirs();
}
if (success) {
imageFile = new File(storageDir, fileName);
}

rotationDegree = getRotationFromURI(selectedImage, activity);

Matrix matrix = new Matrix();
matrix.postRotate(rotationDegree);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bm, bm.getWidth(), bm.getHeight(), true);
rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);

if (imageFile != null) {
OutputStream fOut = new FileOutputStream(imageFile);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 70, fOut);
fOut.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return new ImagesData(imageFile, rotatedBitmap);
}

private int getRotationFromURI(Uri contentUri, Context mContext) {
Cursor cursor = null;
try {
String[] proj = new String[]{MediaStore.Images.ImageColumns.ORIENTATION};
cursor = mContext.getContentResolver().query(contentUri, proj, null, null, null);
assert cursor != null;
cursor.moveToFirst();
return cursor.getInt(0);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
return 0;
}

public class ImagesData {

File imageFile = null;
Bitmap bitmap = null;

private ImagesData(File imageFile, Bitmap bitmap) {
this.imageFile = imageFile;
this.bitmap = bitmap;
}

public File getImageFile() {
return imageFile;
}

public void setImageFile(File imageFile) {
this.imageFile = imageFile;
}

public Bitmap getBitmap() {
return bitmap;
}

public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
}
}

Now you can call camera intent using below way on click of any button.

Global Declaration : Uri fileUri;

PictureUtils images = new PictureUtils(ArtistAddEditMembers.this);
fileUri = images.openCameraIntent(REQUEST_OPEN_CAMERA);

You can handel the result in onActivityResult method using below way.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_OPEN_CAMERA) {
try {
PictureUtils images = new PictureUtils(ArtistAddEditMembers.this);
PictureUtils.ImagesData imagesData = images.resultFromCamera(data);

imageFile = imagesData.getImageFile();
finalBitmap = imagesData.getBitmap();

displayImagePreview(finalBitmap);

} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == REQUEST_OPEN_GALLERY) {
try {

PictureUtils images = new PictureUtils(ArtistAddEditMembers.this);
PictureUtils.ImagesData imagesData = images.imageFromGallery(data);

imageFile = imagesData.getImageFile();
finalBitmap = imagesData.getBitmap();

displayImagePreview(finalBitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

For Display image in imageview

public void displayImagePreview(Bitmap bitmap) {
Glide.with(mContext).load(bitmap).into(ivUserProfile);
}

or you can simply set image bitmap in imageview without using glide.

I handeled below case in above example.

  • If version is >= N, the I am using FileProvider.getUriForFile and Uri.fromFile for lower version.
  • Display right rotation of Image instead of Display image after rotate.

Add below code in AndroidManefest.xml file under application tag for file provider

<application
......>

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
</application>

Create filepaths.xml file under xml resource directory

<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="."/>
</paths>

Make sure you popup and handel runtime permission for Camera and Write Internal Storage permission before call camera intent.

Creating file from Uri

You can try this;

try {
Uri uri = data.getData();
String selectedFilePath = FilePath.getPath(getActivity(), uri);
final File file = new File(selectedFilePath);

new UploadFileToServer().execute(file);
}
catch (Exception e)
{
e.printStackTrace();
}

and define FilePath class like this;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;

public class FilePath {

/**
* Method for return file path of Gallery image/ Document / Video / Audio
*
* @param context
* @param uri
* @return path of the selected image file from gallery
*/
public static String getPath(final Context context, final Uri uri) {

// check here to KITKAT or new version
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {

// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];

if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/"
+ split[1];
}
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {

final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),
Long.valueOf(id));

return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];

Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}

final String selection = "_id=?";
final String[] selectionArgs = new String[] { split[1] };

return getDataColumn(context, contentUri, selection,
selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {

// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();

return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}

return null;
}

/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context
* The context.
* @param uri
* The Uri to query.
* @param selection
* (Optional) Filter used in the query.
* @param selectionArgs
* (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri,
String selection, String[] selectionArgs) {

Cursor cursor = null;
final String column = "_data";
final String[] projection = { column };

try {
cursor = context.getContentResolver().query(uri, projection,
selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}

/**
* @param uri
* The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri
.getAuthority());
}

/**
* @param uri
* The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri
.getAuthority());
}

/**
* @param uri
* The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri
.getAuthority());
}

/**
* @param uri
* The Uri to check.
* @return Whether the Uri authority is Google Photos.
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri
.getAuthority());
}
}

how to convert URI to File Android 10

You cannot convert the direct file to URI in android 10 instead of this you can make a copy of the file into your file directory which will help you to get a file object.

File f = getFile(getApplicationContext(), uri);

The below method provide you file object of URI and also you have a copy of the file in your file directory.

    public static File getFile(Context context, Uri uri) throws IOException {
File destinationFilename = new File(context.getFilesDir().getPath() + File.separatorChar + queryName(context, uri));
try (InputStream ins = context.getContentResolver().openInputStream(uri)) {
createFileFromStream(ins, destinationFilename);
} catch (Exception ex) {
Log.e("Save File", ex.getMessage());
ex.printStackTrace();
}
return destinationFilename;
}

public static void createFileFromStream(InputStream ins, File destination) {
try (OutputStream os = new FileOutputStream(destination)) {
byte[] buffer = new byte[4096];
int length;
while ((length = ins.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
os.flush();
} catch (Exception ex) {
Log.e("Save File", ex.getMessage());
ex.printStackTrace();
}
}

private static String queryName(Context context, Uri uri) {
Cursor returnCursor =
context.getContentResolver().query(uri, null, null, null, null);
assert returnCursor != null;
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
String name = returnCursor.getString(nameIndex);
returnCursor.close();
return name;
}

for more detail refer here



Related Topics



Leave a reply



Submit