android webview 弹出图片选择器上传文件

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient.FileChooserParams;
import android.widget.Toast;


import java.io.File;


/**
 * Handle the file upload. This does not support selecting multiple files yet.
 */
public class UploadHandlerL {
    private final static String IMAGE_MIME_TYPE = "image/*";
    private final static String VIDEO_MIME_TYPE = "video/*";
    private final static String AUDIO_MIME_TYPE = "audio/*";


    private final static String FILE_PROVIDER_AUTHORITY = "me.wangyuwei.thoth";
    //private final static String FILE_PROVIDER_AUTHORITY = "com.android.browser-classic.file";


    /*
     * The Object used to inform the WebView of the file to upload.
     */
    private ValueCallback<Uri[]> mUploadMessage;


    private boolean           mHandled;
    private Controller        mController;
    private FileChooserParams mParams;
    private Uri               mCapturedMedia;


    public UploadHandlerL(Controller controller) {
        mController = controller;
    }


    boolean handled() {
        return mHandled;
    }


    public void onResult(int resultCode, Intent intent) {
        Uri[] uris;
        // As the media capture is always supported, we can't use
        // FileChooserParams.parseResult().
        uris = parseResult(resultCode, intent);
        mUploadMessage.onReceiveValue(uris);
        mHandled = true;
    }


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public void openFileChooser(ValueCallback<Uri[]> callback, FileChooserParams fileChooserParams) {


        if (mUploadMessage != null) {
            // Already a file picker operation in progress.
            return;
        }


        mUploadMessage = callback;
        mParams = fileChooserParams;
        Intent[] captureIntents = createCaptureIntent();
        assert (captureIntents != null && captureIntents.length > 0);
        Intent intent = null;
        // Go to the media capture directly if capture is specified, this is the
        // preferred way.
        if (fileChooserParams.isCaptureEnabled() && captureIntents.length == 1) {
            intent = captureIntents[0];
        } else {
            intent = new Intent(Intent.ACTION_CHOOSER);
            intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, captureIntents);
            intent.putExtra(Intent.EXTRA_INTENT, fileChooserParams.createIntent());
        }
        startActivity(intent);
    }


    private Uri[] parseResult(int resultCode, Intent intent) {
        if (resultCode == Activity.RESULT_CANCELED) {
            return null;
        }
        Uri result =  intent == null || resultCode != Activity.RESULT_OK ? null
                : intent.getData();


        // As we ask the camera to save the result of the user taking
        // a picture, the camera application does not return anything other
        // than RESULT_OK. So we need to check whether the file we expected
        // was written to disk in the in the case that we
        // did not get an intent returned but did get a RESULT_OK. If it was,
        // we assume that this result has came back from the camera.
        if (result == null && resultCode == Activity.RESULT_OK
                && mCapturedMedia != null) {
            result = mCapturedMedia;
        }


        Uri[] uris = null;
        if (result != null) {
            uris = new Uri[1];
            uris[0] = result;
        }
        return uris;
    }


    private void startActivity(Intent intent) {
        try {
            mController.getActivity().startActivityForResult(intent, Controller.FILE_SELECTED);
        } catch (ActivityNotFoundException e) {
            // No installed app was able to handle the intent that
            // we sent, so file upload is effectively disabled.
            Toast.makeText(mController.getActivity(), "上传文件失败",
                    Toast.LENGTH_LONG).show();
        }
    }


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private Intent[] createCaptureIntent() {
        String mimeType = "*/*";
        String[] acceptTypes = mParams.getAcceptTypes();
        if (acceptTypes != null && acceptTypes.length > 0) {
            mimeType = acceptTypes[0];
        }
        Intent[] intents;
        if (mimeType.equals(IMAGE_MIME_TYPE)) {
            intents = new Intent[1];
            intents[0] = createCameraIntent(createTempFileContentUri(".jpg"));
        } else if (mimeType.equals(VIDEO_MIME_TYPE)) {
            intents = new Intent[1];
            intents[0] = createCamcorderIntent();
        } else if (mimeType.equals(AUDIO_MIME_TYPE)) {
            intents = new Intent[1];
            intents[0] = createSoundRecorderIntent();
        } else {
            intents = new Intent[3];
            intents[0] = createCameraIntent(createTempFileContentUri(".jpg"));
            intents[1] = createCamcorderIntent();
            intents[2] = createSoundRecorderIntent();
        }
        return intents;
    }


    private Uri createTempFileContentUri(String suffix) {
        try {
            File mediaPath = new File(mController.getActivity().getFilesDir(), "captured_media");
            if (!mediaPath.exists() && !mediaPath.mkdir()) {
                throw new RuntimeException("Folder cannot be created.");
            }
            File mediaFile = File.createTempFile(
                    String.valueOf(System.currentTimeMillis()), suffix, mediaPath);
            Uri uri = MyFileProvider.getUriForFile(mController.getActivity(),
                    FILE_PROVIDER_AUTHORITY+"."+mController.getActivity().getPackageName(), mediaFile);
            return uri;


        } catch (java.io.IOException e) {
            throw new RuntimeException(e);
        }
    }


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private Intent createCameraIntent(Uri contentUri) {
        if (contentUri == null)
            throw new IllegalArgumentException();
        mCapturedMedia = contentUri;
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
                Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedMedia);
        intent.setClipData(ClipData.newUri(mController.getActivity().getContentResolver(),
                FILE_PROVIDER_AUTHORITY+"."+mController.getActivity().getPackageName(), mCapturedMedia));
        return intent;
    }


    private Intent createCamcorderIntent() {
        return new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    }


    private Intent createSoundRecorderIntent() {
        return new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值