Android Question Error while loading bitmap from MMS database... Please help.

bsnqt

Active Member
Licensed User
Longtime User
Hi Erel

I am using ContentResolver and trying to access data from MMS that stored in phone. I can access the data from MMS parts, but I can not load it successfully into the ImageView. I already spent 2 - 3 days on it and cannot find out why I got error. Please help (see error quoted below).

B4X:
Sub Button1_Click
    Dim InboxUri As Uri
    InboxUri.Parse("content://mms/inbox")
    Dim cursor1 As Cursor = cr.Query(InboxUri, Null, "", Null, "")
    Dim mmsId As Int = 0
    For i = 0 To cursor1.RowCount - 1
        cursor1.Position = i
        '// To get the id of the very last MMS in the database, or any MMS just to test
        If i = 0 Then
            mmsId = cursor1.GetString("_id")
        End If
    Next
    Log("The MMS Id is = " & mmsId)
    cursor1.Close

    InboxUri.Parse("content://mms/part")
    Dim cursor2 As Cursor = cr.Query(InboxUri, Null, "mid = ?", Array As String(mmsId), "")
    Dim data() As Byte
    For i = 0 To cursor2.RowCount - 1
        cursor2.Position = i
        If cursor2.GetString("ct").Contains("image") Then
            'datapath = cursor2.GetString("_data")
            data = cursor2.GetBlob("_data")
        End If
    Next

    cursor2.Close

    Dim InputStream1 As InputStream
    InputStream1.InitializeFromBytesArray(data, 0, data.Length)
    'Read stream into image view
    Dim Bitmap1 As Bitmap
    Bitmap1.Initialize2(InputStream1) '// <-- ERROR IS HERE (WITH DEBUG MODE)
    InputStream1.Close
    ImageView1.Bitmap = Bitmap1
End Sub

Error log is as follow:

The MMS Id is = 92
main_button1_click (B4A line: 104)
Bitmap1.Initialize2(InputStream1)
java.lang.RuntimeException: Error loading bitmap.
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:521)
at b4a.example.main._button1_click(main.java:387)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:63)
at android.view.View.performClick(View.java:3574)
at android.view.View$PerformClick.run(View.java:14293)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
at dalvik.system.NativeStart.main(Native Method)

Edit: I attached herewith the test project. Please don't forget to get at least any 1 MMS received in your inbox (with sample image) before testing.
I also attached the screenshot of the folder where we can find the data of MMS, we can see that the file does exist there...
 

Attachments

  • MMSTest.zip
    7.7 KB · Views: 284
  • Screenshot1.jpg
    Screenshot1.jpg
    20.4 KB · Views: 339
Last edited:

bsnqt

Active Member
Licensed User
Longtime User
Hi Erel, thanks for your reply.

Actually I don't know how check the value of data in that point.
I just can log it with:
B4X:
Log(BytesToString(data, 0, data.Length, "UTF-8"))
In the debugger I got this:
/data/data/com.android.providers.telephony/app_parts/PART_1377512880232��

And in the Local variables window I got something similar to array of numbers (from 0 to 71) like this:
data[0=47, 1=100, 2=97, 3=116, 4=97, 5=47, 6=100, 7=97, 8=116, 9=97, 10=47, 11=99, 12=111, 13=109.... [I]until[/I] 71=0]
 
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
You are totally correct, that is the same question I asked myself, because if I run the same code with a file that I put into another folder by myself, it works well.

Just to add, by searching I found this piece of code, explaining how to read the MMS data (image) from stackoverflow but cannot convert it into B4A. I see it is not very similar to the way I am doing, please take a look.

B4X:
private Bitmap getMmsImage(String _id) {
    Uri partURI = Uri.parse("content://mms/part/" + _id);
    InputStream is = null;
    Bitmap bitmap = null;
    try {
        is = getContentResolver().openInputStream(partURI);
        bitmap = BitmapFactory.decodeStream(is);
    } catch (IOException e) {}
    finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {}
        }
    }
    return bitmap;
}

I don't know what is equivalent to "openInputStream(partURI)" in our in B4A (the data I got is actually using cursor.getBlob(...))
 
Last edited:
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
Hi Erel,

I composed a small library using Eclipse and the Java codes I posted earlier, those codes do the job well, now I can read the file.
Can we convert those Java codes into B4A? Thanks so much for your help so far.
 
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
Thanks a lot Erel for your support. At this moment, I will temporarily use what I can have :)
 
Upvote 0
Top