我想捕捉图像&将其保存到SD卡。现在它的工作正常。
我的问题是1)捕获后OK and Cancel
按钮是可用的。当我单击确定时,它只需要将图像保存到SD卡。
2)它不是onActivityResult
方法。我在onActivityResult
课程中写了ActivityGroup
。
此代码当用户点击相机按钮时,它会打开cameara&保存它
//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); } }
实际上我需要从onPhotoTaken
致电onActivityResult
。根据当前我的代码,如果用户也单击取消,它将信息保存到DB。图像未被捕获..
这是我的应用图片:
这是捕获图像后显示的按钮:
请有人解决这个问题..
提前致谢
答案 0 :(得分:1)
检查以下答案
假设我有一个按钮选择&当用户点击按钮时,相机屏幕将会打开。
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
}
}
}
答案 1 :(得分:0)
问题在于呼叫我需要致电getParent().startActivityForResult(intent, CAMERA_PIC_REQUEST);
更多详情see here