Created
February 29, 2020 10:54
-
-
Save nknr/84bcb6da40ec2dcf654177a65c796bbb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.database.Cursor; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.provider.MediaStore; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import timber.log.Timber; | |
public class FileNamePath { | |
private static String readFromFile(Context context, Uri uri, String fileName) { | |
try { | |
InputStream inputStream = null; | |
OutputStream output = null; | |
try { | |
inputStream = context.getContentResolver().openInputStream(uri); | |
File file = new File(context.getCacheDir(), fileName); | |
output = new FileOutputStream(file); | |
if (inputStream != null) { | |
byte[] buffer = new byte[4 * 1024]; // or other buffer size | |
int read; | |
while ((read = inputStream.read(buffer)) != -1) { | |
output.write(buffer, 0, read); | |
} | |
output.flush(); | |
return reduceFileSize(file).getAbsolutePath(); | |
} else { | |
Timber.e("Error while reading selected image"); | |
return null; | |
} | |
} finally { | |
if (output != null) output.close(); | |
if (inputStream != null) inputStream.close(); | |
} | |
} catch (FileNotFoundException e) { | |
return null; | |
} catch (IOException e) { | |
return null; | |
} | |
} | |
public static String getFilePathFromContentUri(Context context, Uri selectedVideoUri) { | |
String fileName; | |
if (Build.VERSION.SDK_INT < 29) { | |
String[] filePathColumn = {MediaStore.MediaColumns.DATA}; | |
Cursor cursor = context.getContentResolver().query(selectedVideoUri, filePathColumn, null, null, null); | |
cursor.moveToFirst(); | |
int columnIndex = cursor.getColumnIndex(filePathColumn[0]); | |
fileName = getName(cursor.getString(columnIndex)); | |
cursor.close(); | |
} else { | |
String[] filePathColumn = {MediaStore.MediaColumns.DISPLAY_NAME}; | |
Cursor cursor = context.getContentResolver().query(selectedVideoUri, filePathColumn, null, null, null); | |
cursor.moveToFirst(); | |
fileName = cursor.getString(cursor.getColumnIndex(filePathColumn[0])); | |
cursor.close(); | |
} | |
return readFromFile(context, selectedVideoUri, fileName); | |
} | |
public static String getName(String filename) { | |
if (filename == null) { | |
return null; | |
} | |
int index = filename.lastIndexOf('/'); | |
return filename.substring(index + 1); | |
} | |
public static File reduceFileSize(File file) { | |
try { | |
BitmapFactory.Options o = new BitmapFactory.Options(); | |
o.inJustDecodeBounds = true; | |
o.inSampleSize = 6; | |
FileInputStream inputStream = new FileInputStream(file); | |
BitmapFactory.decodeStream(inputStream, null, o); | |
inputStream.close(); | |
final int REQUIRED_SIZE = 75; | |
int scale = 1; | |
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE) { | |
scale *= 2; | |
} | |
BitmapFactory.Options o2 = new BitmapFactory.Options(); | |
o2.inSampleSize = scale; | |
inputStream = new FileInputStream(file); | |
Bitmap selectedBitmap = BitmapFactory.decodeStream(inputStream, null, o2); | |
inputStream.close(); | |
file.createNewFile(); | |
FileOutputStream outputStream = new FileOutputStream(file); | |
selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); | |
return file; | |
} catch (Exception e) { | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment