FFmpeg可执行文件Chmod权限

时间:2013-11-25 19:01:52

标签: android permissions ffmpeg command

尝试使用存储在原始文件夹eclipse for android app中的可执行文件运行FFmpeg命令。我收到权限被拒绝错误,无法调整视频大小。如何从我的java文件中提供正确的权限。

2 个答案:

答案 0 :(得分:2)

将此代码放在静态类中或任何您想要的位置:

public static void installBinaryFromRaw(Context context, int resId, File file) {
    final InputStream rawStream = context.getResources().openRawResource(resId);
    final OutputStream binStream = getFileOutputStream(file);

    if (rawStream != null && binStream != null) {
        pipeStreams(rawStream, binStream);

        try {
            rawStream.close();
            binStream.close();
        } catch (IOException e) {
            Log.e(TAG, "Failed to close streams!", e);
        }       

        doChmod(file, 777);
    }       
}
public static OutputStream getFileOutputStream(File file) {
    try {
        return new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        Log.e(TAG, "File not found attempting to stream file.", e);
    }
    return null;
}

public static void pipeStreams(InputStream is, OutputStream os) {
    byte[] buffer = new byte[IO_BUFFER_SIZE];
    int count;
    try {
        while ((count = is.read(buffer)) > 0) {
            os.write(buffer, 0, count);
        }
    } catch (IOException e) {
        Log.e(TAG, "Error writing stream.", e);
    }
}
public static void doChmod(File file, int chmodValue) {
    final StringBuilder sb = new StringBuilder();
    sb.append("chmod");
    sb.append(' ');
    sb.append(chmodValue);
    sb.append(' ');
    sb.append(file.getAbsolutePath());

    try {
        Runtime.getRuntime().exec(sb.toString());
    } catch (IOException e) {
        Log.e(TAG, "Error performing chmod", e);
    }
}

并使用此代码调用它:

private void installFfmpeg() {
    File ffmpegFile = new File(getCacheDir(), "ffmpeg");
    mFfmpegInstallPath = ffmpegFile.toString();
    Log.d(TAG, "ffmpeg install path: " + mFfmpegInstallPath);

    if (!ffmpegFile.exists()) {
        try {
            ffmpegFile.createNewFile();
        } catch (IOException e) {
            Log.e(TAG, "Failed to create new file!", e);
        }
        Utils.installBinaryFromRaw(this, R.raw.ffmpeg, ffmpegFile);
    }else{
        Log.d(TAG, "It was already installed");
    }

    ffmpegFile.setExecutable(true);
    Log.d(TAG, String.valueOf(ffmpegFile.canExecute()));
}

希望它有用!!

答案 1 :(得分:1)

您的Android应用必须具有读取和写入存储空间的权限,AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />.

另一个警告是路径,/storage/emulated/0/并非在所有设备上都可用,您应该使用Environment.getExternalStorageDirectory()来查找实际路径。

最后,有一个简单的解决方法,而不是从原始文件夹中提取ffmpeg,将ffmpeg重命名为lib...ffmpeg...so并将其放入项目中的目录libs/armeabi

当然,您稍后会运行Runtime.getRuntime().exec(getContext().getApplicationInfo().nativeLibraryDir + "/lib...ffmpeg...so" + )

系统安装程序会自动将可执行文件解压缩到/data/data/your.package.full.name/lib