开发者

Android: Problem setting file permissions when filename has a space

Have an app where I store .png images in the app's cache directory, and as I am sharing these files via messaging, etc, I need to make the files readable temporarily by everyone (i.e. chmod 755).

As suggested in another thread, I am running Runtime.getRuntime.exec() to do this:

Runtime.getRuntime().exec("setperm chmod 755 /path/to/filename.png");

This works fine, and as I am filtering / and \, any name works... except a name with a space, unsurpisingly. This fails:

Runtime.getRuntime().exec("setperm chmod 755 /path/to/file name.png");

So, coming from linux, I try wrapping the file path in quotes, which works on linux, but still fails to change the file perms on Android:

开发者_StackOverflow社区Runtime.getRuntime().exec("setperm chmod 755 \"/path/to/file name.png\"");
or
Runtime.getRuntime().exec("setperm chmod 755 '/path/to/file name.png'");

Any ideas?

Thanks,

Paul


Have an app where I store .png images in the app's cache directory, and as I am sharing these files via messaging, etc, I need to make the files readable temporarily by everyone (i.e. chmod 755).

Don't put them in the cache dir. Use openFileOutput() and set MODE_WORLD_READABLE.

As suggested in another thread, I am running Runtime.getRuntime.exec() to do this:

Whoever suggested that to you is a moron. No Android application should be using exec(). There are no command-line binaries that are part of the SDK and that you can rely upon being there.


UPDATE

In a related android-developers thread, Dianne Hackborn (@hackbod) writes:

The recommended way to do this is to write a content provider, which the other app can call ContentResolver.openFileDescriptor() etc. It is actually really easy to write such a content provider -- it doesn't need a database or anything, just to implement ContentProvider.openFile().

Every situation I have seen where MODE_WORLD_* is used it causes more troubles than just writing a content provider. I regret having made that.


The exec() solution works well when in development you want to get access to your app's data but don't want to root your phone. I agree that it shouldn't be used in production code, but it sure saved me a lot of time firing up emulators just to get at my database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜