2012-01-26 5 views
0

私のアプリでは、ユーザーがデバイスから画像を選択できるようなものを使用する必要があります。
これを行う簡単な方法はなんですか?Android - 画像セレクタを作成する方法

私は選択後にその画像を使用することを覚えておいてください。

ありがとうございます。

+0

した後、ユーザーがギャラリーから画像を選択するために起こっていますか? –

答えて

1

利用ティコードをギャラリー

galleryPic.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(); 
      Util.DogDye = false; 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 

      startActivityForResult(
        Intent.createChooser(intent, "Complete action using"), 
        1); 

     } 
    }); 

から画像を選択すると、これは1つの新しいメソッドを追加

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // if (resultCode != RESULT_OK) return; 

    switch (requestCode) { 

    case 1: 
     if (data != null) { 
      selectedImageUri = data.getData(); 
      String selectedImagePath = getPath(path); 
BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inJustDecodeBounds = true; 
Bitmap btemp = BitmapFactory.decodeFile(selectedImagePath, 
         options); 
      /// use btemp Image file 

     } 
     break; 
    } 

} 


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

ギャラリーを開き、ユーザーが画像を選択できるようにする、次の方法を試すことができます。

Intent i = new Intent(Intent.ACTION_PICK, 
    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
startActivityForResult(i, ACTIVITY_SELECT_IMAGE); 

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

switch(requestCode) { 
case REQ_CODE_PICK_IMAGE: 
    if(resultCode == RESULT_OK){ 
     Uri selectedImage = imageReturnedIntent.getData(); 
     String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

     Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
     cursor.moveToFirst(); 

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String filePath = cursor.getString(columnIndex); 
     cursor.close(); 


     Bitmap selectedImage = BitmapFactory.decodeFile(filePath); 
    } 
} 

}

「selectedImageは、」選択した画像であるので、あなたは今、あなたのアプリケーションの残りの部分でそれを使用することができます。

+0

ありがとうございます、ここでうまく動作します。 :) – Raist

+0

問題はありません、聞いて嬉しい=) –

関連する問題