AndroidStudio 2021 SDK 30
我有一个可以使用GPS、共享等功能的网络视图,可以上传文件、询问相机或画廊。
当用户选择图库中的现有文件时,我的代码可以工作,但是当从相机中拍摄时,什么也不会发生
它还请求用户的常规权限
这是AndroidManifest。xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.CAMERA2" />
<uses-permission android:name="android.webkit.PermissionRequest" />
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="com.android.vending.CHECK_LICENSE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.VIDEO_CAPTURE" />
<uses-permission android:name="android.permission.AUDIO_CAPTURE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera2" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature android:name="android.hardware.location.network" />
<uses-feature android:name="android.hardware.audio.low_latency" />
<uses-feature android:name="android.hardware.audio.pro" />
<uses-feature android:name="android.hardware.microphone" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.front" />
<application
android:allowBackup="true"
android:fullBackupContent="true"
android:hardwareAccelerated="true"
android:requestLegacyExternalStorage="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:grantUriPermissions="true"
android:theme="@style/SplashTheme">
<activity android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</application>
</manifest>
主要活动
@SuppressLint({"SetJavaScriptEnabled", "WrongViewCast"})
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
private ValueCallback<Uri[]> mFilePathCallback;
private String mCameraPhotoPath;
private String mCameravideoPath;
private static final int INPUT_FILE_REQUEST_CODE = 1;
// FOR FILE UPLOAD
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) {
super.onActivityResult(requestCode, resultCode, data);
return;
}
Uri[] results = null;
// Check that the response is a good one
if (resultCode == Activity.RESULT_OK) {
if (data == null || data.getDataString() == 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;
}
@Override
protected void onStart() {
super.onStart();
String[] permissions = {
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.MODIFY_AUDIO_SETTINGS,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
requestPermissions(permissions, 0);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
String appNAME = getString(R.string.app_name);
String stubdomain = getString(R.string.stubdomain);
mWebView = new WebView(this);
WebSettings webSettings = mWebView.getSettings();
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setDomStorageEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setGeolocationEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setSupportZoom(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
webSettings.setAllowFileAccess(true);
webSettings.setAllowContentAccess(true);
webSettings.setDatabaseEnabled(true);
webSettings.setMediaPlaybackRequiresUserGesture(false);
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
// FOR FILE UPLOAD
mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
// WEBCHROME
mWebView.setWebChromeClient(new AppTheWayChromeExtended() {
// FILE UPLOAD
private File createImageFile() throws IOException {
int timeStamp = (new Random().nextInt((1000000 - 1) + 1) + 1);
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getCacheDir();
return File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
}
private File createVideoFile() throws IOException {
int timeStamp = (new Random().nextInt((1000000 - 1) + 1) + 1);
String videoFileName = "VID_" + timeStamp + "_";
File storageDir = getCacheDir();
return File.createTempFile(
videoFileName,
".mp4",
storageDir
);
}
@SuppressLint("QueryPermissionsNeeded")
public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> filePath, WebChromeClient.FileChooserParams fileChooserParams) {
// Double check that we don't have any existing callbacks
if (mFilePathCallback != null) {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = filePath;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
} catch (IOException ex) {
// Error occurred while creating the File
// Log.e(TAG, "Unable to create Image File", ex);
}
// Continue only if the File was successfully created
if (photoFile != null) {
mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
} else {
takePictureIntent = null;
}
}
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the video should go
File videoFile = null;
try {
videoFile = createVideoFile();
takeVideoIntent.putExtra("VideoPath", mCameravideoPath);
} catch (IOException ex) {
// Error occurred while creating the File
// Log.e(TAG, "Unable to create Image File", ex);
}
// Continue only if the File was successfully created
if (videoFile != null) {
mCameravideoPath = "file:" + videoFile.getAbsolutePath();
takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(videoFile));
} else {
takeVideoIntent = null;
}
}
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("image/*, video/*");
contentSelectionIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
contentSelectionIntent.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{"image/*", "video/*"});
contentSelectionIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/* video/*");
Intent[] intentArray;
if (takePictureIntent != null && takeVideoIntent != null) {
intentArray = new Intent[]{takePictureIntent, takeVideoIntent};
} else if (takePictureIntent != null) {
intentArray = new Intent[]{takePictureIntent};
} else if (takeVideoIntent != null) {
intentArray = new Intent[]{takeVideoIntent};
} else {
intentArray = new Intent[0];
}
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Upload");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE);
return true;
}
});
mWebView.loadUrl("https://filebin.net/");
this.setContentView(mWebView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater myMenuInflater = getMenuInflater();
myMenuInflater.inflate(R.menu.super_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@SuppressLint("NonConstantResourceId")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.myMenuOne:
onBackPressed();
break;
case R.id.myMenuTwo:
GoForward();
break;
}
return true;
}
private void GoForward() {
if (mWebView.canGoForward()) {
mWebView.goForward();
} else {
Toast.makeText(this, "Can't go further!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
}
}
@Override
public boolean onKeyDown(final int keyCode, final KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
在网上阅读时,我发现了一些关于文件路径的东西。xml,但我尝试了不同的价值观,但没有成功
<paths>
<external-files-path name="external_files" path="." />
<external-files-path name="extfiles" path="." />
<external-files-path name="external" path="." />
<external-files-path name="my_images" path="." />
<external-files-path name="my_videos" path="." />
<external-media-path name="external_files" path="." />
<external-media-path name="extfiles" path="." />
<external-media-path name="my_images" path="." />
<external-media-path name="my_videos" path="." />
<external-path name="external_files" path="."/>
<external-path name="extfiles" path="."/>
<external-path name="external" path="."/>
<external-path name="my_images" path="."/>
<external-path name="my_videos" path="."/>
<files-path name="external_files" path="."/>
<files-path name="extfiles" path="."/>
<files-path name="external" path="."/>
<files-path name="my_images" path="."/>
<files-path name="my_videos" path="."/>
<files-path name="files" path="."/>
</paths>
...........................
编辑:2021-03-07
我添加了日志。我喜欢这样
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
Log.e("imgFileU", "247 "+mCameraPhotoPath);
} catch (IOException ex) {
// Error occurred while creating the File
Log.e("imgFileU", "250 Unable to create Image File", ex);
}
// Continue only if the File was successfully created
if (photoFile != null) {
mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
Log.e("imgFileU", "257 "+mCameraPhotoPath);
} else {
takePictureIntent = null;
}
}
然后我进了日志
2021-03-07 16:18:58.556 7705-7705/com.... E/imgFileU: 247 null
2021-03-07 16:18:58.556 7705-7705/com.... E/imgFileU: 257 file:/data/user/0/com...../cache/JPEG_809310_3070323002224129302.jpg
基本思想是使用意图,而不是实现具体的细节。一个按钮[或任何真正可以启动这个过程的东西]。一个容易理解的例子是地图,如果你有一个地理代码或地址,你就启动一个活动来获得结果,然后让设备根据意图打开“任何东西”。
从代码示例中不清楚您是否正在使用intents上载快照。例如:
这在按钮的点击处理程序中应该会打开相机。[未经测试]
if (options[item].equals("Take Photo"))
{
Intent intent = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new
File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);
}
我正在尝试使用webview上传图像。一切正常,如预期的那样,但只有图像没有上传,当照片从相机中点击时。请帮我找到这个。我得到了一些答案,但没有帮助或没有回答,就像这个没有回答一样,这对我没有帮助,这是直接打开画廊,不适用于相机,这个没有帮助我 这是我的活动代码
我有一个问题与我的代码,我必须上传图像到Firebase存储,我需要的图像来自画廊和相机,从画廊的图像是好的,但来自相机的图像给问题,图像加载在ImageView和被发送到数据库是黑色的。有人知道如何解决这个问题吗,或者你知道任何其他加载图像的方法吗? 来自画廊 从相机
我正在开发一个简单的博客应用程序,允许用户将照片从手机图库和描述上传到Firebase服务器。我正在尝试修改我的当前项目,以允许用户从照相机捕获照片并将其上载到firebase服务器。 目前,我能够将我捕获的图像显示到Image按钮中,但是我无法将我的图像发布到Firebase服务器(提交发布按钮不会对我的点击功能做出反应)。 我怀疑我的startPosting()函数中有错误,或者我没有正确编码
我正在尝试用相机拍照并上传到Firebase。我使用AlertDialog询问用户是否想要使用相机或从图库中选择图像。我可以用相机拍照,但是当我试图上传图像时,它说找不到图像。 以下是我的图像选择方法: 下面是我上传图片的方法: 提前感谢任何帮助的朋友。
我在Android webview上工作。我在从中的动作上传图像时遇到了问题。在这里,相机打开,我点击图像,但然后什么也没发生,图像无法上传。但它可以很好地与文档或图库。有很多解决方案和这个。但我无法解决我的问题。可以做些什么来解决这个问题? 在我的menifest文件中,我包括: 在我的活动中,我包括: 对于我的WebChromeClient,我包括: 这里可以做些什么,以便图像通过相机动作上传
我正在尝试使用GStreamer将RTMP/RTSP流连接到v4l2loopback虚拟设备。 工程1-RTMP至AutoVideoSink,sudo gst-launch-1.0 rtspsrc位置=rtsp://192.168.xxx.xxx/live/av0 ! decodebin!自动视频接收器sudo gst-launch-1.0 rtmpsrc位置=rtmp://192.168.xxx