要实现Android从相机拍摄后上传多张图片,可以按照以下步骤进行操作:
public class CameraActivity extends AppCompatActivity {
private static final int REQUEST_IMAGE_CAPTURE = 1;
private List imageUris = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
Button takePhotoButton = findViewById(R.id.btn_take_photo);
takePhotoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dispatchTakePictureIntent();
}
});
Button uploadButton = findViewById(R.id.btn_upload);
uploadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadImages();
}
});
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
}
if (photoFile != null) {
Uri photoUri = FileProvider.getUriForFile(this, "com.example.android.fileprovider", photoFile);
imageUris.add(photoUri);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
return image;
}
private void uploadImages() {
// 在这里实现上传多张图片的逻辑
// 可以遍历imageUris,并将每张图片上传到服务器
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
// 拍照成功后,可以在这里处理拍照后的操作,例如显示预览图等
}
}
}
...
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CAMERA = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button cameraButton = findViewById(R.id.btn_camera);
cameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MainActivity.this, CameraActivity.class);
startActivityForResult(cameraIntent, REQUEST_CAMERA);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {
// 拍照成功后的处理逻辑
}
}
}
以上就是一个简单的实现Android从相机拍摄后上传多张图片的示例代码。你可以根据自己的需求进行修改和扩展。