2017-01-07 9 views
10

我想知道如何從DialogPreference中的圖像選取器流程接收結果。Android偏好圖像選取器 - 如何在DialogPreference中接收結果

我想爲DialogPreference到被調用後onActivityResult,因此它可以使用所選圖像的Uri位置顯示爲圖像預覽在其對話框中的用戶打確定前/取消

也許我需要在onActivityResult的末尾設置一些東西,然後調用DialogPreference的生命週期鉤子,但我不確定。

到目前爲止,邏輯是這樣的:

ImagePreference.java

public class ImagePreference extends DialogPreference { 

    View mView; 

    public ImagePreference(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     initWith(context, attrs); 
    } 

    private void initWith(Context context, AttributeSet attrs) { 
     setWidgetLayoutResource(R.layout.pref_image_widget); 
     setDialogLayoutResource(R.layout.pref_image_dialog); 

    } 

    @Override 
    protected View onCreateDialogView() { 
     mView = super.onCreateDialogView(); 

     ImageButton button = (ImageButton) mView.findViewById(R.id.add_image); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       ((SettingsContract.SelectImage)getContext()).fromGallery(); 
      } 
     }); 

     return mView; 
    } 

SettingsActivity.java

public class SettingsActivity extends AppCompatActivity 
     implements SettingsContract.SelectImage { 

    private static final int PICK_IMAGE_REQUEST = 1; 

    // ... 

    @Override 
    public void fromGallery() { 
     Intent intent = new Intent(); 
     // Show only images, no videos or anything else 
     intent.setType("image/*"); 
     intent.setAction(Intent.ACTION_GET_CONTENT); 
     // Always show the chooser (if there are multiple options available) 
     startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data);  
     if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) { 
      Uri uri = data.getData(); 
      // what to do here?? 
    } 
} 
+0

嘿,你給出的獎金?只剩下半個小時了。 – Ajay

+0

@Ajay你的回答不夠詳細,這就是爲什麼我沒有獎勵賞金。我知道你在說什麼,但是對於其他人,尤其是那些更新的Android,如果沒有你更多的解釋,他們將不會從你的答案中獲得任何好處。 –

+0

好的,我明白你的意思。我只是擔心你忘記了這個問題,因爲如果沒有得到回報,聲譽就會消失 – Ajay

回答

2

你不能使用AlertDialogImageView結合。

您可以設置爲在這裏看到一個對話框:https://stackoverflow.com/a/2115770/1985387

new AlertDialog.Builder(context) 
    .setTitle("Title") 
    .setMessage("Here is a preview") //not necessary, you could remove to just show image 
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      // continue with delete 
     } 
    }) 
    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      // do nothing 
     } 
    }) 
    .setIcon(android.R.drawable.ic_dialog_alert) 
    .show(); 

要添加ImageView,你可以從一個URI.show()

.setView(imageView); 

你可以加載ImageView之前添加此此https://stackoverflow.com/a/9080762/1985387

Uri imgUri = Uri.parse(uri); 
imageView.setImageURI(null); 
imageView.setImageURI(imgUri);