Android中实现从SD卡的Gallery画廊带回图片
来源:互联网 发布:sql注入绕过空格 编辑:程序博客网 时间:2024/06/05 08:23
在App中我们常常遇到通过点击某个按钮,就会跳转到图片库页面,也就是Gallery画廊,那么一方面我们要如何从其中获取我们想要的图片呢?并且带回我们正在浏览的页面。另一方面,我们还有控制图片的大小,如何让带回的图片自适应我们正在浏览页面框的大小?接下来我就为大家揭晓答案
<?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:text="Button" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
先建一个可以点击的按钮和一个放图片的ImageView控件,接下来完成代码部分
ImageDemoActivity.java
import java.io.FileNotFoundException;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.view.Display;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class ImageDemoActivity extends Activity { /** Called when the activity is first created. */private Button btn1;private ImageView imageView1;int hd;int wd; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn1= (Button) findViewById(R.id.button1); imageView1= (ImageView) findViewById(R.id.imageView1); btn1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {//用Intent调用系统图库Intent i=new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);startActivityForResult(i, 0);}}); }@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);if (resultCode==RESULT_OK&&requestCode==0) {Uri imageUri=data.getData();Display display=getWindowManager().getDefaultDisplay();//获取设备尺寸hd=display.getHeight();wd=display.getWidth();BitmapFactory.Options bo=new BitmapFactory.Options();//只加载图片的尺寸bo.inJustDecodeBounds=true;//设置为true,并不会返回一个Bitmap图像try {Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri), null, bo);//通过getContentResolver().openInputStream(imageUri)将URI转换成流int heightRatio=(int) Math.ceil(bo.outHeight/hd);int widthRatio=(int) Math.ceil(bo.outWidth/wd);if (heightRatio>widthRatio) {bo.inSampleSize=heightRatio;}else {bo.inSampleSize=widthRatio;//inSampleSize成员变量,可以把图像的实际宽高和期望的比较得到合理的值}//以上代码只是设置图片尺寸,这个时候我要整个图片加载并显示bo.inJustDecodeBounds=false;bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri), null, bo);//根据第一次得到的合适的缩放比例生成缩络图imageView1.setImageBitmap(bitmap);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}首先我们是通过Intent调用系统图库
Intent i=new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
为了让图片能够符合我们的要求,这里用到了startActivityForResult()方法来带回图片,在完成方法startActivityForResult()之后我们要覆写onActivityResult()方法,此方法能够帮我们传递回调的数据
为了让图片的大小能够符合我们显示器的大小,这里做了一些比较通过获取设备的尺寸BitmapFactory.Options bo=new BitmapFactory.Options();,其他的都在代码中做了详细的注释,就不一一介绍了,对了,要注意bo.inJustDecodeBounds=true;在代码中的变化。
2 0
- Android中实现从SD卡的Gallery画廊带回图片
- Gallery实现图片的画廊展示
- Android 使用ViewPager实现类似gallery画廊的效果(画廊效果之ViewPager显示多个图片)
- 【转】Android 使用ViewPager实现类似gallery画廊的效果(画廊效果之ViewPager显示多个图片)
- RecycleView实现的Gallery画廊效果(图片制作成圆角)
- Android Gallery画廊 图片展示控件
- android中画廊视图Gallery和ImageSwitcher组件的使用
- android 中画廊Gallery的单页滑动
- Android中Gallery(画廊)的创建与使用
- Android-画廊实现 (Gallery , ImageSwitch)
- android中Gallery画廊效果(自动滚动)
- 89 Android Gallery 简单的画廊效果
- android 画廊控件Gallery的使用
- Android 用Gallery和ImageSwicher实现画廊效果,展示图片+完整源代码
- Gallery实现ViewPager的页面切换效果、以及实现图片画廊效果
- 【Android 界面效果30】Android中ImageSwitcher结合Gallery展示SD卡中的资源图片
- android 画廊 gallery
- Android--Gallery,画廊
- 搜索0之1001
- HDU 1024 Max Sum Plus Plus(求m个不相交连续子序列最大和/01背包)
- 你看不见的幕后推手
- String 的基本用法
- C\C++精华总结
- Android中实现从SD卡的Gallery画廊带回图片
- Android:Android面试准备二
- JavaScript中if的表达式自动转换为false的情况
- 关于启用sessionState外部存储,导致IHttpAsyncHandler长连接阻塞线程的问题
- JavaScript解析Json中的对象
- 给Android开发者的RxJava
- Spring事务管理
- 【LeetCode】89. Gray Code解法及注释
- LeetCode-two sum:python解答数组问题