private void openFile(DocumentFile fileDocument) { Intent intent = new Intent(Intent.ACTION_VIEW); if (Build.VERSION.SDK_INT >= 24) { //Nougat and later require "content://" uris instead of "file://" uris File file = new File(fileDocument.getUri().getPath()); Uri contentUri = FileProvider.getUriForFile(device.getContext(), "org.kde.kdeconnect_tp.fileprovider", file); intent.setDataAndType(contentUri, fileDocument.getType()); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); } else { intent.setDataAndType(fileDocument.getUri(), fileDocument.getType()); } device.getContext().startActivity(intent); } }
/** * コンストラクタ * @param context * @param callback * @param output * @throws IOException */ public AbstractMediaAVRecorder(@NonNull final Context context, @Nullable final RecorderCallback callback, @NonNull final DocumentFile output) throws IOException { super(callback); mWeakContext = new WeakReference<Context>(context); mSaveTreeId = 0; mOutputFile = output; mOutputPath = UriHelper.getPath(context, output.getUri()); setupMuxer(context, output); }
private void publishFile(DocumentFile fileDocument, long size) { if (!ShareSettingsFragment.isCustomDestinationEnabled(device.getContext())) { Log.i("SharePlugin", "Adding to downloads"); DownloadManager manager = (DownloadManager) device.getContext().getSystemService(Context.DOWNLOAD_SERVICE); manager.addCompletedDownload(fileDocument.getUri().getLastPathSegment(), device.getName(), true, fileDocument.getType(), fileDocument.getUri().getPath(), size, false); } else { //Make sure it is added to the Android Gallery anyway Log.i("SharePlugin", "Adding to gallery"); MediaStoreHelper.indexFile(device.getContext(), fileDocument.getUri()); } }
/** * 指定したUriがDocumentFileの下に存在するフォルダを指し示していれば * 対応するDocumentFileを取得して返す * フォルダが存在していない時に書き込み可能でなければIOExceptionを投げる * @param context * @param baseDoc * @param uri * @return * @throws IOException */ public static DocumentFile getDocumentFile(@NonNull Context context, @NonNull final DocumentFile baseDoc, @Nullable final Uri uri) throws IOException { if (uri != null) { final String basePathString = UriHelper.getPath(context, baseDoc.getUri()); final String uriString = UriHelper.getPath(context, uri); if (!TextUtils.isEmpty(basePathString) && !TextUtils.isEmpty(uriString) && uriString.startsWith(basePathString)) { return getDocumentFile(context, baseDoc, uriString.substring(basePathString.length())); } } return null; } }
/** * 指定したUriがDocumentFileの下に存在するフォルダを指し示していれば * 対応するDocumentFileを取得して返す * フォルダが存在していない時に書き込み可能でなければIOExceptionを投げる * @param context * @param baseDoc * @param uri * @return * @throws IOException */ public static DocumentFile getDocumentFile(@NonNull Context context, @NonNull final DocumentFile baseDoc, @Nullable final Uri uri) throws IOException { if (uri != null) { final String basePathString = UriHelper.getPath(context, baseDoc.getUri()); final String uriString = UriHelper.getPath(context, uri); if (!TextUtils.isEmpty(basePathString) && !TextUtils.isEmpty(uriString) && uriString.startsWith(basePathString)) { return getDocumentFile(context, baseDoc, uriString.substring(basePathString.length())); } } return null; } }
/** * 指定したUriが存在する時にその下に出力用ファイルを生成してOutputStreamとして返す * @param context * @param parent * @param dirs * @param mime * @param name * @return * @throws FileNotFoundException */ public static InputStream getStorageInputStream(@NonNull final Context context, @NonNull final DocumentFile parent, @Nullable final String dirs, final String mime, final String name) throws IOException { final DocumentFile tree = getStorage(context, parent, dirs); if (tree != null) { final DocumentFile file = tree.findFile(name); if (file != null) { if (file.isFile()) { return context.getContentResolver().openInputStream( file.getUri()); } else { throw new IOException("directory with same name already exists"); } } } throw new FileNotFoundException(); }
/** * 指定したUriが存在する時にその下に出力用ファイルを生成してOutputStreamとして返す * @param context * @param parent * @param dirs * @param mime * @param name * @return * @throws FileNotFoundException */ public static InputStream getStorageInputStream(@NonNull final Context context, @NonNull final DocumentFile parent, @Nullable final String dirs, final String mime, final String name) throws IOException { final DocumentFile tree = getStorage(context, parent, dirs); if (tree != null) { final DocumentFile file = tree.findFile(name); if (file != null) { if (file.isFile()) { return context.getContentResolver().openInputStream( file.getUri()); } else { throw new IOException("directory with same name already exists"); } } } throw new FileNotFoundException(); }
/** * 指定したUriが存在する時にその下にファイルを生成するためのpathを返す * @param context * @param treeUri * @param mime * @param fileName * @return */ @Nullable @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static File createStorageFile(@NonNull final Context context, final Uri treeUri, final String mime, final String fileName) { Log.i(TAG, "createStorageFile:" + fileName); if (BuildCheck.isLollipop()) { if ((treeUri != null) && !TextUtils.isEmpty(fileName)) { final DocumentFile saveTree = DocumentFile.fromTreeUri(context, treeUri); final DocumentFile target = saveTree.createFile(mime, fileName); final String path = UriHelper.getPath(context, target.getUri()); if (!TextUtils.isEmpty(path)) { return new File(path); } } } return null; }
/** * 指定したUriが存在する時にその下にファイルを生成するためのpathを返す * @param context * @param treeUri * @param mime * @param fileName * @return */ @Nullable @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static File createStorageFile(@NonNull final Context context, final Uri treeUri, final String mime, final String fileName) { Log.i(TAG, "createStorageFile:" + fileName); if (BuildCheck.isLollipop()) { if ((treeUri != null) && !TextUtils.isEmpty(fileName)) { final DocumentFile saveTree = DocumentFile.fromTreeUri(context, treeUri); final DocumentFile target = saveTree.createFile(mime, fileName); final String path = UriHelper.getPath(context, target.getUri()); if (!TextUtils.isEmpty(path)) { return new File(path); } } } return null; }
/** * 指定したidに対応するUriが存在する時にその下にファイルを生成するためのpathを返す * @param context * @param treeId * @return */ @Nullable @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static File createStorageDir(@NonNull final Context context, final int treeId) { if (BuildCheck.isLollipop()) { final Uri treeUri = getStorageUri(context, treeId); if (treeUri != null) { final DocumentFile saveTree = DocumentFile.fromTreeUri(context, treeUri); final String path = UriHelper.getPath(context, saveTree.getUri()); if (!TextUtils.isEmpty(path)) { return new File(path); } } } return null; }
/** * 指定したidに対応するUriが存在する時にその下にファイルを生成するためのpathを返す * @param context * @param treeId * @return */ @Nullable @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static File createStorageDir(@NonNull final Context context, final int treeId) { if (BuildCheck.isLollipop()) { final Uri treeUri = getStorageUri(context, treeId); if (treeUri != null) { final DocumentFile saveTree = DocumentFile.fromTreeUri(context, treeUri); final String path = UriHelper.getPath(context, saveTree.getUri()); if (!TextUtils.isEmpty(path)) { return new File(path); } } } return null; }
/** * 指定したUriが存在する時にその下に出力用ファイルを生成してOutputStreamとして返す * @param context * @param parent * @param dirs * @param mime * @param name * @return * @throws FileNotFoundException */ public static OutputStream getStorageOutputStream(@NonNull final Context context, @NonNull final DocumentFile parent, @Nullable final String dirs, final String mime, final String name) throws IOException { final DocumentFile tree = getStorage(context, parent, dirs); if (tree != null) { final DocumentFile file = tree.findFile(name); if (file != null) { if (file.isFile()) { return context.getContentResolver().openOutputStream( file.getUri()); } else { throw new IOException("directory with same name already exists"); } } else { return context.getContentResolver().openOutputStream( tree.createFile(mime, name).getUri()); } } throw new FileNotFoundException(); }
if (file.isFile()) { return context.getContentResolver().openInputStream( file.getUri()); } else { throw new IOException("directory with same name already exists");
if (file.isFile()) { return context.getContentResolver().openInputStream( file.getUri()); } else { throw new IOException("directory with same name already exists");
@SuppressLint("NewApi") protected static IMuxer createMuxer(@NonNull final Context context, @NonNull final DocumentFile file) throws IOException { IMuxer result = null; if (VideoConfig.sUseMediaMuxer) { if (BuildCheck.isOreo()) { result = new MediaMuxerWrapper(context.getContentResolver() .openFileDescriptor(file.getUri(), "rw").getFileDescriptor(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); } else { final String path = UriHelper.getPath(context, file.getUri()); final File f = new File(UriHelper.getPath(context, file.getUri())); if (/*!f.exists() &&*/ f.canWrite()) { // 書き込めるファイルパスを取得できればそれを使う result = new MediaMuxerWrapper(path, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); } else { Log.w(TAG, "cant't write to the file, try to use VideoMuxer instead"); } } } if (result == null) { result = new VideoMuxer(context.getContentResolver() .openFileDescriptor(file.getUri(), "rw").getFd()); } return result; } }
/** * 指定したUriが存在する時にその下に出力用ファイルを生成してOutputStreamとして返す * @param context * @param parent * @param dirs * @param mime * @param name * @return * @throws FileNotFoundException */ public static OutputStream getStorageOutputStream(@NonNull final Context context, @NonNull final DocumentFile parent, @Nullable final String dirs, final String mime, final String name) throws IOException { final DocumentFile tree = getStorage(context, parent, dirs); if (tree != null) { final DocumentFile file = tree.findFile(name); if (file != null) { if (file.isFile()) { return context.getContentResolver().openOutputStream( file.getUri()); } else { throw new IOException("directory with same name already exists"); } } else { return context.getContentResolver().openOutputStream( tree.createFile(mime, name).getUri()); } } throw new FileNotFoundException(); }
/** * 指定したidに対応するUriが存在する時にその下に生成したファイルのrawファイルディスクリプタを返す * @param context * @param treeUri * @param mime * @param fileName * @return */ @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static int createStorageFileFD(@NonNull final Context context, final Uri treeUri, final String mime, final String fileName) { Log.i(TAG, "createStorageFileFD:" + fileName); if (BuildCheck.isLollipop()) { if ((treeUri != null) && !TextUtils.isEmpty(fileName)) { final DocumentFile saveTree = DocumentFile.fromTreeUri(context, treeUri); final DocumentFile target = saveTree.createFile(mime, fileName); try { final ParcelFileDescriptor fd = context.getContentResolver().openFileDescriptor(target.getUri(), "rw"); return fd != null ? fd.getFd() : 0; } catch (final FileNotFoundException e) { Log.w(TAG, e); } } } return 0; }
/** * 指定したidに対応するUriが存在する時にその下に生成したファイルのrawファイルディスクリプタを返す * @param context * @param treeUri * @param mime * @param fileName * @return */ @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static int createStorageFileFD(@NonNull final Context context, final Uri treeUri, final String mime, final String fileName) { Log.i(TAG, "createStorageFileFD:" + fileName); if (BuildCheck.isLollipop()) { if ((treeUri != null) && !TextUtils.isEmpty(fileName)) { final DocumentFile saveTree = DocumentFile.fromTreeUri(context, treeUri); final DocumentFile target = saveTree.createFile(mime, fileName); try { final ParcelFileDescriptor fd = context.getContentResolver().openFileDescriptor(target.getUri(), "rw"); return fd != null ? fd.getFd() : 0; } catch (final FileNotFoundException e) { Log.w(TAG, e); } } } return 0; } }