/** * Using reflection to persist Uri permission in order to support backward compatibility since * this API is available only from Kitkat onwards. Try to persist Uri access permission for the * client to be able to read the contents from this Uri even after the client is restarted after * device reboot. * * @param file Uri of file to transfer * @throws RcsServiceException */ public static void tryToTakePersistableContentUriPermission(Context context, Uri file) throws RcsServiceException { if (!(ContentResolver.SCHEME_CONTENT.equals(file.getScheme()))) { return; } if (android.os.Build.VERSION.SDK_INT < KITKAT_VERSION_CODE) { return; } try { ContentResolver contentResolver = context.getContentResolver(); Method takePersistableUriPermissionMethod = contentResolver .getClass() .getMethod( TAKE_PERSISTABLE_URI_PERMISSION_METHOD_NAME, TAKE_PERSISTABLE_URI_PERMISSION_PARAM_TYPES); Object[] methodArgs = new Object[] { file, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION }; takePersistableUriPermissionMethod.invoke(contentResolver, methodArgs); } catch (NoSuchMethodException e) { throw new RcsServiceException(e); } catch (IllegalAccessException e) { throw new RcsServiceException(e); } catch (InvocationTargetException e) { throw new RcsServiceException(e); } }
public static int getUriType(Uri uri) { assertNonRelative(uri); String scheme = uri.getScheme(); if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { return URI_TYPE_CONTENT; } if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) { return URI_TYPE_RESOURCE; } if (ContentResolver.SCHEME_FILE.equals(scheme)) { if (uri.getPath().startsWith("/android_asset/")) { return URI_TYPE_ASSET; } return URI_TYPE_FILE; } if ("data".equals(scheme)) { return URI_TYPE_DATA; } if ("http".equals(scheme)) { return URI_TYPE_HTTP; } if ("https".equals(scheme)) { return URI_TYPE_HTTPS; } return URI_TYPE_UNKNOWN; }
public static InputStream openInputStream(Uri uri) { if (uri == null) return null; String scheme = uri.getScheme(); InputStream stream = null; if (scheme == null || ContentResolver.SCHEME_FILE.equals(scheme)) { // from file stream = openFileInputStream(uri.getPath()); } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { // from content stream = openContentInputStream(uri); } else if (Constants.SCHEME_HTTP.equals(scheme) || Constants.SCHEME_HTTPS.equals(scheme)) { // from remote uri stream = openRemoteInputStream(uri); } else if (Constants.SCHEME_RESOURCE.equals(scheme)) { stream = openResourceStream(uri); } else if (Constants.SCHEME_ASSETS.equals(scheme)) { stream = openAssetsStream(uri); } return stream; }
public static String getRealFilePath(Context context, final Uri uri) { if (uri == null) { return null; } final String scheme = uri.getScheme(); String path = null; if (scheme == null) { path = uri.getPath(); } else if (ContentResolver.SCHEME_FILE.equals(scheme)) { path = uri.getPath(); } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { Cursor cursor = context .getContentResolver() .query(uri, new String[] {ImageColumns.DATA}, null, null, null); if (null != cursor) { if (cursor.moveToFirst()) { int index = cursor.getColumnIndex(ImageColumns.DATA); if (index > -1) { path = cursor.getString(index); } } cursor.close(); } } return path; }
public static String getFilePathFromUrl(Context context, Uri uri) { String path = null; String scheme = uri.getScheme(); if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { String projection[] = { ImageColumns.DATA, }; Cursor c = context.getContentResolver().query(uri, projection, null, null, null); if (c != null && c.moveToFirst()) { path = c.getString(0); } if (c != null) c.close(); } else if (ContentResolver.SCHEME_FILE.equals(scheme)) { path = uri.getPath(); } return path; }
public static String getPathFromUri(Context context, Uri uri) { String path = null; String uriScheme = uri.getScheme(); if (ContentResolver.SCHEME_FILE.equals(uriScheme)) { path = uri.getSchemeSpecificPart(); } else if (ContentResolver.SCHEME_CONTENT.equals(uriScheme)) { String[] projection = {MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null); if (cursor != null) { if (cursor.moveToFirst()) { path = cursor.getString(0); } cursor.close(); } } else { path = uri.getPath(); } return path; }
private int openOrDownloadInner(JobContext jc) { String scheme = mUri.getScheme(); if (ContentResolver.SCHEME_CONTENT.equals(scheme) || ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme) || ContentResolver.SCHEME_FILE.equals(scheme)) { try { if (MIME_TYPE_JPEG.equalsIgnoreCase(mContentType)) { InputStream is = mApplication.getContentResolver().openInputStream(mUri); mRotation = Exif.getOrientation(is); Utils.closeSilently(is); } mFileDescriptor = mApplication.getContentResolver().openFileDescriptor(mUri, "r"); if (jc.isCancelled()) return STATE_INIT; return STATE_DOWNLOADED; } catch (FileNotFoundException e) { Log.w(TAG, "fail to open: " + mUri, e); return STATE_ERROR; } } else { try { URL url = new URI(mUri.toString()).toURL(); mCacheEntry = mApplication.getDownloadCache().download(jc, url); if (jc.isCancelled()) return STATE_INIT; if (mCacheEntry == null) { Log.w(TAG, "download failed " + url); return STATE_ERROR; } if (MIME_TYPE_JPEG.equalsIgnoreCase(mContentType)) { InputStream is = new FileInputStream(mCacheEntry.cacheFile); mRotation = Exif.getOrientation(is); Utils.closeSilently(is); } mFileDescriptor = ParcelFileDescriptor.open(mCacheEntry.cacheFile, ParcelFileDescriptor.MODE_READ_ONLY); return STATE_DOWNLOADED; } catch (Throwable t) { Log.w(TAG, "download error", t); return STATE_ERROR; } } }
/** * Fetch the file size from URI * * @param context Context * @param file URI * @return fileSize long * @throws IllegalArgumentException */ public static long getFileSize(Context context, Uri file) throws IllegalArgumentException { Cursor cursor = null; try { cursor = context.getContentResolver().query(file, null, null, null, null); if (ContentResolver.SCHEME_CONTENT.equals(file.getScheme())) { if (cursor != null && cursor.moveToFirst()) { return Long.valueOf(cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.SIZE))); } else { throw new IllegalArgumentException("Error in retrieving file size form the URI"); } } else if (ContentResolver.SCHEME_FILE.equals(file.getScheme())) { return (new File(file.getPath())).length(); } throw new IllegalArgumentException("Unsupported URI scheme"); } finally { if (cursor != null) { cursor.close(); } } }
/** * Fetch the file name from URI * * @param context Context * @param file URI * @return fileName String * @throws IllegalArgumentException */ public static String getFileName(Context context, Uri file) throws IllegalArgumentException { Cursor cursor = null; try { cursor = context.getContentResolver().query(file, null, null, null, null); if (ContentResolver.SCHEME_CONTENT.equals(file.getScheme())) { if (cursor != null && cursor.moveToFirst()) { return cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME)); } else { throw new IllegalArgumentException("Error in retrieving file name from the URI"); } } else if (ContentResolver.SCHEME_FILE.equals(file.getScheme())) { return file.getLastPathSegment(); } throw new IllegalArgumentException("Unsupported URI scheme"); } finally { if (cursor != null) { cursor.close(); } } }
/** 通过Uri获得文件路径* */ public static String getRealFilePath(final Context context, final Uri uri) { if (null == uri) return null; final String scheme = uri.getScheme(); String data = null; if (scheme == null) data = uri.getPath(); else if (ContentResolver.SCHEME_FILE.equals(scheme)) { data = uri.getPath(); } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { Cursor cursor = context .getContentResolver() .query(uri, new String[] {MediaStore.Images.ImageColumns.DATA}, null, null, null); if (null != cursor) { if (cursor.moveToFirst()) { int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); if (index > -1) { data = cursor.getString(index); } } cursor.close(); } } return data; }
/** * Downloads a file form a given URL and saves it to the specified directory. * * @param source URL of the server to receive the file * @param target Full path of the file on the file system */ private void download( final String source, final String target, JSONArray args, CallbackContext callbackContext) throws JSONException { Log.d(LOG_TAG, "download " + source + " to " + target); final boolean trustEveryone = args.optBoolean(2); final String objectId = args.getString(3); final JSONObject headers = args.optJSONObject(4); final URL url; try { url = new URL(source); } catch (MalformedURLException e) { JSONObject error = createFileTransferError(INVALID_URL_ERR, source, target, null, 0); Log.e(LOG_TAG, error.toString(), e); callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, error)); return; } final String sourceProtocol = url.getProtocol(); final boolean useHttps = "https".equals(sourceProtocol); final boolean useResolvers = ContentResolver.SCHEME_FILE.equals(sourceProtocol) || ContentResolver.SCHEME_CONTENT.equals(sourceProtocol) || ContentResolver.SCHEME_ANDROID_RESOURCE.equals(sourceProtocol); // TODO: refactor to also allow resources & content: if (!useResolvers && !Config.isUrlWhiteListed(source)) { Log.w(LOG_TAG, "Source URL is not in white list: '" + source + "'"); JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, null, 401); callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, error)); return; } final RequestContext context = new RequestContext(source, target, callbackContext); synchronized (activeRequests) { activeRequests.put(objectId, context); } cordova .getThreadPool() .execute( new Runnable() { public void run() { if (context.aborted) { return; } URLConnection connection = null; HostnameVerifier oldHostnameVerifier = null; SSLSocketFactory oldSocketFactory = null; File file = null; PluginResult result = null; try { UriResolver sourceResolver = null; UriResolver targetResolver = webView.resolveUri(Uri.parse(target)); TrackingInputStream inputStream = null; file = targetResolver.getLocalFile(); context.targetFile = file; Log.d(LOG_TAG, "Download file:" + url); FileProgressResult progress = new FileProgressResult(); if (useResolvers) { sourceResolver = webView.resolveUri(Uri.parse(source)); long len = sourceResolver.computeLength(); if (len != -1) { progress.setLengthComputable(true); progress.setTotal(len); } inputStream = new SimpleTrackingInputStream(sourceResolver.getInputStream()); } else { // connect to server // Open a HTTP connection to the URL based on protocol if (useHttps) { // Using standard HTTPS connection. Will not allow self signed certificate if (!trustEveryone) { connection = (HttpsURLConnection) httpClient.open(url); } // Use our HTTPS connection that blindly trusts everyone. // This should only be used in debug environments else { // Setup the HTTPS connection class to trust everyone HttpsURLConnection https = (HttpsURLConnection) httpClient.open(url); oldSocketFactory = trustAllHosts(https); // Save the current hostnameVerifier oldHostnameVerifier = https.getHostnameVerifier(); // Setup the connection not to verify hostnames https.setHostnameVerifier(DO_NOT_VERIFY); connection = https; } } // Return a standard HTTP connection else { connection = httpClient.open(url); } if (connection instanceof HttpURLConnection) { ((HttpURLConnection) connection).setRequestMethod("GET"); } // Add cookie support String cookie = CookieManager.getInstance().getCookie(source); if (cookie != null) { connection.setRequestProperty("cookie", cookie); } // This must be explicitly set for gzip progress tracking to work. connection.setRequestProperty("Accept-Encoding", "gzip"); // Handle the other headers if (headers != null) { addHeadersToRequest(connection, headers); } connection.connect(); if (connection.getContentEncoding() == null || connection.getContentEncoding().equalsIgnoreCase("gzip")) { // Only trust content-length header if we understand // the encoding -- identity or gzip progress.setLengthComputable(true); progress.setTotal(connection.getContentLength()); } inputStream = getInputStream(connection); } OutputStream outputStream = null; try { outputStream = targetResolver.getOutputStream(); synchronized (context) { if (context.aborted) { return; } context.currentInputStream = inputStream; } // write bytes to file byte[] buffer = new byte[MAX_BUFFER_SIZE]; int bytesRead = 0; while ((bytesRead = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, bytesRead); // Send a progress event. progress.setLoaded(inputStream.getTotalRawBytesRead()); PluginResult progressResult = new PluginResult(PluginResult.Status.OK, progress.toJSONObject()); progressResult.setKeepCallback(true); context.sendPluginResult(progressResult); } } finally { context.currentInputStream = null; safeClose(inputStream); safeClose(outputStream); } Log.d(LOG_TAG, "Saved file: " + target); // create FileEntry object JSONObject fileEntry = FileUtils.getEntry(file); result = new PluginResult(PluginResult.Status.OK, fileEntry); } catch (FileNotFoundException e) { JSONObject error = createFileTransferError(FILE_NOT_FOUND_ERR, source, target, connection); Log.e(LOG_TAG, error.toString(), e); result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error); } catch (IOException e) { JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, connection); Log.e(LOG_TAG, error.toString(), e); result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error); } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); result = new PluginResult(PluginResult.Status.JSON_EXCEPTION); } catch (Throwable e) { JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, connection); Log.e(LOG_TAG, error.toString(), e); result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error); } finally { synchronized (activeRequests) { activeRequests.remove(objectId); } if (connection != null) { // Revert back to the proper verifier and socket factories if (trustEveryone && useHttps) { HttpsURLConnection https = (HttpsURLConnection) connection; https.setHostnameVerifier(oldHostnameVerifier); https.setSSLSocketFactory(oldSocketFactory); } } if (result == null) { result = new PluginResult( PluginResult.Status.ERROR, createFileTransferError(CONNECTION_ERR, source, target, connection)); } // Remove incomplete download. if (result.getStatus() != PluginResult.Status.OK.ordinal() && file != null) { file.delete(); } context.sendPluginResult(result); } } }); }
private boolean isLocalUri(String scheme) { return ContentResolver.SCHEME_FILE.equals(scheme) || ContentResolver.SCHEME_CONTENT.equals(scheme) || ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme); }
@Override public void run() { try { final int BUF_SIZE = 100 * 1024; buf = new byte[BUF_SIZE]; String scheme = u.getScheme(); if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inTempStorage = buf; InputStream is = null; for (int b = 1; b < 0x80000; b <<= 1) { try { options.inSampleSize = b; is = getContentResolver().openInputStream(u); if (is == null) { Log.e(TAG, "Failed to get the content stream for: " + u); return; } bmp = BitmapFactory.decodeStream(is, null, options); if (bmp != null) return; } catch (Throwable e) { } finally { if (is != null) is.close(); } Log.w(TAG, "Cant decode stream to bitmap. b=" + b); } } else { File f = null; setPriority(Thread.MAX_PRIORITY); boolean local = CA.isLocal(scheme); if (local) { // pre-cache in a file f = new File(u.getPath()); } else { CommanderAdapter ca = null; FileOutputStream fos = null; InputStream is = null; try { ca = CA.CreateAdapterInstance(CA.GetAdapterTypeId(scheme), ctx); if (ca == null) return; Credentials crd = null; try { crd = (Credentials) getIntent().getParcelableExtra(Credentials.KEY); } catch (Exception e) { Log.e(TAG, "on taking credentials from parcel", e); } ca.setCredentials(crd); // output - temporary file File pictvw_f = ctx.getDir("pictvw", Context.MODE_PRIVATE); if (pictvw_f == null) return; f = new File(pictvw_f, "file.tmp"); fos = new FileOutputStream(f); // input - the content from adapter is = ca.getContent(u); if (is == null) return; int n; boolean available_supported = is.available() > 0; while ((n = is.read(buf)) != -1) { // Log.v( "readStreamToBuffer", "Read " + n + " bytes" ); // sendProgress( tot += n ); Thread.sleep(1); fos.write(buf, 0, n); if (available_supported) { for (int i = 0; i < 10; i++) { if (is.available() > 0) break; // Log.v( "readStreamToBuffer", "Waiting the rest " + i ); Thread.sleep(20); } if (is.available() == 0) { // Log.v( "readStreamToBuffer", "No more data!" ); break; } } } } catch (Throwable e) { throw e; } finally { if (ca != null) { if (is != null) ca.closeStream(is); ca.prepareToDestroy(); } if (fos != null) fos.close(); } } if (f != null) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inTempStorage = buf; for (int b = 1; b < 0x80000; b <<= 1) { try { options.inSampleSize = b; bmp = BitmapFactory.decodeFile(f.getAbsolutePath(), options); } catch (Throwable e) { } if (bmp != null) { if (!local) f.delete(); return; } } } } } catch (Throwable e) { Log.e(TAG, u != null ? u.toString() : null, e); msgText = e.getLocalizedMessage(); } finally { sendProgress(-1); } }