Giv*_*oot 2 audio video android get mms
我开始开发一个必须与MMS附件进行交互的android应用,尤其是获取附件(例如文本,位图,音频,视频等)并将其存储在手机上的特定文件夹中。
因此,我开始在网络上阅读一些书籍和一些帖子,但这并不是一个很普遍的论点,而且我也没有找到一种正式的方式来做自己想做的事情。
我在这里找到有关堆栈溢出的相当不错的文章:如何在Android中读取MMS数据?...对我来说效果很好,但是有两个问题:
我对没有官方教程或“操作方法”对此感到非常失望,因为SMS和MMS管理是移动开发中非常普遍的需求。我希望有一个人可以帮助我....
提前致谢!!
我找到了一种从MMS读取视频/音频数据的相当简单的方法,因此我决定为所有需要此功能的用户发布本课的这一部分,该部分提供MMS附件。
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();
}
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您可以通过传递以下内容从MMS中提取“原始”数据,例如音频/视频/图像:
拥有byte []后,可以创建一个空文件,然后使用FileOutputStream将byte []写入其中。如果文件路径\扩展名正确,并且您的应用具有所有正确的权限,则可以存储数据。
PS。此过程已经过几次测试,并且可以正常工作,但是我不排除可能发生错误状态的一些非托管异常情况。恕我直言,它也可以改进...
| 归档时间: |
|
| 查看次数: |
4243 次 |
| 最近记录: |