如何在用户选择图库选项时显示所有照片文件夹

时间:2016-12-08 07:27:39

标签: android android-camera

用户在Android中选择图库选项时如何显示所有照片文件夹

1 个答案:

答案 0 :(得分:0)

这里是自定义对话框和选择图像的完整代码。

btnSelect=(Button)findViewById(R.id.btnselect);

btnSelect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //custom dialogBox
            final Dialog dialog=new Dialog(MainActivity.this);
            dialog.setContentView(R.layout.custom_dialog_layout);
            dialog.setTitle("Select from..");

            //set the custom dialog components
            TextView txtmsg=(TextView)dialog.findViewById(R.id.txtmsg);
          Button btnGallaery=(Button)dialog.findViewById(R.id.btngallery);
            Button btnCamara=(Button)dialog.findViewById(R.id.btncamara);

            btnGallaery.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent image=new Intent();
                    image.setType("image/*");
                    image.setAction(Intent.ACTION_GET_CONTENT);
                       startActivityForResult(Intent.createChooser(image,"select file"),SELECT_IMAGE);
                    dialog.dismiss();
                }
            });




            btnCamara.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent cam=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

                    cam.putExtra("Image", fileUri);
                    startActivityForResult(cam,REQUEST_CAMERA);
                    dialog.dismiss();



                }
            });

            dialog.show();
        }
    });

    public Uri getOutputMediaFileUri(int type) {
         return Uri.fromFile(getOutputMediaFile(type));
    }

    /*
    * returning image / video
    */
   private static File getOutputMediaFile(int type) {

    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {

         mediaFile = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".jpg");

    }
    else {
        return null;
    }

    return mediaFile;

}

custom_dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:text="From where to select Image ? "
    android:textStyle="bold"
    android:textSize="20sp"
    android:textColor="@android:color/holo_blue_dark"
    android:layout_gravity="center"
    android:textAlignment="center"
    android:id="@+id/txtmsg"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btngallery"
        android:text="Gallery"
        android:textSize="15sp"
        android:textColor="@android:color/black"
        android:layout_gravity="center"
        android:layout_weight="0.5"
        android:layout_margin="10dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btncamara"
        android:text="camara"
        android:textSize="15sp"
        android:textColor="@android:color/black"
        android:layout_weight="0.5"
        android:layout_margin="10dp"/>

     </LinearLayout>
    </LinearLayout>