android 42 获取图片


android 42 获取图片

android 42 获取图片

资源中获取图片:可以从工程assets文件夹、res/drawble文件夹、sd卡、服务端下载图片。

 页面:

android 42 获取图片
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnDecodeFile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="从文件解析图片" />

    <Button
        android:id="@+id/btnDecodeStream"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="丛输入流解析" />

    <Button
        android:id="@+id/btnDecodeResource"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="从项目res/drawble中解析" />

    <Button
        android:id="@+id/btnDecodeByteArray"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="从字节数组解析" />
    <ImageView                             显示解析的图片
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>
</LinearLayout>
android 42 获取图片

 java

android 42 获取图片
package com.sxt.day06_08;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class MainActivity extends Activity {
    ImageView mImageView;
    static final String FILE_NAME="sxt_logo.png";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setListener();
    }

    private void setListener() {
        setResetClickListener();
        setDecodeFileClickListener();
        setDecodeResourceClickListener();
        setDecodeStreamClickListener();
        setDecodeByteArrayClickListener();
    }

    private void setDecodeByteArrayClickListener() {
        findViewById(R.id.btnDecodeByteArray).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(){
                    public void run() {
                        HttpGet get=new HttpGet("http://10.0.2.2/"+FILE_NAME);//服务端资源文件路径
                        HttpClient client=new DefaultHttpClient();
                        try {
                            HttpResponse response = client.execute(get);
                            HttpEntity entity = response.getEntity();//
                            byte[] data = EntityUtils.toByteArray(entity);
                            final Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                            //工作线程不能修改UI,可以用Handler做,runOnUiThread方法会把里面的代码发送给主线程,修改UI。
                            //Runnable可以被多个线程共享,工作线程可以把该Runnable对象交给主线程由主线程执行。
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mImageView.setImageBitmap(bitmap);
                                }
                            });
                        } catch (ClientProtocolException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }finally{
                            if(client!=null){
                                client.getConnectionManager().shutdown();//关闭
                            }
                        }
                    };
                }.start();
            }
        });
    }

    
    private void setDecodeStreamClickListener() {
        findViewById(R.id.btnDecodeStream).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                File file=new File(dir, FILE_NAME);//sd卡图片路径
                FileInputStream in=null;
                try {
                    in=new FileInputStream(file);
                    Bitmap bitmap = BitmapFactory.decodeStream(in);
                    mImageView.setImageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }finally{
                    if(in!=null){
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
    }

    private void setDecodeResourceClickListener() {
        findViewById(R.id.btnDecodeResource).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mImageView.setImageResource(R.drawable.sxt_logo);
            }
        });
    }

    private void setResetClickListener() {//点击图片还原
        findViewById(R.id.iv).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mImageView.setImageResource(R.drawable.ic_launcher);
            }
        });
    }

    //获取sd卡的图片文件
    private void setDecodeFileClickListener() {
        findViewById(R.id.btnDecodeFile).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);//sd卡的图片的路径
                File file=new File(dir, FILE_NAME);//获取sd卡的图片文件
                Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                mImageView.setImageBitmap(bitmap);
            }
        });
    }

    private void initView() {
        mImageView=(ImageView) findViewById(R.id.iv);
    }
}
android 42 获取图片

 工程描述文件添加:

    <uses-permission android:name="android.permission.INTERNET"/>     申请网络权限
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>   申请读sd卡权限

 


本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/4889921.html,如需转载请自行联系原作者


上一篇:android机型适配问题


下一篇:快来pick你心目中的影帝小哥哥,一起迎接2019吧!