Android start little experience

Original link: http://www.cnblogs.com/imhurley/archive/2012/03/25/2417243.html

  He began to study the development of android, and began to think about the most, and should be about the same MFC. They are a ready-made framework, with a system of API, and object-oriented, think they felt quite simple, so categorically promised good teacher they can do, get started quickly. There is reading an article that is to do php directly because the company needs to start doing android, from scratch, the product launch, the time it takes a month! Well, I think he can I can! Then hurry off, go to the library to borrow a book or two, I have bought one!

     The first week, did not do anything to the teacher before going there on Wednesday, with the environment, to understand a few basic controls, and he let me look at things on parsing XML documents. Because in the past in their own windows and ubuntu above have had experience with, so one afternoon to get. The back is a variety of reading, learning how to use java (actually a set of ideas with C #), then I ran into a little trouble, what extends, super, implements, and see things inheritance and polymorphism package package aspects, because that is the same and I did not see how, then it really is very troublesome ah! But so fast stumbling through the first week, until Friday afternoon, Tan brother assigned me a task, that is, the next program, network download pictures, then show it. In this process, implementation is not very smooth, a Friday afternoon did not do it, although seen a lot of examples online, but did not realize I wrote, and because the studio all day Saturday to go to work, but also during the day Sunday , only a Sunday afternoon! My heart I want to be a little pressure ah, do not do a lot of embarrassing thing!

    Now starting Friday to talk about the details.

  Friday night, has been transferred android in the studio because the studio's mission accomplished. From the Internet to find a few examples, but can not achieve locally, a variety of errors, and then looked at each of their own, are summarized under the general process, and then try to complete their own, without success. Just finished the time, has been entangled debugging is not convenient, then think about playing a AlertWindow, he let the value of each variable in the program report, but because it is in the background, download tools package inside, the result is not successful, look for a lot of reasons , have not been resolved! Later make up the debug mode under the android, android and borrowed a cell phone, slowly studying the point!

  By Saturday night, from eleven in the evening began to adjust, high Toast function for a long time, on the other bag useless, had to be placed inside the main Activity make it happen, and then wrote a function tools, used from the specified URL inside download pictures, various unsuccessful, but a serious look useless error message (melon it, hee hee), then you do not know all kinds of problems! Shetenglebantian back, slowly began to see the error message, and finally found useless permission to access the network, and then added. But then tune, or not, the results can not access the network, it is tangled ah! Then two in the morning, stand up, go to bed.

  Sunday Oh, in the studio, around 6:00 had to work, but because the weather is good today, everyone clamoring to play, so go play, until 6:30 pm, just ready to go back to the bedroom to take a bath! Undressed in front of the computer hesitated, then began to transfer the program, and then carefully look at the error message. Errors discovered inside said Fatal error, Main, and then bing a bit, we found a problem with the configuration xml file, and then changed, then there are still many questions, feel tangled, which put the tools into the Activity code inside a function, then a tune hungry ok. Peace of mind to take a bath. The final program is so ah

package pictureyou.main;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.Button;


public class PictureYouActivity extends Activity {
	private ImageView imgView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imgView = (ImageView)findViewById(R.id.imageView1);
        Toast.makeText(getBaseContext(), "好像可以用", Toast.LENGTH_SHORT).show();
        try
        {
            URL url = new URL("http://bbs.qshpan.com/images/newind/logo.gif");
            try
            {
            	Bitmap bit = null;
            	HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            	//Bitmap bit = getImage(url);
				conn.setDoInput(true);
				conn.connect();
				InputStream is = conn.getInputStream();
				bit = BitmapFactory.decodeStream(is);
                if(bit != null)
                {
                    imgView.setImageBitmap(bit);
                }
                else
                {
                	Bitmap bitt1 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                    imgView.setImageBitmap(bitt1);
                }
            }
            catch(IOException e)
            {
            	e.printStackTrace();
            }
        }
        catch(MalformedURLException err)
        {
        	err.printStackTrace();
        }
        Button bu = (Button)findViewById(R.id.button1);
        bu.setOnClickListener(new Button.OnClickListener(){
    		public void onClick(View arg0) {
			// TODO Auto-generated method stub
        	Bitmap bitt1 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
            imgView.setImageBitmap(bitt1);
			}
        });
    }
    
	public Bitmap getImage(URL url) throws IOException
	{
		Bitmap bit = null;
		try{
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			int ints = conn.getResponseCode();
			if(ints == HttpURLConnection.HTTP_OK){
				conn.setDoInput(true);
				conn.connect();
				InputStream is = conn.getInputStream();
				int length = (int)conn.getContentLength();
				if(length != -1){
//					byte[] imageContent = new byte[length];
//					byte[] buff = new byte[512];
//					int readLength = 0,desPos = 0;
//					while((readLength = is.read(buff))>0){
//						System.arraycopy(buff,0,imageContent,desPos,readLength);
					bit = BitmapFactory.decodeStream(is);
				}
					//bit = BitmapFactory.decodeByteArray(imageContent, 0, imageContent.length);
			}
			else
			{
				bit = BitmapFactory.decodeResource(getResources(), R.drawable.a123);
			}
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
		return bit;
	}
}

  

 xml file like this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.24"
        android:text="Button" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/vv1"
        android:layout_weight="0.24"
        android:src="@drawable/a123" />

  

 Which tools inside the case, although there is no use, I can move the reference code

 

package pictureyou.tools;

import pictureyou.main.*;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.R;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import android.view.LayoutInflater;

import pictureyou.main.*;

public class LoadRemoteImage {
	public static Bitmap getImage(URL url,Object obj) throws IOException
	{
		Bitmap bit = null;
		try{
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			conn.setDoInput(true);
			conn.connect();
			InputStream is = conn.getInputStream();
			int length = (int)conn.getContentLength();
			if(length != -1){
				byte[] imageContent = new byte[length];
				byte[] buff = new byte[512];
				int readLength = 0,desPos = 0;
				while((readLength = is.read(buff))>0){
					System.arraycopy(buff,0,imageContent,desPos,readLength);
				}
				bit = BitmapFactory.decodeByteArray(imageContent, 0, imageContent.length);
			}
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
		return bit;
	}
}

  androidmanifest.xml inside the right to add

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pictureyou.main"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="9" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />   
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".PictureYouActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

  Under summary Well, that is after a serious look at the error log, no error log, on their own speculation, for aspects new to or not fly ah!

  Learn from the difficulties of learning, with easy to use from office. Do not start the problem and made so complicated to achieve the basic requirements is fundamental. I saw this in object-oriented programming practices inside too, although not the original words to say, but almost mean (learn not deep enough), have to pay attention in the future! For example, what AlertWindows, Toast is irrelevant things, you should not get ah.

  There are now, this instant thing, it is important to see examples ah! Learning speed can quickly!

 

Reproduced in: https: //www.cnblogs.com/imhurley/archive/2012/03/25/2417243.html

Guess you like

Origin blog.csdn.net/weixin_30438813/article/details/94953928