简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM