php - 如何接收相机数据以上传到android中的php服务器
问题描述
我正在尝试创建 android 文件上传到 php 服务器,一切正常,除了当我选择相机拍摄上传图片时它没有接收到数据,我已经尝试了所有我可以但无法让它写入的东西。我正在使用 android gotev
https://github.com/gotev/android-upload-service进行文件上传。
我认为我的问题是从onActivityResult
接收凸轮数据,但我不知道该怎么做。
private Bitmap bitmap;
private Uri filePath;
private String CAM_MESSAGE;
private ValueCallback<Uri> FILE_MESSAGE;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMAGE_REQUEST_CODE){
if (resultCode == FileUploadActivity.RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
tvPath.setText("Path: ".concat(getPath(filePath)));
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
} else {
Uri[] camfilePath = null;
if (CAM_MESSAGE != null) {
try {
camfilePath = new Uri[]{Uri.parse(CAM_MESSAGE)};
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), camfilePath);
tvPath.setText("Path: ".concat(getPath(camfilePath)));
imageView.setImageBitmap(bitmap);
//Toast.makeText(getApplicationContext(), "CAM MESSAGE:"+CAM_MESSAGE, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}else{
Toast.makeText(getApplicationContext(), "Empty data", Toast.LENGTH_SHORT).show();
}
}
}else{
Toast.makeText(getApplicationContext(), "Not Request Code", Toast.LENGTH_SHORT).show();
}
}
我的文件选择器
private void showFileChooser(){
File photoFile = null;
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType(APP_FTYPE);
Intent[] intentArray;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(FileUploadActivity.this.getPackageManager()) != null) {
try {
photoFile = createImage();
takePictureIntent.putExtra("PhotoPath", CAM_MESSAGE);
} catch (IOException ex) {
Log.e(TAG, "Image file creation failed", ex);
}
if (photoFile != null) {
CAM_MESSAGE = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
} else {
takePictureIntent = null;
}
}
if (takePictureIntent != null) {
intentArray = new Intent[]{takePictureIntent};
} else {
intentArray = new Intent[0];
}
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Choose an action");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, IMAGE_REQUEST_CODE);
}
解决方案
推荐阅读
- search - 如何在 Oracle 中搜索逗号分隔的 DB 列
- javascript - 如何在画布中定位 Path2D SVG 元素?
- ruby-on-rails - 如何在选择框中显示现有数据
- vb.net - 从 DatagridView 创建 Tiff 位图文件
- graphql - AppSync 中的 GraphQL 字段级验证
- c# - 无法在选择列表中保存多个选择 - asp.net core web app razor pages
- c# - C# 8 在返回具有可空约束的可空泛型时发出警告
- ios - 尝试使用 babel-plugin-proposal-class-properties 时使用 .babelrc 的语法加倍
- mysql - 如何优化我的 Google Cloud SQL (MySQL) 数据库以与 API 一起使用
- php - 打印调试堆栈跟踪(在块内)