【问题标题】:Android - Take/Select picture and put it in the right imageviewAndroid - 拍摄/选择图片并将其放在正确的图像视图中
【发布时间】:2016-04-20 14:09:08
【问题描述】:

问题:用户最多可以拍摄/选择 3 张照片。我无法弄清楚如何填写这 3 个案例;我不确定如何检索相应的 ImageView ID。

自从我使用以来,我尝试了 putextra Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE),不过貌似不能用putextra方法(我没有检索到多余的)

因此,让我与您分享代码,如果您会以不同的方式继续,请随时告诉我。非常感谢!

所以我在这里捕捉点击事件并将 V.getID 传递给将处理与选择/拍照相关的操作的方法。

@Override
public void onClick(View v) {

    switch(v.getId()){
        case R.id.add_item_give_button:
            checkAddedItem();
            break;
        case R.id.add_item_image_1:
            selectImage(v.getId());
            break;
        case R.id.add_item_image_2:
            selectImage(v.getId());
            break;
        case R.id.add_item_image_3:
            selectImage(v.getId());
            break;
    }
}

调用 selectImage 方法并处理 alertDialog 询问用户是要拍照还是选择一张。我正在尝试在 putExtra 方法中传递 ID,但 startActivityForResult 中没有收到任何内容

public void selectImage(final int imageViewID){

    final CharSequence[] options = {getString(R.string.cameral_select_photo_label), getString(R.string.camera_take_photo_label), getString(R.string.common_cancel_label)};
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(getString(R.string.camera_dialog_title_label));

    builder.setItems(options, new DialogInterface.OnClickListener() {
        @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if(options[which].equals(getString(R.string.camera_take_photo_label))){

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra("ImageViewID", imageViewID);
                startActivityForResult(intent, REQUEST_CAMERA);

            }
            else if(options[which].equals(getString(R.string.cameral_select_photo_label))){

                Utils.verifyStoragePermissions(getActivity());

                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI
                );

                intent.setType("image/*");
                startActivityForResult(
                        Intent.createChooser(intent, getResources().getText(R.string.camera_select_image)),SELECT_FILE);

            }
            else if(options[which].equals(getString(R.string.common_cancel_label))){

                dialog.dismiss();

            }
        }
    });
    builder.show();
}

在 startActivityForResult 中,我没有收到 ImageViewID。所以现在,我只是将图像放在第一个 ImageView 中,因为我无法检索到正确的 ID。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == Activity.RESULT_OK){

        if(requestCode == REQUEST_CAMERA){

            Log.d("Data content", String.valueOf(data));

            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

            File destination = new File(Environment.getExternalStorageDirectory(),
                    System.currentTimeMillis() + ".jpg");

            FileOutputStream fo;

            try {
                destination.createNewFile();
                fo = new FileOutputStream(destination);
                fo.write(bytes.toByteArray());
                fo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            itemPic1.setImageBitmap(thumbnail);

        } else if (requestCode == SELECT_FILE){

            Log.d("imageViewOrigin", String.valueOf(data.getIntExtra("imageViewID", 0)));

            Uri selectedImageUrl = data.getData();
            String[] projection = {MediaStore.MediaColumns.DATA};
            CursorLoader cursorLoader = new CursorLoader(getContext(), selectedImageUrl, projection, null, null, null);
            Cursor cursor = cursorLoader.loadInBackground();
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
            cursor.moveToFirst();

            String selectedImagePath = cursor.getString(column_index);

            Bitmap bm;
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(selectedImagePath, options);
            final int REQUIRED_SIZE = 200;
            int scale = 1;
            while(options.outWidth / scale / 2 >= REQUIRED_SIZE && options.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale += 2;
            options.inSampleSize = scale;
            options.inJustDecodeBounds = false;
            bm = BitmapFactory.decodeFile(selectedImagePath, options);

            itemPic1.setImageBitmap(bm);


        }

    }

}

【问题讨论】:

    标签: android android-intent camera


    【解决方案1】:

    我建议在 ImageViews 上设置标签。按照链接,它是一个类似的问题What is the main purpose of setTag() getTag() methods of View?。如果您需要更多帮助,请告诉我!

    【讨论】:

    • 谢谢你,Feddy,但恐怕我会面临同样的问题。还是谢谢!
    • 对不起,我想我现在明白了。而是在设置单击侦听器时创建单独的 TextView 对象作为公共类对象。例如 TextView firstTextView = (TextView) findViewById(R.id."id here");一旦对象初始化,您就可以从那里向对象添加单独的点击侦听器。只需输入 firstTextView.setOnClick 然后 Android Studio 将自动为您填充其余部分。 (阅读下面的评论)
    • 然后您可以在其中调用该方法并使用该方法传递文本视图。 public void selectImage(final int imageViewID, TextView clickedTextView)。然后在该方法中,您可以简单地将位图设置为传递的文本视图。如果您需要更多帮助,请告诉我!这么晚才回复很抱歉。因为它太长了,所以做了 2 厘米。
    【解决方案2】:

    试试这个方法:

     private void openImageIntent(int IMAGE_TYPE) {
    
        // Determine Uri of camera image to save.
        final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "mycapturedImage" + File.separator);
        root.mkdirs();
        final String fname = getUniqueImageFilename();
        final File sdImageMainDirectory = new File(root, fname);
        outputFileUri = Uri.fromFile(sdImageMainDirectory);
    
        // Camera.
        final List<Intent> cameraIntents = new ArrayList<Intent>();
        final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        final PackageManager packageManager = getPackageManager();
        final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    
        for (ResolveInfo res : listCam) {
            final String packageName = res.activityInfo.packageName;
            final Intent intent = new Intent(captureIntent);
            intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            intent.setPackage(packageName);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            cameraIntents.add(intent);
        }
    
        // Filesystem.
        final Intent galleryIntent = new Intent();
        galleryIntent.setType("image/*");
        galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
    
        // Chooser of filesystem options.
        final Intent chooserIntent = Intent.createChooser(galleryIntent, "Choisir une Source");
    
        // Add the camera options.
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
    
        startActivityForResult(chooserIntent, IMAGE_TYPE);
    }
    

    然后像这样检索每个图像:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_CANCELED) {
            if (resultCode == RESULT_OK) {
                if (requestCode == FIRST_IMAGE_INTENT) {
                    final boolean isCamera;
                    if (data == null) {
                        isCamera = true;
    
                    } else {
                        final String action = data.getAction();
                        if (action == null) {
                            isCamera = false;
                        } else {
                            isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        }
                    }
    
    
                    if (isCamera) {
    
                        selectedCaptureUri = outputFileUri; 
    
                    } else {
    
                        selectedCaptureUri = data == null ? null : data.getData();
    
    
                    }
    
       //Display image here 
    
                } else if (requestCode == SECOND_PICTURE_INTENT) {...}
    

    【讨论】:

    • 谢谢你 bashizip,我现在就试试这个,如果它有效会告诉你:)
    猜你喜欢
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    相关资源
    最近更新 更多