2016-03-01 188 views
0

在我的应用程序中,我添加了代码以允许用户从图库中选择图像或使用相机拍摄照片。在右上角选择摄像机图标时出现这种情况,并出现一个对话框,其中包含这两个选项。我只是想知道我将如何添加一个图标,将用作按钮。因此,例如,当对话框出现时,我想要一个android画廊图标的图像,当您选择将您带入画廊。还希望使用不同字体颜色的文字“图库”。Android Studio - 设置图标并更改字体颜色

下面是用于创建选项

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) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Pick Image from") 
       .setPositiveButton("Camera", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         //camera intent 
         Intent cameraIntent = new Intent(ConversationActivity.this, CameraActivity.class); 
         cameraIntent.putExtra("EXTRA_CONTACT_JID", contact.getJid()); 
         startActivity(cameraIntent); 
        } 
       }) 
       .setNegativeButton("Gallery", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         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); 
        } 
       }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 
    return false; 
} 
+0

你想在画廊屏幕上显示你自己的图库和字体吗? –

+0

是的,我想显示自己的图标,目前为了进入画廊...您选择当前只是文本的'图库',在弹出选项框中选择摄像机图标时。所以,而不是文字,我想我自己的画廊图标出现。谢谢 –

+0

使用onShow事件:http://stackoverflow.com/a/20830973/1129995 – Zaki

回答

0

解决方案代码是创建活动,将列出从您的画廊所有图像(因为它是你自己的活动,你可以有任何图标或字体)。除此之外,你水湿更改默认相册应用

+0

这是实现这个目标的唯一可能吗?谢谢 –

+0

是的,这是唯一达到此目的但我会建议你选择一个已编写的Github库来显示图库(只需根据您的字体和其他要求进行更改),而不是自己写整个东西 –

+0

林不知道什么你的意思是,我对android studio和编程很陌生。对不起 –

0

下面是部分代码从图库中选择图片或采取与相机的帮助下,代码...

你可以看到完整的在https://gist.github.com/Mariovc/f06e70ebe8ca52fbbbe2

public static Intent getPickImageIntent(Context context){ 
    Intent chooserIntent = null; 
    List<Intent> intentList = new ArrayList<>(); 
    Intent pickIntent = new Intent(Intent.ACTION_PICK, 
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    takePhotoIntent.putExtra("return-data", true); 
    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(context))); 
    intentList = addIntentsToList(context, intentList, pickIntent); 
    intentList = addIntentsToList(context, intentList, takePhotoIntent); 
    if (intentList.size() > 0) { 
     chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1), 
       context.getString(R.string.pick_image_intent_text)); 
     chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{})); 
    } 
    return chooserIntent; 
} 
private static List<Intent> addIntentsToList(Context context, List<Intent> list, Intent intent) { 
    List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(intent, 0); 
    for (ResolveInfo resolveInfo : resInfo) { 
     String packageName = resolveInfo.activityInfo.packageName; 
     Intent targetedIntent = new Intent(intent); 
     targetedIntent.setPackage(packageName); 
     list.add(targetedIntent); 
    } 
    return list; 
} 
0

AlertDialog alert = builder.create();

dialog.setOnShowListener(new OnShowListener() { 

     @Override 
     public void onShow(DialogInterface dialogInterface) { 
      Button button = dialog.getButton(AlertDialog.BUTTON_NEGATIVE); 

      button.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.your_gallery_resource, 


      Drawable drawable = getActivity().getResources().getDrawable(
        android.R.drawable.your_gallery_resource); 

      // set the bounds to place the drawable a bit right 
      drawable.setBounds((int) (drawable.getIntrinsicWidth() * 0.5), 
        0, (int) (drawable.getIntrinsicWidth() * 1.5), 
        drawable.getIntrinsicHeight()); 
      button.setCompoundDrawables(drawable, null, null, null); 

      // could modify the placement more here if desired 
      // button.setCompoundDrawablePadding(); 
     } 
    }); 

注意下面给出的行地址:上面的代码只对库按钮,做一个类似的方式进行相机按钮。