我有一个webview,在其中一个页面上有一个上传照片按钮。我找到了一些代码来实现文件选择器(Android,为什么这么难????)如果我选择了库,一切正常。如果我选择10次相机1,它的工作原理。但是大部分时间我拍照并单击保存(这都是相机活动中),webview会加载应用程序启动时加载的第一页。似乎onActivityResult()
没有被调用,而是onCreate()
而不是它,这会弄乱我的应用程序。你能给我一个如何在我拍照后恢复webView状态的例子吗? (也许我应该提一下,我在WebView中登录)。
这是WebChromeClient类:
public class WebViewChromeClient extends WebChromeClient {
private Activity activity;
public Uri imageUri;
private static final int FILECHOOSER_RESULTCODE = 1;
private Uri mCapturedImageURI = null;
private Context context;
private MainActivity mainActivity;
public WebViewChromeClient(Context context, Activity activity,
MainActivity mainActivity) {
this.activity = activity;
this.context = context;
this.mainActivity = mainActivity;
}
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
// Update message
((Audi) activity.getApplication()).setmUploadMessage(uploadMsg);
if (uploadMsg == null) {
Log.d("UPLOAD MESSAGE", "NULL");
}
try {
File imageStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"AndroidExampleFolder");
if (!imageStorageDir.exists()) {
// Create AndroidExampleFolder at sdcard
imageStorageDir.mkdirs();
}
// Create camera captured image file path and name
File file = new File(imageStorageDir + File.separator + "IMG_"
+ String.valueOf(System.currentTimeMillis()) + ".jpg");
mCapturedImageURI = Uri.fromFile(file);
mainActivity.setmCapturedImageURI(mCapturedImageURI);
Log.d("Line", "57");
// Camera capture image intent
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
mainActivity.setmCapturedImageURI(mCapturedImageURI);
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
// Create file chooser intent
Intent chooserIntent = Intent.createChooser(i, "Image Chooser");
// Set camera intent to file chooser
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
new Parcelable[] { captureIntent });
// On select image call onActivityResult method of activity
activity.startActivityForResult(chooserIntent,
FILECHOOSER_RESULTCODE);
} catch (Exception e) {
Toast.makeText(context, "Exception:" + e, Toast.LENGTH_LONG).show();
}
}
// openFileChooser for Android < 3.0
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
openFileChooser(uploadMsg, "");
}
// openFileChooser for other Android versions
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType, String capture) {
openFileChooser(uploadMsg, acceptType);
}
// The webPage has 2 filechoosers and will send a
// console message informing what action to perform,android wml_siso init
// taking a photo or updating the file
public boolean onConsoleMessage(ConsoleMessage cm) {
onConsoleMessage(cm.message(), cm.lineNumber(), cm.sourceId());
return true;
}
public void onConsoleMessage(String message, int lineNumber, String sourceID) {
Log.d("androidruntime", "Show console messages, Used for debugging: "
+ message);
}
}
这是onActivityResult方法:
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
webView.requestFocus();
if (requestCode == FILECHOOSER_RESULTCODE) {
Log.d("MainActivity", "onActivityResult");
if (null == ((Audi) getApplication()).getmUploadMessage()) {
Log.d("FileChooser Result", "58");
return;
}
Log.d("MainActivity", "onActivityResult");
Uri result = null;
try {
if (resultCode != RESULT_OK) {
result = null;
} else {
// retrieve from the private variable if the intent is null
result = intent == null ? mCapturedImageURI : intent
.getData();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "activity :" + e,
Toast.LENGTH_LONG).show();
}
((Audi) getApplication()).getmUploadMessage().onReceiveValue(result);
((Audi) getApplication()).setmUploadMessage(null);
}
Log.d("MainActivity", "onActivityResult");
}
答案 0 :(得分:1)
运行文件选择器时,系统可能会暂停/停止您的活动。
您需要处理onResume()
并恢复您之前活动的状态。重新启动活动时会调用onResume
方法,因此在启动文件选择器之前,这是重新加载显示的任何WebView的好地方。
答案 1 :(得分:1)
通过在清单android:configChanges="keyboardHidden|orientation|screenSize"
答案 2 :(得分:0)
查看您发布的代码,这是一条危险的行:
webView.requestFocus();
是onActivityResult()
中的第一个。如果正在重新创建 MainActivity ,则可能会导致 NullPointerException 。系统将无缝重新启动您的应用程序,可能会产生报告的行为。
将另一个Log.d()添加到onActivityResult()
的开头,将try / catch放在这个危险的行上,和使用onSaveInstanceState()和onRestoreInstanceState()。
请注意,如果您的 MainActivity 是肖像,那么您将面临额外的麻烦,相机应用会强制横向显示。