如何访问Android的MMS资源,如视频/音频等? [英] How to access android MMS resources such as video/audio etc?

查看:152
本文介绍了如何访问Android的MMS资源,如视频/音频等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始开发一个Android应用程序,必须与MMS attachements互动,特别是得到attachements如文本,位图,音频,视频等,并将其存储在手机上的特定文件夹。

I started developing an android app that have to interact with MMS attachements, in particular, get attachements such as text, bitmaps, audio, video etc. and store them on the phone in a specific folder.

于是我开始阅读一些书籍,并在网络上的一些帖子,但它不是一个很普遍的说法,我也没有找到正式的办法做我想做的事情。

So i started reading some books and some post on the web but it isn't a very common argument, and i didn't find an official way to do what i want to do.

我发现了一个相当不错的文章在这里堆栈溢出这里:如何阅读彩信数据在Android中 ...它非常适合我,但有2个问题:

I found a fairly good article here on stack-overflow here: How to Read MMS Data in Android?... it works very well for me, but there are 2 problems:

  1. 在该文介绍了如何获得MMS数据通过查询在隐藏的短信,彩信内容提供商,而据我所知,谷歌并不能保证他们会保持目前的结构,每一个Android未来的版本更新
  2. 在这篇文章只介绍了如何获得文本数据和位图数据MMS ......什么视频/音频?我试图让从InputStream视频/音频流,如示例那样使用位图,遗憾的是,没有运气...

我很失望的是,没有官方的教程或如何做过这种说法,因为SMS和MMS管理是一个非常普遍的需求在移动的研究与开发。 我希望有人能帮助我....

I'm very disappointed about the absence of official tutorial or "How-To" over this argument because SMS and MMS management is a very common need in mobile developement. I hope someone can help me....

在此先感谢!

推荐答案

我发现了一个相当简单的方式来读取彩信视频/音频数据,所以我决定出版这部分我类,提供MMS attachements的,为所有用户需要这一点。

I found a fairly simple way to read Video/Audio data from MMS, so i decided to publish this part of my class that provides MMS attachements, for all users that need this.

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

在这种方式,您可以提取原始数据,如音频/视频从MMS /图片由传:

In this way you can extract "Raw" data such as Audio/Video/Images from MMS by passing:

  1. 在您需要使用此功能的情况下
  2. 彩信部分,它包含要提取数据的URI(为前的内容:// MMS /零件/ 2。)
  3. 的按地址参数,返回一个最终的错误code抛出的过程。

一旦你有你的byte [],您可以创建一个空文件,然后使用一个FileOutputStream写字节[]进去。如果文件路径\扩展名是正确的,如果你的应用程序拥有所有的权利 权限,你就可以存储你的数据。

Once you have your byte[], you can create an empty File and then use a FileOutputStream to write the byte[] into it. If the file path\extension is correct and if your app has all the right permissions, you'll be able to store your data.

PS。这个程序已经测试了几次,它的工作,但我不排除可能是某些非托管的例外情况下,可能会产生错误状态。恕我直言可以improoved太...

PS. This procedure has been tested a few times and it worked, but i don't exclude can be some unmanaged exception cases that may produce error states. IMHO it can be improoved too...

这篇关于如何访问Android的MMS资源,如视频/音频等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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