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

此博客给出了Android获取本地图片的代码示例。通过导入相关包,在MainActivity类中实现了点击ImageView后开启图片选择界面,选择图片后将其显示在ImageView上,若上传成功会给出提示,还包含了一些辅助方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

// }

}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com

特别注意:本站所有转载文章言论不代表本站观点!

本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

“FFI” 的全名是 Foreign Function Interface,通常指的是允许以一种语言编写的代码调用另一种语言的代码。而 “Libffi” 库只提供了最底层的、与架构相关的、完整的”FFI”,因此在它之上必须有一层来负责管理两种语言之间参数的格式转换。 高级语言编译器产生代码时都会依据一系列的规则,这些规则十分必要,特别是对独立编译来说。其中之一是“调用约定” (Calling Convention),它包含了编译器关于函数入口处的函数参数、函数返回值的一系列假设。它有时也被称作“ABI”(Application Binary Interface)。调用约定(Calling Conventions)定义了程序中调用函数的方式,它决定了在函数调用的时候数据(比如说参数)在堆栈中的组织方式。 通常来说函数调用要用到的两条基本的指令:”CALL”指令和”RET”指令。”CALL”指令将当前的指令指针(这个指针指向紧接在CALL指令后面的那条指令)压入堆栈,然后执行一条无条件转移指令转移到新的代码地址。”RET”是与”CALL”指令配合使用的指令,在绝大多数函数中它是最后一条指令。”RET”指令弹出返回地址(就是早些时候”CALL”指令压入堆栈的地址)并将其加载到”EIP”寄存器中,然后从这个地址开始继续执行。 图1-1 说明调用约定”stdcall”的调用过程,调用时,最末一个参数最先压入堆栈,而由被调用函数使用RET指令清栈。RET指令带有一个操作数,该操作数指明在EIP跳回主要函数之前需要释放的堆栈空间的字节数。这就是说,stdcall调用约定中RET指令带的操作数往往就意味着函数一共传入几个参数。 图1-1 stdcall调用 标签:LibFFI 分享 window._bd_share_config = { "common": { "bdSnsKey": {}, "bdText": "", "bdMini": "2", "bdMiniList": [], "bdPic": "", "bdStyle": "1", "bdSize": "24" }, "share": {} }; with (document)0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = 'http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion=' ~(-new Date() / 36e5)];\r\n \r\n \r\n \r\n \r\n \u8f6f\u4ef6\u9996\u9875\r\n \u8f6f\u4ef6\u4e0b\u8f7d\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\nwindow.changyan.api.config({\r\nappid: 'cysXjLKDf', conf: 'prod_33c27aefa42004c9b2c12a759c851039' });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值