Có thể tải tệp lên qua chế độ xem web không?
Tính năng này có sẵn trong phiên bản 1.3.0 và mới hơn. Đối với các phiên bản cũ của ứng dụng, có thể, nhưng bạn cần phải thêm một số mã phụ để làm cho nó hoạt động. Thêm mã sau vào tệp AndroidManifest.xml của bạn. Đặt nó bên dưới các yếu tố khác cho phép sử dụng:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Sao chép/dán lớp java MediaUtility.java này vào dự án. Đặt nó vào thư mục com/robotemplates/webviewapp/utility.
Mở gói MainFragment.java trong gói phân đoạn. Tìm phương pháp renderView() và thay thế “webView.setWebChromeClient (WebChromeClient mới());” bằng cách làm theo mã:
webView.setWebChromeClient(new WebChromeClient()
{
public void openFileChooser(ValueCallback<Uri> filePathCallback)
{
mFilePathCallback4 = filePathCallback;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER);
}
public void openFileChooser(ValueCallback filePathCallback, String acceptType)
{
mFilePathCallback4 = filePathCallback;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER);
}
public void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType, String capture)
{
mFilePathCallback4 = filePathCallback;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER);
}
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
{
mFilePathCallback5 = filePathCallback;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER);
return true;
}
});
Cuối cùng thêm mã sau ở đâu đó bên trong đối tượng MainFragment (ví dụ bên dưới dòng này “boolean tin mLocal = false;”):
private static final int REQUEST_FILE_PICKER = 1;
private ValueCallback<Uri> mFilePathCallback4;
private ValueCallback<Uri[]> mFilePathCallback5;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if(requestCode==REQUEST_FILE_PICKER)
{
if(mFilePathCallback4!=null)
{
Uri result = intent==null || resultCode!=Activity.RESULT_OK ? null : intent.getData();
if(result!=null)
{
String path = MediaUtility.getPath(getActivity(), result);
Uri uri = Uri.fromFile(new File(path));
mFilePathCallback4.onReceiveValue(uri);
}
else
{
mFilePathCallback4.onReceiveValue(null);
}
}
if(mFilePathCallback5!=null)
{
Uri result = intent==null || resultCode!=Activity.RESULT_OK ? null : intent.getData();
if(result!=null)
{
String path = MediaUtility.getPath(getActivity(), result);
Uri uri = Uri.fromFile(new File(path));
mFilePathCallback5.onReceiveValue(new Uri[]{ uri });
}
else
{
mFilePathCallback5.onReceiveValue(null);
}
}
mFilePathCallback4 = null;
mFilePathCallback5 = null;
}
}
Đừng quên để thêm hàng nhập khẩu cần thiết:‘nhập khẩu android.webkit.ValueCallback;’,‘nhập khẩu com.robotemplates.webviewapp.utility.MediaUtility;’,“nhập khẩu java.io .Tập tin;". Nếu bạn vẫn gặp sự cố, hãy thử thêm quy tắc trong tập lệnh Proguard: https://code.google.com/p/android/issues/detail?id=62220#c120.
Bạn đang làm việc với phiên bản Android nào? Tôi tin rằng trước khi FroYo (2.2) trình duyệt Android tích hợp không hỗ trợ tải lên tệp: http://code.google.com/p/android/issues/detail?id=2519 (tính đến 2.2, nó sẽ hoạt động mặc dù , hoạt động tốt trên N1 của tôi chạy 2.2). –
@Charlie Collins Tôi đang cố gắng này trên 2.1, 2.2, 2.3 và nó sẽ là ok nếu nó làm việc trên bất kỳ trong số này. Nó hoạt động tốt trong trình duyệt ngay cả trên 2.1 (Galaxy S), nhưng tôi cần điều này để làm việc trong một WebView trong một ứng dụng tùy chỉnh. Bạn đang nói nó hoạt động tốt trong ứng dụng của bạn với một WebView trên 2,2? – m0s
Tôi đã không cố gắng cụ thể nó trong một WebView, không có (Tôi chỉ sử dụng các ứng dụng trình duyệt). Trình duyệt Android sử dụng WebViews mặc dù: http://android.git.kernel.org/?p=platform/packages/apps/Browser.git;a=blob_plain;f=src/com/android/browser/BrowserActivity.java; hb = HEAD –