1

我想捕获图像并将其保存到 SD 卡。现在它工作正常。我的问题是 1)在捕获 OK and Cancel按钮可用后。当我单击确定时,只需将图像保存到 SD 卡中。

2)它没有onActivityResult方法。我onActivityResultActivityGroup课堂上写了我的。

此代码用于当用户单击相机按钮时,它将打开相机并保存

       //Camera
    Button btnCamera =(Button)findViewById(R.id.btnCamera);
    btnCamera.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
             selectedImagePath = Environment.getExternalStorageDirectory()+"/"+retailerCode+"-"+count+".jpg";
             imgName =retailerCode+"-"+count+".jpg";
             count++;
             File file = new File(selectedImagePath);
             Uri outputFileUri = Uri.fromFile(file);
             Intent intent = new Intent (android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
             Bundle b = new Bundle();
             b.putString("Activity", "RetailerOrderSActivity");
             b.putString("RetailerName", seletctedRetailer);
             b.putString("RetailerCode", retailerCode);
             intent.putExtras(b);
             intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
             startActivityForResult(intent, CAMERA_PIC_REQUEST);
             onPhotoTaken();


        }
    });

    protected void onPhotoTaken() {
         _taken = true;
         DBAdapter dbAdapter = DBAdapter.getDBAdapterInstance(CameraMainActivity.this);
         dbAdapter.openDataBase();
         boolean status = dbAdapter.saveImageInfo(retailerCode,strExecutive,strBusinessUnit,strTerritoryCode,imgName,visitNumber);
         if(status) {
          Toast.makeText(SalesActivityGroup.group.getApplicationContext(), "Image has been saved successfully" , Toast.LENGTH_SHORT).show();
         }else {
           Toast.makeText(SalesActivityGroup.group.getApplicationContext(), "Image has not been saved successfully" , Toast.LENGTH_SHORT).show();
         }
        dbAdapter.close();
        lstCaptures = getAllImage(imgDateVal.getText().toString());
        imageViewTable.removeAllViews();
        loadTableLayout();  
     }

这是 ActivityGroup 的代码

      public class SalesActivityGroup extends ActivityGroup {
          @Override
          protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       System.out.println("===REQUEST=====" +requestCode);
         System.out.println("==resultCode==" +resultCode); } }

实际上我需要onPhotoTakenonActivityResult. 根据当前我的代码,如果用户也单击取消,它将信息保存到数据库。图片没拍到。。

这是我的应用图像:

在此处输入图像描述

这是捕获图像后显示的按钮:

在此处输入图像描述 请任何人解决这个问题..

提前致谢

4

2 回答 2

1

检查以下答案

假设我有一个按钮Select & 当用户点击按钮时,相机屏幕将打开。

btn_select.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

          String fileName = new StringBuilder(String.valueOf(System.currentTimeMillis())).append(
                                                        ".jpg").toString();
                  ContentValues values = new ContentValues();
                  values.put(MediaStore.Images.Media.TITLE, fileName);
                  mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                  values);
                  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                  intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
                  startActivityForResult(intent, IShipdocsMobileConstants.CAMERA_ACTION);
                }
    });

用户拍照并单击“保存/确定”按钮(取决于移动设备)后,使用以下代码获取捕获图像的数据。

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode == IShipdocsMobileConstants.CAMERA_ACTION) {
            if (resultCode == RESULT_OK) {
                String[] projection = { MediaStore.Images.Media.DATA };
                Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null);
                int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                String capturedImageFilePath = cursor.getString(column_index_data);
                SelectedFileInfo selectedFileObj = null;
                ArrayList<SelectedFileInfo> cameraArrList = new ArrayList<SelectedFileInfo>();
                File fileObj = new File(capturedImageFilePath);
                String fileSize = String.valueOf(fileObj.length()); //File Size
                String fileName = Utils.getFileName(capturedImageFilePath); //File Name
                }else if (resultCode == RESULT_CANCELED) {
         // handle the condition in which the user didn't save the image
                }
            } else {
                // handle the condition in which the request code was not CAMERA_ACTION , maybe send the user to the home/default screen
            }

        }
    }
于 2012-04-17T12:04:00.753 回答
0

问题是打电话的地方我需要打电话 getParent().startActivityForResult(intent, CAMERA_PIC_REQUEST); 更多细节见这里

于 2012-04-17T12:31:24.337 回答