检查imageview中的图片是否是从相机或图库中捕获的

时间:2014-07-15 13:00:05

标签: android image camera

我可以从图库或相机中捕获图像,并使用以下代码将其放入imageview中:

1。从图库或相机中捕获图像:

private void captureImageInitialization() {

    final String[] items = new String[] { "Take from camera",
            "Select from gallery" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.select_dialog_item, items);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Select Image");
    builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) { // pick from
                                                                // camera
            if (item == 0) {
                Intent intent = new Intent(
                        "android.media.action.IMAGE_CAPTURE");
                startActivityForResult(intent, PICK_FROM_CAMERA);

            } else {
                // pick from file
                /**
                 * To select an image from existing files, use
                 * Intent.createChooser to open image chooser. Android will
                 * automatically display a list of supported applications,
                 * such as image gallery or file manager.
                 */
                Intent intent = new Intent();

                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);

                startActivityForResult(Intent.createChooser(intent,
                        "Complete action using"), PICK_FROM_FILE);
            }
        }
    });

    dialog = builder.create();
}

2。在imageview中显示:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK)
            return;

        switch (requestCode) {
        case PICK_FROM_CAMERA:

            mImageCaptureUri = data.getData();
            imagepath = getPath(mImageCaptureUri);
            bitmap = BitmapFactory.decodeFile(imagepath);

            mImageView.setImageBitmap(bitmap);
            // Log.d("camera ---- > ", "" + data.getExtras().get("data"));
            new ProcessUploadAvatar2().execute();

            break;

        case PICK_FROM_FILE:
            /**
             * After selecting image from files, save the selected path
             */
            mImageCaptureUri = data.getData();
            imagepath = getPath(mImageCaptureUri);
            bitmap2 = BitmapFactory.decodeFile(imagepath);
            mImageView.setImageBitmap(bitmap2);

            break;

        }
    }

现在我的问题是如何检查imageview中的图像是否已被摄像机或图库采用超出onActivityResult()方法? 我尝试实现类似这样:

public void NetAsync() {

        if (mImageView.getDrawable() != null) {

            if (mImageView = bitmap) {
                new NetCheck().execute();
            } else if (mImageView = bitmap2) {
                new ProcessUploadAvatar2().execute();
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

您可以通过单一检查onActivityResult()中的请求代码轻松获得此信息...我指的是您在调用相机/图库意图时发送的请求代码。