2012-05-08 99 views
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> 

如何做到這一點?任何想法?

回答

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); 
    } 
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); 
    } 
} 

如果是使用全給你,然後選擇權。

相關問題