android中的MemoryFile有什么用 [英] what is the use of MemoryFile in android

查看:14
本文介绍了android中的MemoryFile有什么用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些字节写入共享内存.这是在我的 application1 中完成的.从我的另一个应用程序:application2 我想访问该共享内存以读取写入的字节.为此,我尝试使用 android 的 MemoryFile 类.我被困在如何在两个不同的应用程序之间引用相同的分片内存.我现在也很困惑 memoryFile 是否用于相同的目的.http://developer.android.com/reference/android/os/MemoryFile.html 这个链接是我找到的关于这个话题的.提前致谢.克里希纳

解决方案

如果您想通过 MemoryFile 进行跨进程使用,您可以使用以下 fugly 方法:

import android.os.MemoryFile;导入 android.os.ParcelFileDescriptor;导入 java.io.FileDescriptor;导入 java.lang.reflect.InvocationTargetException;导入 java.lang.reflect.Method;公共类 MemoryFileUtil {私有静态最终方法 sMethodGetParcelFileDescriptor;私有静态最终方法 sMethodGetFileDescriptor;静止的 {sMethodGetParcelFileDescriptor = get("getParcelFileDescriptor");sMethodGetFileDescriptor = get("getFileDescriptor");}公共静态 ParcelFileDescriptor getParcelFileDescriptor(MemoryFile 文件) {尝试 {返回 (ParcelFileDescriptor) sMethodGetParcelFileDescriptor.invoke(file);} catch (IllegalAccessException e) {抛出新的运行时异常(e);} catch (InvocationTargetException e) {抛出新的运行时异常(e);}}公共静态文件描述符 getFileDescriptor(MemoryFile 文件) {尝试 {返回(文件描述符)sMethodGetFileDescriptor.invoke(文件);} catch (IllegalAccessException e) {抛出新的运行时异常(e);} catch (InvocationTargetException e) {抛出新的运行时异常(e);}}私有静态方法 get(String name) {尝试 {返回 MemoryFile.class.getDeclaredMethod(name);} catch (NoSuchMethodException e) {抛出新的运行时异常(e);}}}

您应该关注的是 #getParcelFileDescriptor(MemoryFile) 方法,您可以从 ContentProvider#openFile(Uri, String) 的实现中返回该方法.>

I want to write some bytes to a shared memory. This is done in my application1. From my another application: application2 I want to access that shared memory to read the written bytes. For this purpose I tried using android's MemoryFile class. 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. http://developer.android.com/reference/android/os/MemoryFile.html this link I found regarding the topic. Thanks in advance. Krishna

解决方案

If you want some cross-process use with MemoryFile you can use the following fugly method:

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).

这篇关于android中的MemoryFile有什么用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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