Android 数据存储机制

一、Android数据存储机制

  1. 使用SharedPreferences存储数据;
  2. 文件存储数据;
  3. SQLite数据库存储数据;
  4. 使用ContentProvider存储数据;
  5. 网络存储数据;
注意:Android 中的数据存储都是私有的,其他应用程序都是无法访问的,除非通过ContentResolver获取其他程序共享的数据。


二、如何访问彩信中附加内容

1.使用content provider访问附加内容;

2.访问视频/音频附件;

private static final int RAW_DATA_BLOCK_SIZE = 16384; //Set the block size used to write a ByteArrayOutputStream to byte[] public static final int ERROR_IO_EXCEPTION = 1; public static final int ERROR_FILE_NOT_FOUND = 2; public static byte[] LoadRaw(Context context, Uri uri, int Error){ InputStream inputStream = null; byte[] ret = new byte[0]; //Open inputStream from the specified URI try { inputStream = context.getContentResolver().openInputStream(uri); //Try read from the InputStream if(inputStream!=null) ret = InputStreamToByteArray(inputStream); } catch (FileNotFoundException e1) { Error = ERROR_FILE_NOT_FOUND; } catch (IOException e) { Error = ERROR_IO_EXCEPTION; } finally{ if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { //Problem on closing stream. //The return state does not change. Error = ERROR_IO_EXCEPTION; } } } //Return return ret; } //Create a byte array from an open inputStream. Read blocks of RAW_DATA_BLOCK_SIZE byte private static byte[] InputStreamToByteArray(InputStream inputStream) throws IOException{ ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int nRead; byte[] data = new byte[RAW_DATA_BLOCK_SIZE]; while ((nRead = inputStream.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } buffer.flush(); return buffer.toByteArray(); }  

你可能感兴趣的:(Android 数据存储机制)