Android Studio - 从图库中选择图片

时间:2016-02-17 10:59:31

标签: android android-studio

在应用程序上,当我选择右上角的摄像头时,我想要显示一条消息或对话框。选择相机后,我想要显示一条消息,说“拍照”和“拍照”。或者'从图库中选择'

以下是代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!contact.getHidden()) {
        getMenuInflater().inflate(R.menu.conversation, menu);
        if (!Preferences.getBoolean(this, Preferences.PREF_DEV_IMAGE_CAPTURE, false)) {
            MenuItem camera = menu.findItem(R.id.launch_camera);
            camera.setVisible(false);

        }
    }
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.launch_voip_call) {
        Utils.startCall(this, contact);
        return true;
    } else if (item.getItemId() == R.id.launch_camera) {
        Intent cameraIntent = new Intent(ConversationActivity.this, CameraActivity.class);
        cameraIntent.putExtra("EXTRA_CONTACT_JID", contact.getJid());
        startActivity(cameraIntent);
    }
    return false;
}

2 个答案:

答案 0 :(得分:0)

在strings.xml中添加

<array name="image_upload_methods">
    <item>Take photo</item>
    <item>Select from Gallery</item>
</array>

当用户点击launch_camera

else if (item.getItemId() == R.id.launch_camera) {
    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("Upload Demo")
                            .setItems(R.array.image_upload_methods, new OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    if (which == 0){
                                        Intent cameraIntent = new Intent(ConversationActivity.this, CameraActivity.class);
                                        cameraIntent.putExtra("EXTRA_CONTACT_JID", contact.getJid());
                                        startActivity(cameraIntent);
                                    }else {
                                        Intent intent = new Intent();
                                        intent.setType("image/*");
                                        intent.setAction(Intent.ACTION_GET_CONTENT);
                                        startActivityForResult(Intent.createChooser(intent,
                                                "Select Picture"), RESULT_LOAD_IMG);
                                    }
                                }
                            }).show();
}

答案 1 :(得分:0)

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Pick Image from")
   .setCancelable(false)
   .setPositiveButton("Camera", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
           //camera intent
       }
   })
   .setNegativeButton("Gallery", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            //gallery intent
       }
   });
AlertDialog alert = builder.create();
alert.show();