【发布时间】:2013-12-10 15:37:30
【问题描述】:
尝试使用存储在 Raw 文件夹 eclipse 中的可执行文件运行 FFmpeg 命令以用于 android 应用程序。我收到权限被拒绝错误,无法调整视频大小。如何从我的 java 文件中授予正确的权限。
【问题讨论】:
标签: android permissions ffmpeg command
尝试使用存储在 Raw 文件夹 eclipse 中的可执行文件运行 FFmpeg 命令以用于 android 应用程序。我收到权限被拒绝错误,无法调整视频大小。如何从我的 java 文件中授予正确的权限。
【问题讨论】:
标签: android permissions ffmpeg command
将此代码放在静态类中或任何您想要的地方:
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()));
}
希望有用!!
【讨论】:
您的 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" +whatever)
系统安装程序会自动为您解压可执行文件到/data/data/your.package.full.name/lib。
【讨论】: