java - 从相机上传照片时出错
问题描述
我是 android studio 和一般开发的移动应用程序的初学者,但我仍在努力学习。所以我设法构建了我的第一个 webview 应用程序来显示页面和图片。我找到了很多有用的资源来帮助我这样做,但我仍然面临上传从手机相机拍摄的照片的问题(不是从图库)直接上传到服务器。
1-按下浏览按钮后,应用程序会提示我是从相机还是从图库中拍照。(Android 版本 9) 2- 当我从图库上传照片时,该应用程序运行良好,但当我使用相机拍照并立即上传时,它不起作用。尽管照片名称返回到字段路径,但它给了我以下错误消息
-1_net::ERR_ACCESS_DENIED
这是我的 onActivityResult 代码
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == this.mUploadMessage) {
return;
}
Uri result = null;
try {
if (resultCode != RESULT_OK) {
result = null;
} else {
// retrieve from the private variable if the intent is null
result = data == null ? mCapturedImageURI : data.getData();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "activity :" + e, Toast.LENGTH_LONG).show();
}
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (requestCode != FILECHOOSER_RESULTCODE || mFilePathCallback == null) {
super.onActivityResult(requestCode, resultCode, data);
return;
}
Uri[] results = null;
if (resultCode == Activity.RESULT_OK) {
if (data == null || data.getData() == null) {
// if there is not data, then we may have taken a photo
if (mCameraPhotoPath != null) {
results = new Uri[]{Uri.parse(mCameraPhotoPath)};
}
} else {
String dataString = data.getDataString();
if (dataString != null) {
results = new Uri[]{Uri.parse(dataString)};
}
}
}
mFilePathCallback.onReceiveValue(results);
mFilePathCallback = null;
} // end of code for Lollipop only
}
--------这里是openFileChooser ----------------
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setLoadsImagesAutomatically(true);
try {
File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "DirectoryNameHere");
if (!imageStorageDir.exists()) {
imageStorageDir.mkdirs();
}
File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
mCapturedImageURI = Uri.fromFile(file); // save to the private variable
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
// captureIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
Intent chooserIntent = Intent.createChooser(i, getString(R.string.image_chooser));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[]{captureIntent});
startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
} catch (Exception e) {
Toast.makeText(getBaseContext(), "Camera Exception:" + e, Toast.LENGTH_LONG).show();
}
}
I really appreciate your help
解决方案
仅仅放入清单还不够,您必须启动请求权限,
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.REQUESTED_PERMISSION) ==PackageManager.PERMISSION_GRANTED)
{
// You can use the API that requires the permission
openFileChooser();
}else{
requestPermissions(this,new String[] {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA},100
);
}
@Override
public void onRequestPermissionsResults(
int requestCode, String[] permissions,int[] grantResults)
{
if (requestCode==100 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
openFileChooser();
}else{
//not grant show toas or dialog
}
}
推荐阅读
- python - Python Bs4:如何根据该行的特定“td”值检索表中的行
- c# - 从使用方法外部变量的不同类调用方法
- javascript - Oracle中的阿拉伯数字方向问题
- python - Django简单历史没有记录用户
- bash - 如何在第一个字符/下划线和重命名后去除文件名
- python-3.x - tkinter.filedialog.asksaveasfilename 保存文件慢
- python - 使单元格适合文本(并使其只读)
- reactjs - Microsoft Graph Toolkit 组件的事件处理程序未在 React 应用程序中调用
- swift - 将数据从 CustomTableView 类发送到其父视图控制器
- docker - Dockerfile 在 Linux 容器上安装 .NET 包?