Skip to content

Commit

Permalink
fix: android 13 file path issues
Browse files Browse the repository at this point in the history
  • Loading branch information
gowtham.balamurugan committed Jun 26, 2023
1 parent 0d85c6e commit cfdb52c
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 434 deletions.
5 changes: 0 additions & 5 deletions app/src/main/java/com/gowtham/videotrimmer/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,18 @@
import android.widget.Toast;
import android.widget.VideoView;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.cocosw.bottomsheet.BottomSheet;
import com.gowtham.library.utils.CompressOption;
import com.gowtham.library.utils.FileUtils;
import com.gowtham.library.utils.LogMessage;
import com.gowtham.library.utils.TrimType;
import com.gowtham.library.utils.TrimVideo;
import com.gowtham.library.utils.TrimmerUtils;

import java.io.File;

Expand Down
38 changes: 19 additions & 19 deletions library/src/main/java/com/gowtham/library/ui/ActVideoTrimmer.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
import com.gowtham.library.ui.seekbar.widgets.CrystalSeekbar;
import com.gowtham.library.utils.CompressOption;
import com.gowtham.library.utils.CustomProgressView;
import com.gowtham.library.utils.FileUtils;
import com.gowtham.library.utils.FileUtilKt;
import com.gowtham.library.utils.LocaleHelper;
import com.gowtham.library.utils.LogMessage;
import com.gowtham.library.utils.TrimVideo;
Expand Down Expand Up @@ -83,7 +83,7 @@ public class ActVideoTrimmer extends LocalizationActivity {

private Dialog dialog;

private Uri uri;
private Uri filePath;

private TextView txtStartDuration, txtEndDuration;

Expand Down Expand Up @@ -212,20 +212,20 @@ private void initPlayer() {
private void setDataInView() {
try {
Runnable fileUriRunnable = () -> {
uri = Uri.parse(bundle.getString(TrimVideo.TRIM_VIDEO_URI));
// String path = FileUtils.getPath(ActVideoTrimmer.this, uri);
String path=FileUtils.getRealPath(ActVideoTrimmer.this,uri);
uri = Uri.parse(path);
Uri uri = Uri.parse(bundle.getString(TrimVideo.TRIM_VIDEO_URI));
String path= FileUtilKt.getValidatedFileUri(ActVideoTrimmer.this,uri);
filePath = Uri.parse(path);
runOnUiThread(() -> {
LogMessage.v("VideoUri:: " + uri);
LogMessage.v("VideoPath:: " + filePath);
progressBar.setVisibility(View.GONE);
totalDuration = TrimmerUtils.getDuration(ActVideoTrimmer.this, uri);
totalDuration = TrimmerUtils.getDuration(ActVideoTrimmer.this, filePath);
imagePlayPause.setOnClickListener(v ->
onVideoClicked());
Objects.requireNonNull(playerView.getVideoSurfaceView()).setOnClickListener(v ->
onVideoClicked());
initTrimData();
buildMediaSource(uri);
buildMediaSource();
loadThumbnails();
setUpSeekBar();
});
Expand Down Expand Up @@ -282,10 +282,10 @@ private void seekTo(long sec) {
videoPlayer.seekTo(sec * 1000);
}

private void buildMediaSource(Uri mUri) {
private void buildMediaSource() {
try {
DataSource.Factory dataSourceFactory = new DefaultDataSource.Factory(this);
MediaSource mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(MediaItem.fromUri(mUri));
MediaSource mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(MediaItem.fromUri(filePath));
videoPlayer.addMediaSource(mediaSource);
videoPlayer.prepare();
videoPlayer.setPlayWhenReady(true);
Expand Down Expand Up @@ -505,7 +505,7 @@ private void trimVideo() {
//not exceed given maxDuration if has given
outputPath = getFileName();
LogMessage.v("outputPath::" + outputPath + new File(outputPath).exists());
LogMessage.v("sourcePath::" + uri);
LogMessage.v("sourcePath::" + filePath);
videoPlayer.setPlayWhenReady(false);
showProcessingDialog();
String[] complexCommand;
Expand All @@ -520,7 +520,7 @@ else if (isAccurateCut) {
//fastest trimming command however, result duration
//will be low accurate(2-3 secs)
complexCommand = new String[]{"-ss", TrimmerUtils.formatCSeconds(lastMinValue),
"-i", String.valueOf(uri),
"-i", String.valueOf(filePath),
"-t",
TrimmerUtils.formatCSeconds(lastMaxValue - lastMinValue),
"-async", "1", "-strict", "-2", "-c", "copy", outputPath};
Expand All @@ -543,18 +543,18 @@ private String getFileName() {
if (fileName != null && !fileName.isEmpty())
fName = fileName;
File newFile = new File(path + File.separator +
(fName) + fileDateTime + "." + TrimmerUtils.getFileExtension(this, uri));
(fName) + fileDateTime + "." + TrimmerUtils.getFileExtension(this, filePath));
return String.valueOf(newFile);
}

private String[] getCompressionCmd() {
MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
metaRetriever.setDataSource(String.valueOf(uri));
metaRetriever.setDataSource(String.valueOf(filePath));
String height = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
String width = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
int w = TrimmerUtils.clearNull(width).isEmpty() ? 0 : Integer.parseInt(width);
int h = Integer.parseInt(height);
int rotation = TrimmerUtils.getVideoRotation(this, uri);
int rotation = TrimmerUtils.getVideoRotation(this, filePath);
if (rotation == 90 || rotation == 270) {
int temp = w;
w = h;
Expand All @@ -564,7 +564,7 @@ private String[] getCompressionCmd() {
if (compressOption.getWidth() != 0 || compressOption.getHeight() != 0
|| !compressOption.getBitRate().equals("0k")) {
return new String[]{"-ss", TrimmerUtils.formatCSeconds(lastMinValue),
"-i", String.valueOf(uri), "-s", compressOption.getWidth() + "x" +
"-i", String.valueOf(filePath), "-s", compressOption.getWidth() + "x" +
compressOption.getHeight(),
"-r", String.valueOf(compressOption.getFrameRate()),
"-vcodec", "mpeg4", "-b:v",
Expand All @@ -577,15 +577,15 @@ else if (w >= 800) {
w = w / 2;
h = Integer.parseInt(height) / 2;
return new String[]{"-ss", TrimmerUtils.formatCSeconds(lastMinValue),
"-i", String.valueOf(uri),
"-i", String.valueOf(filePath),
"-s", w + "x" + h, "-r", "30",
"-vcodec", "mpeg4", "-b:v",
"1M", "-b:a", "48000", "-ac", "2", "-ar", "22050",
"-t",
TrimmerUtils.formatCSeconds(lastMaxValue - lastMinValue), outputPath};
} else {
return new String[]{"-ss", TrimmerUtils.formatCSeconds(lastMinValue),
"-i", String.valueOf(uri), "-s", w + "x" + h, "-r",
"-i", String.valueOf(filePath), "-s", w + "x" + h, "-r",
"30", "-vcodec", "mpeg4", "-b:v",
"400K", "-b:a", "48000", "-ac", "2", "-ar", "22050",
"-t",
Expand Down Expand Up @@ -666,7 +666,7 @@ private void showLocationAlert() {

private String[] getAccurateCmd() {
return new String[]{"-ss", TrimmerUtils.formatCSeconds(lastMinValue)
, "-i", String.valueOf(uri), "-t",
, "-i", String.valueOf(filePath), "-t",
TrimmerUtils.formatCSeconds(lastMaxValue - lastMinValue),
"-async", "1", outputPath};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.gowtham.library.utils;

import android.content.Context;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class FileCacheHandler {

public static String putFileInCache(Context context, InputStream fis)
{

FileOutputStream fos=null;
try
{
File f=new File(context.getCacheDir(), "temp_video_file");
if(f.exists()){
f.delete();
}
fos=new FileOutputStream(f);
CopyStream(fis,fos);
return f.getAbsolutePath();
}
catch(Exception e)
{
LogMessage.e(Log.getStackTraceString(e));
return "";
}
finally
{
try
{
fos.flush();
fos.close();
} catch (Exception e) {
LogMessage.e(Log.getStackTraceString(e));
}
}
}

public static void CopyStream(InputStream is, OutputStream os)
{
final int buffer_size = 1024;
try {

byte[] bytes = new byte[buffer_size];
for (; ; ) {

int count = is.read(bytes, 0, buffer_size);
if (count == -1) {
break;
}
os.write(bytes, 0, count);
os.flush();
}
} catch (Exception ex) {
LogMessage.e(Log.getStackTraceString(ex));
}
}
}
Loading

0 comments on commit cfdb52c

Please sign in to comment.