如何执行Android中在chmod - API 8? [英] How to execute a chmod in Android - API 8?

查看:476
本文介绍了如何执行Android中在chmod - API 8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的运行应用程序的问题而播放MP3,在Android 2.3。
这些MP3都是从远程服务器下载到/data/data/com.my.package.name/,默认的文件权限-rw -------。

I have a problem running my application which plays mp3, on Android 2.3. These mp3 are downloaded from a remote server to /data/data/com.my.package.name/, and the default file permissions are -rw-------.

在不同蜂窝后设备(根据我的测试),媒体播放器拒绝读取MP3,如果他们的文件权限-rw不-RW-RW -

Unlike on post HONEYCOMB devices (According to my tests), media player refuses to read mp3 if their file permissions are not -rw-rw-rw-.

我显然核实,如果我设置文件的权限为666与亚行外壳,然后媒体播放器成功地读取它。

I obviously verified that if I set files permissions to 666 with the adb shell, then the media player successfully read it.

所以,在网上一些研究之后,我试图实现以下code,只是写在文件后:

So, after some researches on the net, I tried to implement the following code, just after writing the file:

String chmodString = "chmod 666 " + getActivity().getApplicationContext().getFilesDir().getParentFile().getPath() +"/" + fileName;
Process sh = Runtime.getRuntime().exec("su", null, new File("/system/bin/"));
OutputStream osChgPerms = sh.getOutputStream();
try {
    osChgPerms.write((chmodString).getBytes("ASCII"));
    osChgPerms.flush();
    osChgPerms.close();
    sh.waitFor();
} catch (InterruptedException e) {
    Log.d("2ndGuide", "InterruptedException." + e);
} catch(IOException e) {
    Log.d("2ndGuide", "IO Exception." + e);
}

这code在一个片段执行,但我不认为改变任何东西。

This code is executed in a fragment but I don't think that changes anything.

不幸的是,如果这个code适用于蜂窝后的设备,它是没有用的,

Unfortunately, if this code works on post HONEYCOMB devices, where it's not useful,

osChgPerms.write((chmodString).getBytes("ASCII")); 

抛出 IOException异常:在Android 2.3破管,在这里我真的需要它

其实,我真的不知道,如果问题来自CHMOD还是从方式来执行它。

In fact, I don't really know if the problem comes from the chmod or from the way to execute it.

任何解决办法得到它的工作,或者让媒体播放器pre蜂窝工作不改变文件的权限?

Any solution to get it working or, to get the media player working on pre HONEYCOMB without changing file permissions?

推荐答案

非常好的问题,我在这个问题跑了一会儿回来!

Very good question, I ran in this problem a while back!

使用Java + Android的(用户必须具有CHMOD):

文件夹:

Runtime.getRuntime().exec("chmod -R 777 " + FOLDERNAME);

文件:

Runtime.getRuntime().exec("chmod 777 " + FILENAME);

问题:如果他们没有什么CHMOD二进制?您必须下载并包含在你的应用程序或资产。

Problem: What if they do not have the "chmod" binary? You must download and include it in your application or assets.

使用的Andr​​oid NDK + JNI(用户不需要搭配chmod但需要编译的NDK库):

/**
 * Change file permissions. Eg. chmod 777 FILE.
 * @param e Java environment.
 * @param c Java class.
 * @param file Path to file.
 * @param mode Permissions for the file.
 */
static jint executeCommand(JNIEnv *e, jclass __attribute__((__unused__))c, jstring file, jstring mode) {
    return (chmod(e->GetStringUTFChars(file, 0), strtol(e->GetStringUTFChars(mode, 0), 0, 8)));
}

问题:确保编译NDK与APP_ABI:=所有所有设备

Problem: Make sure to compile NDK with APP_ABI := all for all devices

对于Java(7+)+ Android的API 9+(用户不需要CHMOD或库):

/**
 * Change files to "0777"
 * @param path Path to file
 */
public static void changeFilePermission(final String path) {
    final File file = new File(path);
    file.setReadable(true, false);
    file.setExecutable(true, false);
    file.setWritable(true, false);
}

问题:你minSDK必须是9

Problem: Your minSDK must be 9!

这篇关于如何执行Android中在chmod - API 8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆