我想制作像WhatsApp配置文件照片对话屏幕的对话屏幕

时间:2014-05-23 09:14:27

标签: android dialog whatsapp

你好朋友......

我目前正在尝试使用Implicit Intent创建一个Dialog,我希望在其中显示我的对话框,如whatsApp(个人资料照片屏幕),在此屏幕中,whatsApp正在使用名为“删除照片”的额外字段。当我尝试制作相同类型的屏幕对话框时,我无法添加此额外字段(“删除照片”)。我已经完成了所有代码。它在Dialog中的三个选项(图库,照片,相机)工作正常,我无法在onActivityResult()中处理这些。我发送了我的所有源代码,我已经尝试了很多,但我无法找到解决方案。 PLZ .....朋友帮助我解决这个问题。

在这段代码中我只是创建一个名为openFileChooser()的方法,其中我已经编写了用于创建Dialog Screen的所有代码并在onActivityResult()中处理了这些结果

这是我的代码

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 个答案:

答案 0 :(得分:1)

这看起来像是一个问题:

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

除非FILECHOOSER_RESULTCODE和GALLERY_PICTURE是相同的ints,否则语句将永远不会通过。

答案 1 :(得分:0)

可能可以使用请求代码来区分照片的“类型”,即

from collections import OrderedDict

doc = OrderedDict([
    ('cid', document['cid']),
    ('mac', document['mac']),
    ('category', document['category']),
    # ... other keys ...
])

docs_uplink.append(doc)
dumped = json_util.dumps(dict(data=docs_uplink))

您可以在开始新活动之前创建Intent时使用protected static final int MY_FACE_PHOTO = 0; protected static final int MY_CAR_PHOTO = 1; protected static final int MY_HOUSE_PHOTO = 2; ,例如:

Intent.putExtra(String name, String value)

之后,您可以在@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 */) 方法中使用它们:

onActivityResult()