2

我已经在谷歌上搜索了很长一段时间......但我仍然找不到我的问题的真正答案。问题是我需要一种方法让我的用户在按下 type="file" 的输入元素后能够选择从画廊或直接从相机上传文件。因此,如果那里有任何好的样品,请告诉我,或者如果您有样品可以让我看一下。

提前致谢!

4

2 回答 2

1
public void attachFileInput() {

    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.addCategory(Intent.CATEGORY_OPENABLE);
    i.setType("image/*");
    ((Activity) mContext).startActivityForResult(
                Intent.createChooser(i, "Image Choser"), 1);
}

This method will be in your JSInterface. Call it as follows:

$(".file").live('click', function() {
    mySJInterface.attachFileInput();
});

Hope this helps.

于 2013-09-02T08:30:03.463 回答
1

我正在创建一个使用带有android webview的输入标签上传文件的程序。顺便说一句,我不是安卓应用程序员。

请看一下这些参考资料:Android webview、openfilechooser 终止http://www.cnblogs.com/sipher/archive/2012/09/05/2672361.html

脚步:

  1. 使用输入文件请求制作菜单以捕获照片 - Android webview,openfilechooser 终止

  2. 但是图片没有发送到服务器并修改了 imageUri 值Environment.getExternalStoragePublicDirectory,如此处所示 - http://www.cnblogs.com/sipher/archive/2012/09/05/2672361.html

  3. 所以我需要在 Manifest 中添加权限 WRITE_EXTERNAL_STORAGE。

    webView.setWebChromeClient(new WebChromeClient(){
        @SuppressWarnings("unused")
        public void openFileChooser(ValueCallback<Uri> uploadMsg){
            this.openFileChooser(uploadMsg, "");
        }
    @SuppressWarnings("unused")
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType, String capture) {
        this.openFileChooser(uploadMsg, "");
    }
    
    @SuppressWarnings("unused")
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType) {
        final List<Intent> cameraIntents = new ArrayList<Intent>();
        final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File externalDataDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        File cameraDataDir = new File(externalDataDir.getAbsolutePath()+File.separator+"browser-photos");
        cameraDataDir.mkdirs();
        String mCameraFilePath = cameraDataDir.getAbsolutePath()+File.separator+System.currentTimeMillis()+".jpg";
        imageUri = Uri.fromFile(new File(mCameraFilePath));
    
    final PackageManager packageManager = getPackageManager();
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for(ResolveInfo res : listCam) {
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        cameraIntents.add(intent);
        }
    
    mUploadMessage = uploadMsg;
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.addCategory(Intent.CATEGORY_OPENABLE);
    i.setType("image/*");               
    Intent chooserIntent = Intent.createChooser(i,"Image Chooser");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
    MainActivity.this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
    }
    });
    webView.getSettings().setDomStorageEnabled(true);
    webView.loadUrl("..............");
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.setWebViewClient(new WebViewClientClass());}
    

现在。我可以上传带有输入标签的相机拍摄的照片。

更新:您可以寻找替代方法来解决相同的问题

于 2013-09-11T07:22:11.927 回答