Android初级开发(八)——手机多媒体—弹窗选择照片或直接拍照的实现
最新推荐文章于 2022-09-22 22:01:11 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)
一、效果图
实现的功能主要是主页一个Button,点击后弹出选择框,选择拍照还是从相册选取照片,不管哪种方式,最后将照片返回到主页面上显示出来。
二、实现
1、activity_main.xml布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/btn_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Choose picture" />
</LinearLayout>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_layout" />
</RelativeLayout>
2、创建一个图片的工具类,我这里用的是别人封装好的,如果以后对图片进行操作,可以直接调用
ImageTools.java
/**
* Tools for handler picture
*
*
*/
public final class ImageTools {
/**
* Transfer drawable to bitmap
*
* @param drawable
* @return
*/
public static Bitmap drawableToBitmap(Drawable drawable) {
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
drawable.draw(canvas);
return bitmap;
}
/**
* Bitmap to drawable
*
* @param bitmap
* @return
*/
public static Drawable bitmapToDrawable(Bitmap bitmap) {
return new BitmapDrawable(bitmap);
}
/**
* Input stream to bitmap
*
* @param inputStream
* @return
* @throws Exception
*/
public static Bitmap inputStreamToBitmap(InputStream inputStream)
throws Exception {
return BitmapFactory.decodeStream(inputStream);
}
/**
* Byte transfer to bitmap
*
* @param byteArray
* @return
*/
public static Bitmap byteToBitmap(byte[] byteArray) {
if (byteArray.length != 0) {
return BitmapFactory
.decodeByteArray(byteArray, 0, byteArray.length);
} else {
return null;
}
}
/**
* Byte transfer to drawable
*
* @param byteArray
* @return
*/
public static Drawable byteToDrawable(byte[] byteArray) {
ByteArrayInputStream ins = null;
if (byteArray != null) {
ins = new ByteArrayInputStream(byteArray);
}
return Drawable.createFromStream(ins, null);
}
/**
* Bitmap transfer to bytes
*
* @return
*/
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交
评论
![](https://csdnimg.cn/release/blogv2/dist/pc/img/closeBt.png)
![](https://csdnimg.cn/release/blogv2/dist/pc/img/closeBt.png)
![](https://csdnimg.cn/release/blogv2/dist/pc/img/commentArrowLeftWhite.png)
![](https://csdnimg.cn/release/blogv2/dist/pc/img/iconPark.png)
查看更多评论
![](https://csdnimg.cn/release/blogv2/dist/pc/img/commentArrowDownWhite.png)
添加红包
![](https://csdnimg.cn/release/blogv2/dist/pc/img/guideRedReward02.png)
![](https://csdnimg.cn/release/blogv2/dist/pc/img/guideRedReward03.png)