当前位置:操作系统 > 安卓/Android >>

android学习笔记16--------------Android实现网络图片与html源码获取

获取指定网址的图片:

 \
\

 

获取指定网址的html源码,这里是搜狐的网址举例。

 \

 

MainActivity

[java]
package com.luku.netImage; 
 
import com.luku.Junit.testJunit; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.Toast; 
 
public class MainActivity extends Activity 

    Button getImagebutton; 
    EditText imagePatheditText; 
    ImageView imageView; 
    Button exitbButton; 
    Button gethtml; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        getImagebutton=(Button)findViewById(R.id.getImage); 
        gethtml=(Button)findViewById(R.id.gethtml); 
        exitbButton=(Button)findViewById(R.id.exit); 
        imagePatheditText=(EditText)findViewById(R.id.ImagePath); 
        imageView=(ImageView)findViewById(R.id.imageView); 
         
        getImagebutton.setOnClickListener(new OnClickListener() 
        {            
            @Override 
            public void onClick(View v) 
            { 
                try 
                { 
                    byte[] data=testJunit.testGetImage(imagePatheditText.getText().toString()); 
                    Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length); 
                    imageView.setImageBitmap(bm); 
                } catch (Exception e) 
                { 
                    Toast.makeText(MainActivity.this, "获取图片失败", Toast.LENGTH_SHORT); 
                } 
                 
            } 
        }); 
         
        gethtml.setOnClickListener(new OnClickListener() 
        {            
            @Override 
            public void onClick(View v) 
            { 
                Intent intent=new Intent(MainActivity.this, SecondActivity.class); 
                MainActivity.this.startActivity(intent); 
            } 
        }); 
         
        exitbButton.setOnClickListener(new OnClickListener() 
        { 
             
            @Override 
            public void onClick(View v) 
            { 
                android.os.Process.killProcess(android.os.Process.myPid()); 
                 
            } 
        }); 
         
    } 

package com.luku.netImage;

import com.luku.Junit.testJunit;

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

public class MainActivity extends Activity
{
 Button getImagebutton;
 EditText imagePatheditText;
 ImageView imageView;
 Button exitbButton;
 Button gethtml;
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  
  getImagebutton=(Button)findViewById(R.id.getImage);
  gethtml=(Button)findViewById(R.id.gethtml);
  exitbButton=(Button)findViewById(R.id.exit);
  imagePatheditText=(EditText)findViewById(R.id.ImagePath);
  imageView=(ImageView)findViewById(R.id.imageView);
  
  getImagebutton.setOnClickListener(new OnClickListener()
  {   
   @Override
   public void onClick(View

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,