我可以编程方式重命名可移动SD卡上的文件

时间:2015-08-02 16:10:05

标签: android ffmpeg android-sdcard file-rename

我尝试下面的代码重命名文件。它适用于内部存储,但不适用于可移动的SD卡文件。我需要重命名可移动SD卡上的文件以删除文件名中的空格和特殊字符,因为我正在使用fmpeg命令削减视频。

File from = new File(originalpath);
String rename=originalpath.replace(" ", "").replace("-","").replace(",","").trim();
File to = new File(rename);
Boolean result= from.renameTo(to);

OR

File from = new File(originalpath);
File to=new File((Environment.getExternalStorageDirectory()),"gvideo.mp4");
Boolean result2= from.renameTo(to);

它们都给出结果错误

添加有关command-

的详细信息
execFFmpegBinary("-i " + from.getAbsolutePath() + " -ss " + startMs / 1000 + " -to " + endMs / 1000 + " -strict -2 -async 1 " + dest.getAbsolutePath());
private void execFFmpegBinary(final String command) {
        try {
            ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
                @Override
                public void onFailure(String s) {
                    Log.e("Previewragment", "FAILED with output : " + s);
                }

                @Override
                public void onSuccess(String s) {
                    Log.e("Previewragment", "SUCCESS with output : " + s);
                }

                @Override
                public void onProgress(String s) {
                    Log.e("Previewragment", "Started command : ffmpeg " + command);
                    Log.e("Previewragment", "progress : " + s);
                }

                @Override
                public void onStart() {
                    Log.e("Previewragment", "Started command : ffmpeg " + command);

                }

                @Override
                public void onFinish() {
                    Log.e("Previewragment", "Finished command : ffmpeg " + command);



                }
            });
        } catch (FFmpegCommandAlreadyRunningException e) {
            // do nothing for now
        }
    }

失败讯息 -

FAILED with output : WARNING: linker: /data/data/xyz/files/ffmpeg has text relocations. This is wasting memory and prevents security hardening. Please fix.
    ffmpeg version n2.4.2 Copyright (c) 2000-2014 the FFmpeg developers
    built on Oct  7 2014 15:11:41 with gcc 4.8 (GCC)
    configuration: --target-os=linux --cross-prefix=/home/sb/Source-Code/ffmpeg-android/toolchain-android/bin/i686-linux-android- --arch=x86 --cpu=i686 --enable-runtime-cpudetect --sysroot=/home/sb/Source-Code/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/sb/Source-Code/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/sb/Source-Code/ffmpeg-android/build/x86 --extra-cflags='-I/home/sb/Source-Code/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all -march=i686' --extra-ldflags='-L/home/sb/Source-Code/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
    libavutil      54.  7.100 / 54.  7.100
    libavcodec     56.  1.100 / 56.  1.100
    libavformat    56.  4.101 / 56.  4.101
    libavdevice    56.  0.100 / 56.  0.100
    libavfilter     5.  1.100 /  5.  1.100
    libswscale      3.  0.100 /  3.  0.100
    libswresample   1.  1.100 /  1.  1.100
    libpostproc    53.  0.100 / 53.  0.100
    "/storage/emulated/0/Movies/m_ASUS_Display_Demo.mp4": No such file or directory

添加了完整的方法 -

private void executeTrimCommand(int startMs, int endMs) {

        File moviesDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_MOVIES
        );

        String filePrefix = "guggu";
        String fileExtn = ".mp4";
        String fileName = filePrefix + fileExtn;

       File from = new File(path);

        try {


            dest = new File(moviesDir, filePrefix + "_1" + fileExtn);

            if (dest.exists()) {
                dest.delete();
            }



            String command = String.format("-i \"%s\" -ss %d -to %d -strict -2 -async 1 \"%s\" ",path,startMs / 1000 , endMs / 1000, dest.getAbsolutePath());
            execFFmpegBinary(command);
        } catch (Exception e) {
            Log.e("Previewragment", e.toString());
            e.printStackTrace();
        }
    }

2 个答案:

答案 0 :(得分:1)

对于问题中的注释,String formatter的用法:

String command = String.format("-i \"%s\" -ss %d -to %d -strict -2 -async 1 \"%s\" ",to.getAbsolutePath(),startMs / 1000 , endMs / 1000, dest.getAbsolutePath());

答案 1 :(得分:0)

您的问题是,在shell的上下文中,从Environment.getExternalStorageDirectory()返回的路径将引发文件或目录不存在的错误。

您需要更改发送的所有文件路径以在shell中运行。这是一个静态工厂方法,您可以使用它来获取正确的文件:

/**
 * The external storage path is not readable by shell or root. This replaces {@link
 * Environment#getExternalStorageDirectory()} with the environmental variable
 * "EXTERNAL_STORAGE"
 *
 * @param file
 *         The file to check.
 * @return The original file (if it does not start with {@link
 * Environment#getExternalStorageDirectory()})
 * or a file with the correct path.
 */
@SuppressLint("SdCardPath")
public static File fileForShell(File file) {
    String externalStorage = Environment.getExternalStorageDirectory().getAbsolutePath();
    if (!file.getAbsolutePath().startsWith(externalStorage)) {
        return file;
    }

    String legacyStorage = System.getenv("EXTERNAL_STORAGE");
    String path;

    if (legacyStorage != null) {
        path = file.getAbsolutePath().replaceFirst(externalStorage, legacyStorage);
    } else {
        path = file.getAbsolutePath().replaceFirst(externalStorage, "/sdcard");
    }

    return new File(path);
}

创建命令时,只需使用上述方法即可获得正确的文件路径。

File from = ...
File fileToUseInCommand = fileForShell(from);
...