Android (请帮助)仅允许从Webview中的照相机拍摄图片
背景:Android (请帮助)仅允许从Webview中的照相机拍摄图片,android,react-native,Android,React Native,背景: 我想拍照仅允许在webview react native中使用照相机 在Web版本中(通过url访问),它运行没有任何问题。但是当我尝试使用webview react native(android移动应用程序版本)调用url时。然后,也会出现从“多媒体资料”中拍照的选项。我想知道该选项不存在(已禁用) 我已采取的步骤: 在MainActivity.java中进行修改。这是我的剧本: 导入com.facebook.react.ReactActivity; 导入android.webkit
导入com.facebook.react.ReactActivity;
导入android.webkit.ValueCallback;
导入android.net.Uri;
导入android.provider.MediaStore;
导入java.io.IOException;
导入java.io.File;
导入android.os.Environment;
导入android.content.Intent;
导入android.util.Log;
导入android.annotation.SuppressLint;
导入java.text.simpleDataFormat;
导入java.util.ArrayList;
导入java.util.Date;
公共类MainActivity扩展了ReactActivity{
/**
*返回从JavaScript注册的主组件的名称。该名称用于计划
*组件的渲染。
*/
私有静态最终字符串标记=MainActivity.class.getSimpleName();
私有字符串mCM;
私人财产;
私人资产管理公司;
专用最终静态整数FCR=1;
showFileChooser()上的公共字符串{
Intent takePictureIntent=新的意图(MediaStore.ACTION\u IMAGE\u CAPTURE);
if(takePictureContent.resolveActivity(MainActivity.this.getPackageManager())!=null){
文件photoFile=null;
试一试{
photoFile=createImageFile();
takePictureContent.putExtra(“光路”,mCM);
}捕获(IOEX异常){
Log.e(标签“图像文件创建失败”,ex);
}
if(photoFile!=null){
mCM=“文件:”+photoFile.getAbsolutePath();
takePictureContent.putExtra(MediaStore.EXTRA_输出,Uri.fromFile(photoFile));
}否则{
takePictureContent=null;
}
}
意图内容SelectionContent=新意图(Intent.ACTION\u GET\u CONTENT);
ContentSelectionContent.addCategory(Intent.CATEGORY\u可打开);
contentSelectionContent.setType(“*/*”);
意图[]意图射线;
if(takePictureContent!=null){
intentArray=newintent[]{takePictureIntent};
}否则{
intentArray=新意图[0];
}
意向选择内容=新意向(MediaStore.ACTION\u IMAGE\u CAPTURE);
选择content.putExtra(Intent.EXTRA\u Intent,content selection内容);
选择content.putExtra(Intent.EXTRA_标题,“图像选择器”);
选择content.putExtra(Intent.EXTRA\u INITIAL\u INTENTS,intentArray);
startActivityForResult(选择内容1);
返回“真”;
}
私有文件createImageFile()引发IOException{
@SuppressLint(“SimpleDataFormat”)字符串时间戳=新的SimpleDataFormat(“yyyyMMdd_HHmmss”)。格式(新日期();
字符串imageFileName=“img_u“+时间戳+”;
文件storageDir=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY\u图片);
返回File.createTempFile(imageFileName,.jpg,.storageDir);
}
@凌驾
受保护的字符串getMainComponentName(){
这个.onShowFileChooser();
返回“sbsMobiles”;
}
}
import com.facebook.react.ReactActivity;
import android.webkit.ValueCallback;
import android.net.Uri;
import android.provider.MediaStore;
import java.io.IOException;
import java.io.File;
import android.os.Environment;
import android.content.Intent;
import android.util.Log;
import android.annotation.SuppressLint;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class MainActivity extends ReactActivity {
/**
* Returns the name of the main component registered from JavaScript. This is used to schedule
* rendering of the component.
*/
private static final String TAG = MainActivity.class.getSimpleName();
private String mCM;
private ValueCallback mUM;
private ValueCallback<Uri[]> mUMA;
private final static int FCR=1;
public String onShowFileChooser(){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null){
File photoFile = null;
try{
photoFile = createImageFile();
takePictureIntent.putExtra("PhotoPath", mCM);
}catch(IOException ex){
Log.e(TAG, "Image file creation failed", ex);
}
if(photoFile != null){
mCM = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
}else{
takePictureIntent = null;
}
}
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("*/*");
Intent[] intentArray;
if(takePictureIntent != null){
intentArray = new Intent[]{takePictureIntent};
}else{
intentArray = new Intent[0];
}
Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, 1);
return "true";
}
private File createImageFile() throws IOException{
@SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "img_"+timeStamp+"_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
return File.createTempFile(imageFileName,".jpg",storageDir);
}
@Override
protected String getMainComponentName() {
this.onShowFileChooser();
return "sbsMobiles";
}
}