0

我正在我的应用程序中处理用户个人资料的照片部分。我的活动上有一个带有背景图像的按钮。当我单击按钮时,它将重定向到画廊,我想选择一个图像。所选图像将替换按钮中的背景。
下面是我的布局文件

<?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>

怎么做?任何想法?

4

2 回答 2

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);
    }
于 2012-05-08T07:31:05.833 回答
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);
  }
}

如果它对您来说已满,请选择正确。

于 2012-05-08T07:51:36.217 回答