Ускорение процесса doinbackground () - PullRequest
0 голосов
/ 19 февраля 2020

Я делю зашифрованное видео на 4 части, используя этот код

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;
}

И в своем файле активности я называю это с помощью метода doinbackground задачи asyn c, как показано ниже

 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;
    }

Несмотря на то, что оно разбивает видео, процесс занимает слишком много времени.
Как я могу ускорить их. Поскольку я показываю индикатор выполнения в методе preexecute, похоже, что пользователь видит индикатор выполнения в течение длительного времени, чего я не хочу.

...