I want to make a Dialog Screen Like WhatsApp Profi

2019-02-25 08:09发布

hello Friends....

I am currently trying to make a Dialog using Implicit Intent where i want to show my Dialog like whatsApp(Profile Photo Screen) and in this screen whatsApp are using extra field named as "Remove Photo". When i try to make same type of screen Dialog then i am unable to add this extra field("Remove Photo"). i have done all code. its working fine for three option in Dialog like(Gallery,Photo,Camera) and i am unable to handle these all in onActivityResult() . I am sending my all source code i have tried much hard , but i am not able to find the solution to do so. plz..... friends help me out from this.

In this code i am simply create a method named as openFileChooser() in which i have write all the code for creating Dialog Screen and handle this outcomes in onActivityResult()

Here Is my code

ProfilePhotoActivity.java

public class ProfilePhotoActivity extends Activity implements OnClickListener{
    ImageButton back, editPhoto, selectAction;
    ImageView imgCamera;
    private static final int FILECHOOSER_RESULTCODE   = 2888;
    protected static final int CAMERA_REQUEST = 0;
    protected static final int GALLERY_PICTURE = 1;
    private Uri mCapturedImageURI = null;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_profilephoto);

        back=(ImageButton)findViewById(R.id.btn_back);
        editPhoto=(ImageButton)findViewById(R.id.ibEditPhoto);
        selectAction=(ImageButton)findViewById(R.id.ibSelectAction);

        imgCamera=(ImageView)findViewById(R.id.imvProfilePhoto);

        editPhoto.setOnClickListener(this);
        selectAction.setOnClickListener(this);
        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
        case R.id.btn_back:
            ProfilePhotoActivity.this.finish();
            break;

        case R.id.ibEditPhoto:
            openFileChooser(null, null);
        //  startDialog();


            break;
        case R.id.ibSelectAction:
            break;
        }
    }

     public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType){  

            try{    

             // Create AndroidExampleFolder at sdcard

             File imageStorageDir = new File(
               Environment.getExternalStoragePublicDirectory(
                 Environment.DIRECTORY_PICTURES)
                 , "AndroidExampleFolder");

             if (!imageStorageDir.exists()) {
              // Create AndroidExampleFolder at sdcard
              imageStorageDir.mkdirs();
             }

             // Create camera captured image file path and name 
             File file = new File(
               imageStorageDir + File.separator + "IMG_"
                 + String.valueOf(System.currentTimeMillis()) 
                 + ".jpg");

             mCapturedImageURI = Uri.fromFile(file); 

             // Camera capture image intent
             final Intent captureIntent = new Intent(
               android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

             captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);

             Intent i = new Intent(Intent.ACTION_PICK); 
            // i.addCategory(Intent.CATEGORY_OPENABLE);
             i.setType("image/*");

             // Create file chooser intent
             Intent chooserIntent = Intent.createChooser(i, "Image Chooser");

             // Set camera intent to file chooser 
             chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS
               , new Parcelable[] { captureIntent });

             // On select image call onActivityResult method of activity
             startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);

            }
            catch(Exception e){
             Toast.makeText(getBaseContext(), "Exception:"+e, 
               Toast.LENGTH_LONG).show();
            }

           }
     protected void onActivityResult(int requestCode, int resultCode,Intent intent) { 

          if(requestCode==FILECHOOSER_RESULTCODE)  
          {  
              if (requestCode == GALLERY_PICTURE)
                    {
                        if (resultCode == RESULT_OK) 
                        {
                            if (intent != null) 
                            {                   
                                // our BitmapDrawable for the thumbnail
                                BitmapDrawable bmpDrawable = null;

                                // try to retrieve the image using the data from the intent
                                Cursor cursor = getContentResolver().query(intent.getData(), null, null, null, null);

                                if (cursor != null) 
                                {
                                    cursor.moveToFirst();

                                    int idx = cursor.getColumnIndex(ImageColumns.DATA);
                                    String fileSrc = cursor.getString(idx);
                                    Bitmap galleryBitmap = BitmapFactory.decodeFile(fileSrc); // load preview image                                                                         
                                    galleryBitmap = Bitmap.createScaledBitmap(galleryBitmap, 200, 200, true);


                                    String filePath = Environment.getExternalStorageDirectory()
                                    .getAbsolutePath()+"/TimeChat/image/"+System.currentTimeMillis()+".jpg";

                                    //imgCamera.setRotation(0);
                                    imgCamera.setImageBitmap(galleryBitmap);
//                                  writeToFile(filePath, galleryBitmap);
                                } 
                                else 
                                {
                                    bmpDrawable = new BitmapDrawable(getResources(), intent.getData().getPath());
                                    imgCamera.setImageDrawable(bmpDrawable);
                                }
                            } 
                            else 
                            {
                                Toast.makeText(getApplicationContext(), "Cancelled", Toast.LENGTH_SHORT).show();
                            }
                        } 
                        else if (resultCode == RESULT_CANCELED) 
                        {
                            Toast.makeText(getApplicationContext(), "Cancelled", Toast.LENGTH_SHORT).show();
                        }
                    } 
                    else if (requestCode == CAMERA_REQUEST) 
                    {
                        if (resultCode == RESULT_OK) 
                        {
                            if (intent.hasExtra("data"))
                            {                   
                                // retrieve the bitmap from the intent
                                Bitmap cameraBitmap = (Bitmap) intent.getExtras().get("data");

                                    String filePath = Environment.getExternalStorageDirectory()
                                .getAbsolutePath()+"/TimeChat/image/"+System.currentTimeMillis()+".jpg";

                                // update the image view with the bitmap
                                imgCamera.setImageBitmap(cameraBitmap);
                            //  writeToFile(filePath, circleBitmap);

                            } 
                            else if (intent.getExtras() == null) {

                                Toast.makeText(getApplicationContext(), "No extras to retrieve!", Toast.LENGTH_SHORT).show();

                                BitmapDrawable thumbnail = new BitmapDrawable(getResources(), intent.getData().getPath());

                                // update the image view with the newly created drawable
                                imgCamera.setImageDrawable(thumbnail);

                            }

                        } 
                        else if (resultCode == RESULT_CANCELED) {
                            Toast.makeText(getApplicationContext(), "Cancelled", Toast.LENGTH_SHORT).show();
                        }
                    }
          }
         }


    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        ProfilePhotoActivity.this.finish();
    }

}

2条回答
ら.Afraid
2楼-- · 2019-02-25 08:58

This looks like it will be a problem:

 if(requestCode==FILECHOOSER_RESULTCODE)  
          {  
              if (requestCode == GALLERY_PICTURE)

unless FILECHOOSER_RESULTCODE and GALLERY_PICTURE are the same ints then statement will never pass.

查看更多
地球回转人心会变
3楼-- · 2019-02-25 09:06

It may be possible to use request code to distinguish between "types" of photos i.e.

protected static final int MY_FACE_PHOTO    = 0;
protected static final int MY_CAR_PHOTO     = 1;
protected static final int MY_HOUSE_PHOTO = 2;

And you may use Intent.putExtra(String name, String value) when you create Intents before starting new activity, like:

@NonNls protected static final String STRING_EXTRA = "string_extra";
@NonNls protected static final String CAMERA           = "camera";
@NonNls protected static final String GALLERY          = "gallery";

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Intent galleryIntent  = new Intent(Intent.ACTION_PICK, 
                               MediaStore.Images.Media.EXTERNAL_CONTENT_URI

cameraIntent.putExtra(STRING_EXTRA, CAMERA);
galleryIntent.putExtra(STRING_EXTRA, GALLERY)

startActivityForResult(takePicture, 
                                MY_FACE_PHOTO /* put here your desired request code */)

After that in onActivityResult() method you may use them:

 protected void onActivityResult (int requestCode, int resultCode, Intent intent) 
 {
   super.onActivityResult(requestCode, resultCode, intent);

    if(intent != null)
    {
       if(resultCode == Activity.RESULT_OK)
       {
          String extraString = intent.getStringExtra(STRING_EXTRA);

           if(extraString.equals(CAMERA))
           {
              // ... i. e.
              switch (requestCode)
              {
                  case MY_FACE_PHOTO:
                  {
                       // ...
                       break;
                  }
                  case MY_CAR_PHOTO:
                  {
                       // ...
                       break;
                  }
                  case MY_HOUSE_PHOTO:
                  {
                      // ...
                      break;
                  }
                  default:
                  {
                      // ...
                      break;
                  }
              } 
           }
           else if(extraString.equals(GALLERY))
           {
              // ...
           }
       }
       else
       {
           // ...
       }
    }
 } 
查看更多
登录 后发表回答