2011-11-17 34 views
12

Tôi muốn viết một số byte vào bộ nhớ dùng chung. Điều này được thực hiện trong ứng dụng của tôi1. Từ một ứng dụng khác của tôi: application2 Tôi muốn truy cập bộ nhớ chia sẻ đó để đọc các byte được ghi. Với mục đích này, tôi đã thử sử dụng lớp MemoryFile của android. Tôi đang mắc kẹt như thế nào để tham khảo cùng một bộ nhớ phân đoạn giữa hai ứng dụng khác nhau. Tôi cũng đang bối rối nếu memoryFile được sử dụng cho cùng một mục đích hay không. http://developer.android.com/reference/android/os/MemoryFile.html liên kết này tôi đã tìm thấy về chủ đề. Cảm ơn trước. Krishnaviệc sử dụng MemoryFile trong android

+2

Tôi nghĩ rằng "chính thức" đường để đi là sử dụng một nhà cung cấp nội dung, hoặc sử dụng một tập tin thường xuyên về lưu trữ bên ngoài. –

+1

+1 cho ContentProvider/ContentResolver – Guillaume

+0

kích thước của nội dung lớn (3-4 MB) và tôi cần viết nhanh, vì vậy tôi cần một cái gì đó giống như bộ nhớ dùng chung. Ngoài ra xin vui lòng nếu bạn có thể giúp tôi hiểu việc sử dụng memoryFile trong Android. –

Trả lời

12

Nếu bạn muốn có một số sử dụng cross-trình với MemoryFile bạn có thể sử dụng sau fugly phương pháp:

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); 
     } 
    } 
} 

Những gì bạn nên nhìn vào là #getParcelFileDescriptor(MemoryFile) phương pháp mà bạn có thể trở về từ một thực hiện của ContentProvider#openFile(Uri, String).

+1

Tôi nghi ngờ mã của tôi ném ngoại lệ tại MemoryFile.class.getDeclaredMethod ("getParcelFileDescriptor"); nhưng nó không thực sự goto bắt khoản tôi biết chỉ sau khi đặt một breakpoint có –

+0

Tôi thực sự không nhận được nó. Nếu tất cả các bạn đang làm là đi qua một FD, tại sao sử dụng ashmem ở tất cả? Bạn chỉ có thể sử dụng ParcelFileDescriptor.createPipe() và viết bất kỳ dữ liệu cục bộ nào bạn muốn trên nó. Không yêu cầu ashmem. –

3

Tôi nghi ngờ tệp bộ nhớ không có phương thức getParcelFileDescriptor. Khi tôi nhận xét phương thức getParcelFileDescriptor liên quan này và sử dụng getFileDescriptor. Nó hoạt động rất tốt.

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); 
     } 
    } 
} 

Và mô tả tệp được tạo từ tệp bộ nhớ.

FileDescriptor fd = MemoryFileUtil.getFileDescriptor(memFile); 
0

MemoryFile có thể được sử dụng để ánh xạ tới bộ nhớ vật lý. Bộ mô tả tệp kết quả (fd) có thể được truyền cho máy khách (bên chia sẻ bộ nhớ). Máy khách có thể ánh xạ cùng một fd gốc đến cùng vùng bộ nhớ. Bộ nhớ sau đó có thể được chia sẻ bằng cách sử dụng fd bản địa, có thể được ánh xạ tới lớp java bằng cách sử dụng InputStream.

Vui lòng tham khảo link này để biết thêm chi tiết:
Sharing memory using ashem.

Các vấn đề liên quan