我在我的应用中处理用户个人资料的照片部分。 我有一个带有背景图像的按钮。当我点击按钮它将重定向到图库,我想选择一个图像。所选图像将替换按钮中的背景 下面是我的布局文件
<?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>
怎么做?任何想法?
答案 0 :(得分:1)
要从图库中选择图像,请在按钮
的OnClicklisterner中包含以下内容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);
}
答案 1 :(得分:0)
使用下面的代码,它可能对您有帮助。
我在图片上使用的方式与您使用按钮的方式相同。
add_image = (ImageView) findViewById(R.id.add_imagev);
add_image.setOnClickListener(this);
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == add_image) {
Intent i = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.setType("image/*");
startActivityForResult(i, 1);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
Uri u = (Uri) data.getData();
// Toast.makeText(getApplicationContext(), ""+u, 1).show();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(u, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
// Toast.makeText(getApplicationContext(), ""+filePath, 1).show();
cursor.close();
add_image.setImageURI(u);
}
}
如果您使用完全,请选择正确。