Home » Android » android – Speeding up the doinbackground() process-Exceptionshub

android – Speeding up the doinbackground() process-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I’m splitting an encrypted video into 4 parts using this code

public class SplitVideoFile {
private static String result;
static  ArrayList<String>update=new ArrayList<>();
public static String main(File file) {

    try {
       // File file = new File("C:/Documents/Despicable Me 2 - Trailer (HD) - YouTube.mp4");//File read from Source folder to Split.
        if (file.exists()) {

            String videoFileName = file.getName().substring(0, file.getName().lastIndexOf(".")); // Name of the videoFile without extension
           // String path = Environment.getDataDirectory().getAbsolutePath().toString() + "/storage/emulated/0/Videointegrity";
            String path =  "/storage/emulated/0/Videointegrity";
          //  File myDir = new File(getFile, "folder");
            //myDir.mkdir();

            File splitFile = new File(path.concat("/").concat(videoFileName));//Destination folder to save.
            if (!splitFile.exists()) {
                splitFile.mkdirs();
                Log.d("Directory Created -> ", splitFile.getAbsolutePath());
            }

            int i = 01;// Files count starts from 1
            InputStream inputStream = new FileInputStream(file);
            String videoFile = splitFile.getAbsolutePath() +"/"+ String.format("%02d", i) +"_"+ file.getName();// Location to save the files which are Split from the original file.
            OutputStream outputStream = new FileOutputStream(videoFile);
            Log.d("File Created Location: ", videoFile);
            update.add("File Created Location: ".concat(videoFile));
            int totalPartsToSplit =4 ;// Total files to split.
            int splitSize = inputStream.available() / totalPartsToSplit;
            int streamSize = 0;
            int read = 0;
            while ((read = inputStream.read()) != -1) {

                if (splitSize == streamSize) {
                    if (i != totalPartsToSplit) {
                        i++;
                        String fileCount = String.format("%02d", i); // output will be 1 is 01, 2 is 02
                        videoFile = splitFile.getAbsolutePath() +"/"+ fileCount +"_"+ file.getName();
                        outputStream = new FileOutputStream(videoFile);
                        Log.d("File Created Location: ", videoFile);
                        streamSize = 0;
                    }
                }
                outputStream.write(read);
                streamSize++;
            }

            inputStream.close();
            outputStream.close();

            Log.d("Total files Split ->", String.valueOf(totalPartsToSplit));
        result="success";
        } else {
            System.err.println(file.getAbsolutePath() +" File Not Found.");
            result="failed";
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
public ArrayList<String> getUpdate()
{
    return update;
}

And in my activity file i call this using async task’s doinbackground method like below

 protected String doInBackground(Void...arg0) {
        Log.d(TAG + " DoINBackGround", "On doInBackground...");
        File encvideo=new File(epath.getText().toString());
        SplitVideoFile split=new SplitVideoFile();
        String result=split.main(encvideo);
        publishProgress(1);
        return result;
    }

Even though it splits the video, it takes too much of time to do the process.
How can I speed them up. As I’m showing a progress bar in preexecute method it looks like the user sees the progress bar for a long time, which I don’t want.

How to&Answers: