Android 如何在上传到服务器之前压缩webview中的图像

Android 如何在上传到服务器之前压缩webview中的图像,android,image,webview,upload,compression,Android,Image,Webview,Upload,Compression,我想在webview中上传图像,但我需要先压缩图像,然后再上传到服务器上。 请帮助在我的代码中添加代码,如下所示 这是webview代码,请在我的代码中添加您的代码 我正在学习制作webview应用程序 公共类MainActivity扩展了AppCompatActivity{ private String mCM; private ValueCallback<Uri> mUM; private ValueCallback<Uri[]> mUMA; private fina

我想在webview中上传图像,但我需要先压缩图像,然后再上传到服务器上。

请帮助在我的代码中添加代码,如下所示

这是webview代码,请在我的代码中添加您的代码

我正在学习制作webview应用程序

公共类MainActivity扩展了AppCompatActivity{

private String mCM;
private ValueCallback<Uri> mUM;
private ValueCallback<Uri[]> mUMA;
private final static int FCR = 1;
private final static int FILECHOOSER_RESULTCODE = 1;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
    swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {

            LoadWeb();
        }
    });

    LoadWeb();


    mWebView.setWebChromeClient(new WebChromeClient() {
        public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
            if (mUMA != null) {
                mUMA.onReceiveValue(null);
            }
            mUMA = filePathCallback;
            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("Webview", "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(Intent.ACTION_CHOOSER);
            chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
            chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
            startActivityForResult(chooserIntent, FCR);
            return true;
        }

    });//Image close
}


public void LoadWeb() {

    mWebView = (WebView) findViewById(R.id.webView);
    mWebView.getSettings().setAppCacheEnabled(true);
    mWebView.loadUrl("http://google.com/");
    swipe.setRefreshing(true);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    mWebView.setWebViewClient(new WebViewClient() {

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

            mWebView.loadUrl("file:///android_asset/error.html");

        }


        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            if (Uri.parse(url).getHost().equals("google.com")) {

                return false;

            }


            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

            startActivity(intent);

            return true;
        }

        public void onPageFinished(WebView view, String url) {

            //Hide the SwipeReefreshLayout

            swipe.setRefreshing(false);
        }

    });
}


@Override
public void onBackPressed() {

    if (mWebView.canGoBack()) {
        mWebView.goBack();
    } else {
        finish();
    }



}

class Browser_home extends WebViewClient {

    Browser_home() {
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);

    }

    @Override
    public void onPageFinished(WebView view, String url) {
        setTitle(view.getTitle());

        super.onPageFinished(view, url);

    }
}


public void openFileChooser(ValueCallback<Uri> uploadMsg) {
    this.openFileChooser(uploadMsg, "*/*");
}

public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
    this.openFileChooser(uploadMsg, acceptType, null);
}

public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.addCategory(Intent.CATEGORY_OPENABLE);
    i.setType("*/*");
    MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    if (Build.VERSION.SDK_INT >= 21) {
        Uri[] results = null;
        //Check if response is positive
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == FCR) {
                if (null == mUMA) {
                    return;
                }
                if (intent == null) {
                    //Capture Photo if no image available
                    if (mCM != null) {
                        results = new Uri[]{Uri.parse(mCM)};
                    }
                } else {
                    String dataString = intent.getDataString();
                    if (dataString != null) {
                        results = new Uri[]{Uri.parse(dataString)};
                    }
                }
            }
        }
        mUMA.onReceiveValue(results);
        mUMA = null;
    } else {
        if (requestCode == FCR) {
            if (null == mUM) return;
            Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
            mUM.onReceiveValue(result);
            mUM = null;
        }
    }
}

// Create an image file
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);


}//Image close
私有字符串mCM;
私人财产;
私人资产管理公司;
专用最终静态整数FCR=1;
private final static int FILECHOOSER_RESULTCODE=1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipe=(SwipeRefreshLayout)findviewbyd(R.id.swipe);
swipe.setOnRefreshListener(新的SwipeRefreshLayout.OnRefreshListener(){
@凌驾
公共void onRefresh(){
LoadWeb();
}
});
LoadWeb();
mWebView.setWebChromeClient(新WebChromeClient(){
公共布尔onShowFileChooser(WebView mWebView、ValueCallback filePathCallback、WebChromeClient.FileChooseParams FileChooseParams){
如果(mUMA!=null){
mUMA.onReceiveValue(空);
}
mUMA=filePathCallback;
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(“Webview”,“图像文件创建失败”,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];
}
意图选择器content=新意图(Intent.ACTION\u选择器);
选择content.putExtra(Intent.EXTRA\u Intent,content selection内容);
选择content.putExtra(Intent.EXTRA_标题,“图像选择器”);
选择content.putExtra(Intent.EXTRA\u INITIAL\u INTENTS,intentArray);
startActivityForResult(选择内容,FCR);
返回true;
}
});//图像关闭
}
公共void LoadWeb(){
mWebView=(WebView)findViewById(R.id.WebView);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.loadUrl(“http://google.com/");
swipe.setRefreshing(true);
WebSettings WebSettings=mWebView.getSettings();
setJavaScriptEnabled(true);
setWebViewClient(新的WebViewClient()){
public void onReceivedError(WebView视图、int错误代码、字符串描述、字符串失败URL){
mWebView.loadUrl(“file:///android_asset/error.html");
}
公共布尔值shouldOverrideUrlLoading(WebView视图,字符串url){
if(Uri.parse(url.getHost().equals(“google.com”)){
返回false;
}
Intent Intent=newintent(Intent.ACTION_视图,Uri.parse(url));
星触觉(意向);
返回true;
}
公共void onPageFinished(WebView视图,字符串url){
//隐藏SwipereRefreshLayout
滑动。设置刷新(假);
}
});
}
@凌驾
public void onBackPressed(){
if(mWebView.canGoBack()){
mWebView.goBack();
}否则{
完成();
}
}
类浏览器\u home扩展WebViewClient{
浏览器(u home){
}
@凌驾
public void onPageStarted(WebView视图、字符串url、位图favicon){
super.onPageStarted(视图、url、favicon);
}
@凌驾
公共void onPageFinished(WebView视图,字符串url){
setTitle(view.getTitle());
super.onPageFinished(视图、url);
}
}
public void openFileChooser(ValueCallback uploadMsg){
这个.openFileChooser(uploadMsg,“*/*”);
}
public void openFileChooser(ValueCallback uploadMsg,String acceptType){
this.openFileChooser(uploadMsg,acceptType,null);
}
public void openFileChooser(ValueCallback uploadMsg、字符串接受类型、字符串捕获){
意向i=新意向(意向.行动\u获取\u内容);
i、 addCategory(意图。类别可打开);
i、 集合类型(“*/*”);
MainActivity.this.startActivityForResult(Intent.createChooser(i,“文件浏览器”),FILECHOOSER\u RESULTCODE);
}
@凌驾
ActivityResult上受保护的void(int-requestCode、int-resultCode、Intent-Intent){
super.onActivityResult(请求代码、结果代码、意图);
如果(Build.VERSION.SDK_INT>=21){
Uri[]results=null;
//检查响应是否为阳性
if(resultCode==Activity.RESULT\u确定){
if(requestCode==FCR){
if(null==mUMA){
返回;
}
if(intent==null){
//如果没有可用图像,请拍摄照片
如果(mCM!=null){
结果=新Uri[]{Uri.parse(mCM)};
}
}否则{
字符串dataString=intent.getDataString();
if(数据字符串!=null){
结果=新Uri[]{Uri.parse(dataString)};
}
}
}
}
mUMA.onReceiveValue(结果);
亩