Isaac Askew
Isaac Askew

Reputation: 1291

Upload Android image to php server error

I am following the tutorial here along with somebody's help request here...

I am very new to Java/Android programming, and I'm not exactly sure how to 'launch' this section of code. In my AndroidManifest.xml I have it set as the first activity, assuming this would launch right when I started it:

    <activity
        android:name=".UploadImage"
        android:label="@string/title_activity_starting_point" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Unfortunately it doesn't work this way, because whenever I launch the app it automatically 'stops unexpectedly.' Any other Java class that I put right here launches fine, so I assumed it was something in the code. I'm just not sure where it needs to be for me to get the code 'running', as every time I compile it the program crashes on my phone.

LogCat says it is unable to resolve this line of code:

String image_str = Base64.encodeBase64String(byte_arr);
// old code was String image_str = Base64.encodeBytes(byte_arr), 
// but this didn't compile for me and a comment on another website 
// said that encodeBase64String could replace it...apparently not?

Upvotes: 0

Views: 297

Answers (2)

Isaac Askew
Isaac Askew

Reputation: 1291

Apparently the Base64 file I had entered got modified from the original one; the 'encodeBase64String' doesn't exist, and using:

int flag = 0;
String image_str = Base64.encodeToString(byte_arr, flag);

Fixed the problem.

Upvotes: 0

323go
323go

Reputation: 14274

Welcome to Android.

First off, locate your logCat in Eclipse. It'll be your best friend (and source of much frustration).

Secondly, once you find your logCat, you're likely going to see a NetworkOnMainThread exception. That means that you're attempting to execute a Network request on the main UI thread, which would cause the screen to "freeze" and stutter. It's a no-no, and as of Android 4.0, it will crash your app.

There is a way to explicitly override that, but it'd be better if you learned to avoid that to begin with -- no difficult at all. Just use an AsyncTask.

Lastly, be sure you have INTERNET permission in your Manifest.

Upvotes: 1

Related Questions