Android-在应用程序中创建符号链接 [英] Android - create symbolic link in the app

查看:278
本文介绍了Android-在应用程序中创建符号链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中以编程方式创建符号链接.在Android(4.4+)中可以吗?

I want create programmatically symbolic link in my app. Is it possible in Android (4.4+)?

在Java中,我们可以使用:

In Java we can use:

Path newLink = ...;
Path target = ...;
try {
    Files.createSymbolicLink(newLink, target);
} catch (IOException x) {
    System.err.println(x);
} catch (UnsupportedOperationException x) {
    // Some file systems do not support symbolic links.
    System.err.println(x);
}

来自java.nio.file,但是我应该在Android中使用什么?

from java.nio.file but what I should use in Android?

https://docs.oracle.com/javase/tutorial/essential/io/links.html

我使用reflection/native code/OS.symlink() method进行了测试,但没有任何效果.我总是得到不允许操作(EPERM).我认为您必须具有root权限才能创建符号链接.

I tested using reflection/native code/OS.symlink() method and nothing work. I always get Operation not permitted (EPERM). I think you have to have root permission for create the symlink.

问题可能出在/mnt/sdcard是包裹/data/media/xxx的FUSE垫片.所以我开始使用/data/media/xxx,但我总是得到Permission denied

The problem can be with that /mnt/sdcard is a FUSE shim that wraps /data/media/xxx. So I started using /data/media/xxx but I always get Permission denied

我认为root权限存在问题.

I think it's a problem with root permissions.

推荐答案

以下是对我有用的解决方案,如果成功,则返回true:

Here's a solution that worked for me, which returns true iff succeeded :

public static boolean createSymLink(String symLinkFilePath, String originalFilePath) {
    try {
        if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
            Os.symlink(originalFilePath, symLinkFilePath);
            return true;
        }
        final Class<?> libcore = Class.forName("libcore.io.Libcore");
        final java.lang.reflect.Field fOs = libcore.getDeclaredField("os");
        fOs.setAccessible(true);
        final Object os = fOs.get(null);
        final java.lang.reflect.Method method = os.getClass().getMethod("symlink", String.class, String.class);
        method.invoke(os, originalFilePath, symLinkFilePath);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

或者在科特林:

companion object {
    @JvmStatic
    fun createSymLink(symLinkFilePath: String, originalFilePath: String): Boolean {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Os.symlink(originalFilePath, symLinkFilePath)
                return true
            }
            val libcore = Class.forName("libcore.io.Libcore")
            val fOs = libcore.getDeclaredField("os")
            fOs.isAccessible = true
            val os = fOs.get(null)
            val method = os.javaClass.getMethod("symlink", String::class.java, String::class.java)
            method.invoke(os, originalFilePath, symLinkFilePath)
            return true
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return false
    }
}

这篇关于Android-在应用程序中创建符号链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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