2016-02-17 180 views
2

在應用程序中,當我選擇右上角的相機時,我想要一個消息或對話框出現。一旦選擇相機我希望出現一個消息,說是「拍照」或「從圖庫中選擇」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; 
} 
+0

你面臨什麼問題? – Yashasvi

+0

非常新的android工作室,因爲我只有基本的知識。只是想知道如何在選擇FAB時使對話框出現兩個選項。 – nsamad400

回答

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(); 
} 
+0

謝謝,我會試試這個。 – nsamad400

+0

這段代碼到哪裏去了? – nsamad400

+0

java代碼AlertDialog.Builder應該放置在onOptionsItemSelected中,就像您所示的一樣。 應該在/app/values/string.xml裏面 –

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