从库中的照片,与选定的照片更换背景图片 [英] select photo from library, replace the background image with the selected Photo
本文介绍了从库中的照片,与选定的照片更换背景图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的应用程序工作的用户配置文件的照片部分。
我有我的活动背景图像的按钮。当我按一下按钮,将重定向到画廊,我想选择一个图像。选定的图像将替换按钮的背景。结果
下面是我的布局文件
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
<按钮
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=选择图片
机器人:背景=@绘制/ icon_user
机器人:ID =@ + ID / ChoosePictureButton/>
< / LinearLayout中>
该怎么做?任何想法?
解决方案
要从图库中选择图像包括在按钮的OnClicklisterner以下
意向意图=新的Intent();
intent.setType(图像/ *);
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(意向,SELECT_PICTURE);
块引用>私有静态最终诠释SELECT_PICTURE = 1;
私人字符串selectedImagePath;
@覆盖
公共无效的onActivityResult(INT申请code,INT结果code,意图数据)
{
如果(结果code == RESULT_OK){
如果(要求code == SELECT_PICTURE)
{
乌里selectedImageUri = data.getData();
selectedImagePath =的getPath(selectedImageUri);
尝试{
的FileInputStream fileis =新的FileInputStream(selectedImagePath);
的BufferedInputStream bufferedstream =新的BufferedInputStream(fileis);
字节[] = bMapArray新的字节[bufferedstream.available()];
bufferedstream.read(bMapArray);
位图BMAP = BitmapFactory.de codeByteArray的(bMapArray,0,bMapArray.length);
//在这里,您可以将此/位图图像设置为按钮背景图片 如果(fileis!= NULL)
{
fileis.close();
}
如果(bufferedstream!= NULL)
{
bufferedstream.close();
}
}赶上(FileNotFoundException异常五){
e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
}
}
}
}
公共字符串的getPath(URI URI){
的String [] =投影{MediaStore.Images.Media.DATA};
光标光标= managedQuery(URI,投影,NULL,NULL,NULL);
INT与Column_Index =光标
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
返回cursor.getString(Column_Index中);
}i am working on photo section of user profile in my app. i have a button with a background image on my activity. when i click the button it will redirect to gallery and i would like to select an image. the selected image will replace the background in the button.
below is my layout file<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Choose Picture" android:background="@drawable/icon_user" android:id="@+id/ChoosePictureButton"/> </LinearLayout>
how to do that? any idea?
解决方案To select the image from Gallery include the following in OnClicklisterner of the button
Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); startActivityForResult(intent, SELECT_PICTURE);
private static final int SELECT_PICTURE = 1; private String selectedImagePath; @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { if (requestCode == SELECT_PICTURE) { Uri selectedImageUri = data.getData(); selectedImagePath = getPath(selectedImageUri); try { FileInputStream fileis=new FileInputStream(selectedImagePath); BufferedInputStream bufferedstream=new BufferedInputStream(fileis); byte[] bMapArray= new byte[bufferedstream.available()]; bufferedstream.read(bMapArray); Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length); //Here you can set this /Bitmap image to the button background image if (fileis != null) { fileis.close(); } if (bufferedstream != null) { bufferedstream.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } } public String getPath(Uri uri) { String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = managedQuery(uri, projection, null, null, null); int column_index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); }
这篇关于从库中的照片,与选定的照片更换背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文