android图片显示代码,Android获取本地图片并显示

import java.io.ByteArrayOutputStream;

import java.io.FileNotFoundException;

import java.io.InputStream;

import android.app.Activity;

import android.content.ContentResolver;

import android.content.Intent;

import android.database.Cursor;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.drawable.BitmapDrawable;

import android.net.Uri;

import android.os.Bundle;

import android.provider.MediaStore;

import android.util.Base64;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.Toast;

public class MainActivity extends Activity {

private ImageView iv;

private OnClickListener imgViewListener;

private Bitmap myBitmap;

private int REQUEST_OK = 1;

private LinearLayout ly_list;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

iv = (ImageView) findViewById(R.id.iv);

ly_list=(LinearLayout) findViewById(R.id.ly_list);

iv.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

Intent intent = new Intent();

//intent = new Intent(Intent.ACTION_GET_CONTENT);

/* 开启Pictures画面Type设定为image */

intent.setType("image/*");

/* 使用Intent.ACTION_GET_CONTENT这个Action */

intent.setAction(Intent.ACTION_GET_CONTENT);

/* 取得相片后返回本画面 */

startActivityForResult(intent, 1);

}

});

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// TODO Auto-generated method stub

if (requestCode == REQUEST_OK) {

Uri selectedImage = data.getData();

try {

Bitmap bitmap = BitmapFactory.decodeStream(this

.getContentResolver().openInputStream(Uri.parse(selectedImage.toString())));

iv.setImageBitmap(bitmap);

Toast.makeText(getApplicationContext(), "上传成功",

Toast.LENGTH_SHORT).show();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

super.onActivityResult(requestCode, resultCode, data);

}

// public static Bitmap getPicFromBytes(byte[] bytes,

// BitmapFactory.Options opts) {

// if (bytes != null)

// if (opts != null)

// return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,

// opts);

// else

// return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

// return null;

// }

//

// public static byte[] readStream(InputStream in) throws Exception {

// byte[] buffer = new byte[1024];

// int len = -1;

// ByteArrayOutputStream outStream = new ByteArrayOutputStream();

//

// while ((len = in.read(buffer)) != -1) {

// outStream.write(buffer, 0, len);

// }

// byte[] data = outStream.toByteArray();

// outStream.close();

// in.close();

// return data;

// }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值