2016-04-08 6 views
1

私はダイアログを表示する必要があるアプリケーションに取り組んでいます。このダイアログの中で、ユーザーはボタンを押して画像を選択できますギャラリーから写真を撮り、その画像をダイアログのImageView内のImageViewに表示することができます。 私はカメラ/ギャラリーから写真を撮って、それをImageViewイメージとして使っていますが、レイアウトをリフレッシュする必要があるかのように、もう一度ボタンを押すまで表示されません。 これは私のコードです:Android - ギャラリー/カメラから画像を取得するときにダイアログがImageViewを再表示しない

ダイアログを呼び出すには(get_permissionsを();書き込みを取得するためのAndroid Mで使用してストレージ読み取り権限をされ、これは正常に動作します)

protected void createDialog() { 
     final Dialog dialog = new Dialog(WeightActivity.this, android.R.style.Theme_Material_Light_Dialog_Alert); 
     dialog.getWindow(); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialog.setContentView(R.layout.dialog_add_weight); 

     Button okButton = (Button) dialog.findViewById(R.id.okButton); 
     final ImageButton selectImageButton = (ImageButton) dialog.findViewById(R.id.selectImageButton); 
     final ImageView imageView = (ImageView) dialog.findViewById(R.id.imageView); 

     selectImageButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
      get_permissions(); 
      selectImage(); 
      imageView.setImageBitmap(bm); 
      } 
     }); 

     dialog.show(); 
    } 

private void selectImage() { 
    final String[] items = WeightActivity.this.getResources().getStringArray(R.array.photo_options); 
    AlertDialog.Builder builder = new AlertDialog.Builder(WeightActivity.this); 
    builder.setTitle(R.string.weight_dialog_add_photo); 
    builder.setItems(items, new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int item) { 
      if (items[item].equals(items[0])) { 
       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(intent, REQUEST_CAMERA); 
      } else if (items[item].equals(items[1])) { 
       Intent intent = new Intent(
         Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       intent.setType("image/*"); 
       startActivityForResult(
         Intent.createChooser(intent, "Select File"), 
         SELECT_FILE); 
      } 
     } 
    }); 
    builder.show(); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK) { 
     if (requestCode == REQUEST_CAMERA) { 
      bm = (Bitmap) data.getExtras().get("data"); 
     } else if (requestCode == SELECT_FILE) { 
      Uri selectedImageUri = data.getData(); 
      String[] projection = {MediaStore.MediaColumns.DATA}; 
      CursorLoader cursorLoader = new CursorLoader(this, selectedImageUri, projection, null, null, 
        null); 
      Cursor cursor = cursorLoader.loadInBackground(); 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); 
      cursor.moveToFirst(); 
      String selectedImagePath = cursor.getString(column_index); 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inJustDecodeBounds = true; 
      BitmapFactory.decodeFile(selectedImagePath, options); 
      options.inJustDecodeBounds = false; 
      bm = BitmapFactory.decodeFile(selectedImagePath, options); 
     } 
    } 
} 

誰かが私を助けてくださいもらえますか? ? ありがとう!

答えて

0

問題はこれで、

get_permissions(); 
selectImage(); 
imageView.setImageBitmap(bm); 

selectImage()は、活動を開始しますが、それはブロックされません。すぐに戻ります。次に、setImageBitmap()を呼び出します。おそらくヌル値のbmインスタンスフィールドにあります。

imageView.setImageBitmap()コールをonActivityResult()に移動するか、イメージをデコードした後でのみ、トリガーします。

関連する問題