简体   繁体   English

MemoryFile在android中的用途是什么?

[英]what is the use of MemoryFile in android

I want to write some bytes to a shared memory. 我想写一些字节到共享内存。 This is done in my application1. 这是在我的application1中完成的。 From my another application: application2 I want to access that shared memory to read the written bytes. 从我的另一个应用程序:application2我想访问该共享内存来读取写入的字节。 For this purpose I tried using android's MemoryFile class. 为此,我尝试使用android的MemoryFile类。 I am stuck as how to refer to the same shard memory between two different application. 我被困在如何在两个不同的应用程序之间引用相同的分片内存。 I am also now confused if memoryFile is used for the same purpose or not. 如果memoryFile用于相同的目的,我现在也很困惑。 http://developer.android.com/reference/android/os/MemoryFile.html this link I found regarding the topic. http://developer.android.com/reference/android/os/MemoryFile.html我发现这个主题的链接。 Thanks in advance. 提前致谢。 Krishna 克里希纳

If you want some cross-process use with MemoryFile you can use the following fugly method: 如果你想在MemoryFile中使用一些跨进程,你可以使用以下fugly方法:

import android.os.MemoryFile;
import android.os.ParcelFileDescriptor;

import java.io.FileDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MemoryFileUtil {
    private static final Method sMethodGetParcelFileDescriptor;
    private static final Method sMethodGetFileDescriptor;
    static {
        sMethodGetParcelFileDescriptor = get("getParcelFileDescriptor");
        sMethodGetFileDescriptor = get("getFileDescriptor");
    }

    public static ParcelFileDescriptor getParcelFileDescriptor(MemoryFile file) {
        try {
            return (ParcelFileDescriptor) sMethodGetParcelFileDescriptor.invoke(file);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    public static FileDescriptor getFileDescriptor(MemoryFile file) {
        try {
            return (FileDescriptor) sMethodGetFileDescriptor.invoke(file);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    private static Method get(String name) {
        try {
            return MemoryFile.class.getDeclaredMethod(name);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }
}

What you should be looking at is the #getParcelFileDescriptor(MemoryFile) method which you can return from an implementation of ContentProvider#openFile(Uri, String) . 您应该看到的是#getParcelFileDescriptor(MemoryFile)方法,您可以从ContentProvider#openFile(Uri, String) #getParcelFileDescriptor(MemoryFile) ContentProvider#openFile(Uri, String)的实现返回该方法。

I suspect memory files don't have the getParcelFileDescriptor method. 我怀疑内存文件没有getParcelFileDescriptor方法。 When I commented this getParcelFileDescriptor related methods and use getFileDescriptor. 当我评论这个getParcelFileDescriptor相关方法并使用getFileDescriptor时。 It worked nicely. 它工作得很好。

import java.io.FileDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.os.MemoryFile;

/**
 * Invoke hidden methods using reflection
 * 
 */
public class MemoryFileUtil {
    private static final Method sMethodGetFileDescriptor;
    static {
        sMethodGetFileDescriptor = get("getFileDescriptor");
    }

    public static FileDescriptor getFileDescriptor(MemoryFile file) {
        try {
            return (FileDescriptor) sMethodGetFileDescriptor.invoke(file);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    private static Method get(String name) {
        try {
            return MemoryFile.class.getDeclaredMethod(name);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }
}

And created File descriptor from memory file. 并从内存文件中创建了文件描述符。

FileDescriptor fd = MemoryFileUtil.getFileDescriptor(memFile);

MemoryFile can be used to map to physical memory. MemoryFile可用于映射到物理内存。 The result file descriptor (fd) can be passed to client (memory sharing side). 结果文件描述符(fd)可以传递给客户端(内存共享端)。 The client can map the same native fd to the same memory region. 客户端可以将相同的本机fd映射到相同的内存区域。 The memory can then be shared using the native fd, which can be mapped to java layer using InputStream. 然后可以使用本机fd共享内存,可以使用InputStream将其映射到java层。

Please refer to this link for more details: 有关详细信息,请参阅此链接:
Sharing memory using ashem. 使用ashem共享内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM