Compressor icon indicating copy to clipboard operation
Compressor copied to clipboard

onActivityResult - how to get the file object in order to use this library?

Open alonsd opened this issue 5 years ago • 1 comments

I am getting a URI on onActivityResult of my image file, but this library needs a File object. How do I get the file of the image from that intent?

Already tried the following -

File file = new File(originalDataUri.toString());
      try {
        File compressedFile = new Compressor(getContext()).compressToFile(file);
        cachedLocalImageUri = Uri.fromFile(compressedFile);
      } catch (IOException e) {
        e.printStackTrace();
      }

and got "file not found expection"

what should I do to get the file object?

alonsd avatar Sep 01 '19 12:09 alonsd

` public class FileUtil { private static final int EOF = -1; private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

private FileUtil() {

}

public static File from(Context context, Uri uri) throws IOException {
    InputStream inputStream = context.getContentResolver().openInputStream(uri);
    String fileName = getFileName(context, uri);
    String[] splitName = splitFileName(fileName);
    File tempFile = File.createTempFile(splitName[0], splitName[1]);
    tempFile = rename(tempFile, fileName);
    tempFile.deleteOnExit();
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(tempFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    if (inputStream != null) {
        copy(inputStream, out);
        inputStream.close();
    }

    if (out != null) {
        out.close();
    }
    return tempFile;
}

private static String[] splitFileName(String fileName) {
    String name = fileName;
    String extension = "";
    int i = fileName.lastIndexOf(".");
    if (i != -1) {
        name = fileName.substring(0, i);
        extension = fileName.substring(i);
    }

    return new String[]{name, extension};
}

private static String getFileName(Context context, Uri uri) {
    String result = null;
    if (uri.getScheme().equals("content")) {
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf(File.separator);
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}

private static File rename(File file, String newName) {
    File newFile = new File(file.getParent(), newName);
    if (!newFile.equals(file)) {
        if (newFile.exists() && newFile.delete()) {
            Log.d("FileUtil", "Delete old " + newName + " file");
        }
        if (file.renameTo(newFile)) {
            Log.d("FileUtil", "Rename file to " + newName);
        }
    }
    return newFile;
}

private static long copy(InputStream input, OutputStream output) throws IOException {
    long count = 0;
    int n;
    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    while (EOF != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

}

`

This will return a new file. Hope it works!

raghhav avatar Oct 05 '19 14:10 raghhav