Android的openFileChooser的onCreate()的onActivityResult()之后调用 [英] Android openFileChooser onCreate() called after onActivityResult()

查看:299
本文介绍了Android的openFileChooser的onCreate()的onActivityResult()之后调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个web视图并在其中一个网页有一个上传照片按钮。我发现了一些code来实现文件选择器(Android的,为什么这么难????),如果我挑廊一切工作正常。如果让我选择相机10 1的时候它的工作原理。但大多数的时候我拍摄照片,然后点击保存的时间(这是所有相机的活动),web视图加载时,应用程序启动加载的第一页。看来,的onActivityResult()不叫,但不是它的onCreate()是,这打乱了我的应用程序。你能给我如何我拍照后恢复web视图状态的例子吗? (也许我应该提到,我在的WebView登录)。

这是WebChromeClient类:

 公共类WebViewChromeClient扩展WebChromeClient {
    私人活动活动;
    公共乌里imageUri;    私有静态最终诠释FILECHOOSER_RESULT code = 1;
    私人乌里mCapturedImageURI = NULL;    私人上下文的背景下;    私人MainActivity mainActivity;    公共WebViewChromeClient(上下文的背景下,活动活动,
            MainActivity mainActivity){
        this.activity =活动;
        this.context =背景;
        this.mainActivity = mainActivity;
    }    公共无效openFileChooser(ValueCallback<&乌里GT; uploadMsg,弦乐acceptType){        //更新消息
        ((奥迪)activity.getApplication())setmUploadMessage(uploadMsg)。        如果(uploadMsg == NULL){
            Log.d(上载消息,空);
        }        尝试{
            文件imageStorageDir =新的文件(
                    环境
                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                    AndroidExampleFolder);            如果(!imageStorageDir.exists()){
                //在SD卡创建AndroidExampleFolder
                imageStorageDir.mkdirs();
            }            //创建相机拍摄的图像文件路径和名称
            档案文件=新的文件(imageStorageDir +文件分割符+IMG_
                    +将String.valueOf(System.currentTimeMillis的())+.JPG);            mCapturedImageURI = Uri.fromFile(文件);
            mainActivity.setmCapturedImageURI(mCapturedImageURI);
            Log.d(线,57);
            //摄像头采集图像的意图
            最终意图captureIntent =新意图(
                    android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);            captureIntent.putExtra(MediaStore.EXTRA_OUTPUT,mCapturedImageURI);
            mainActivity.setmCapturedImageURI(mCapturedImageURI);            意图I =新意图(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType(图像/ *);            //创建文件选择意向
            意向chooserIntent = Intent.createChooser(我,图像选择器);            //设置相机意向文件选择
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                    新Parcelable [] {captureIntent});            //在选择的图像调用的活动的onActivityResult方法
            activity.startActivityForResult(chooserIntent,
                    FILECHOOSER_RESULT code);        }赶上(例外五){
            Toast.makeText(背景下,异常:+ E,Toast.LENGTH_LONG).show();
        }    }    // openFileChooser为Android和LT; 3.0
    公共无效openFileChooser(ValueCallback<&乌里GT; uploadMsg){
        openFileChooser(uploadMsg,);
    }    // openFileChooser对于其他Android版本
    公共无效openFileChooser(ValueCallback<&乌里GT; uploadMsg,
            字符串acceptType,字符串捕获){        openFileChooser(uploadMsg,acceptType);
    }    //网页有2 filechoosers并将派
    //控制台消息,告知执行什么操作,机器人wml_siso的init
    //拍照或更新文件    公共布尔onConsoleM​​essage(ConsoleM​​essage厘米){        onConsoleM​​essage(cm.message(),cm.lineNumber(),cm.sourceId());
        返回true;
    }    公共无效onConsoleM​​essage(字符串消息,INT LINENUMBER,弦乐的sourceID){
         Log.d(androidruntime,显示控制台消息,用于调试:
         +消息);    }}

这是的onActivityResult方法:

  @覆盖
保护无效的onActivityResult(INT申请code,INT结果code,
        意向意向){    webView.requestFocus();    如果(要求code == FILECHOOSER_RESULT code){
        Log.d(MainActivity,的onActivityResult);        如果(空==((奥迪)getApplication())。getmUploadMessage()){
            Log.d(文件选择器的结果,58);
            返回;
        }        Log.d(MainActivity,的onActivityResult);
        乌里结果= NULL;        尝试{
            如果(结果code!= RESULT_OK){
                结果= NULL;
            }其他{
                //来自私有变量检索如果意图是空
                结果=意图== NULL? mCapturedImageURI:意图
                        .getData();
            }
        }赶上(例外五){
            Toast.makeText(getApplicationContext(),活动:+ E,
                    Toast.LENGTH_LONG).show();
        }        ((奥迪)getApplication())getmUploadMessage()onReceiveValue(结果)。;
        ((奥迪)getApplication())setmUploadMessage(空)。
    }
    Log.d(MainActivity,的onActivityResult);
}


解决方案

加在清单此行固定的android:configChanges =keyboardHidden |方向|屏幕尺寸

I have a webview and on one of the pages there is an Upload Photo button. I found some code to implement the file chooser (Android, why so hard????) and if I pick the gallery everything works fine. If I choose camera 1 of 10 times it works. But most of the time when I take the picture and click save (this is all in the camera activity) the webview loads the first page loaded when the app was started. It seems that the onActivityResult() is not called but instead of it onCreate() is and this messes up my app. Can you give me an example of how to restore the webView state after I take the picture? (maybe I should mention that I am logged in in the WebView).

This is the WebChromeClient class:

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);

    }

}

And this is the onActivityResult method:

@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");
}

解决方案

Fixed by adding this line in the Manifest android:configChanges="keyboardHidden|orientation|screenSize"

这篇关于Android的openFileChooser的onCreate()的onActivityResult()之后调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆