下面列出了android.provider.DocumentsContract#createDocument ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void createFolder(String new_name)
{
try
{
Uri new_uri = DocumentsContract.createDocument(ctx.getContentResolver(), uri, Document.MIME_TYPE_DIR,
new_name);
if (new_uri != null)
{
notifyRefr(new_name);
return;
}
}
catch (Exception e)
{
e.printStackTrace();
}
notify(ctx.getString(R.string.fman_create_folder_error, new_name), CommanderIf.OPERATION_FAILED);
}
@Override
public com.sovworks.eds.fs.Directory createDirectory(String name) throws IOException
{
Uri uri = DocumentsContract.createDocument(
_context.getContentResolver(),
_path.getDocumentUri(),
DocumentsContract.Document.MIME_TYPE_DIR,
name
);
if(uri == null)
throw new IOException("Failed creating folder");
return new Directory(new DocumentPath(uri));
}
@Override
public com.sovworks.eds.fs.File createFile(String name) throws IOException
{
String mimeType = FileOpsService.getMimeTypeFromExtension(_context, new StringPathUtil(name).getFileExtension());
Uri uri = DocumentsContract.createDocument(
_context.getContentResolver(),
_path.getDocumentUri(),
mimeType, name);
if(uri == null)
throw new IOException("Failed creating file");
return new File(new DocumentPath(uri));
}
public static Uri createFile(Context context, Uri self, String mimeType,
String displayName) {
try {
return DocumentsContract.createDocument(context.getContentResolver(), self, mimeType,
displayName);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
public static Uri createFile(Context context, Uri self, String mimeType,
String displayName) {
try {
return DocumentsContract.createDocument(context.getContentResolver(), self, mimeType,
displayName);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
public static Uri createFile(Context context, Uri self, String mimeType,
String displayName) {
try {
return DocumentsContract.createDocument(context.getContentResolver(), self, mimeType,
displayName);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
@Override
public Uri newFile(String fileName)
{
try
{
Uri curr = getUri();
String mime = FileUtils.getMimeByExt(FileUtils.getFileExt(fileName), "*/*");
return DocumentsContract.createDocument(ctx.getContentResolver(), curr, mime, fileName);
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public static Uri createFile(Context context, Uri self, String mimeType,
String displayName) {
try {
return DocumentsContract.createDocument(context.getContentResolver(), self, mimeType,
displayName);
} catch (Exception e) {
// Maybe user ejects tf card
Log.e(TAG, "Failed to createFile", e);
return null;
}
}
/**
* Creates a directory under the directory represented as the uri in the argument.
*
* @param uri The uri of the directory under which a new directory is created.
* @param directoryName The directory name of a new directory.
*/
//VisibileForTesting
void createDirectory(Uri uri, String directoryName) {
ContentResolver contentResolver = getActivity().getContentResolver();
Uri docUri = DocumentsContract.buildDocumentUriUsingTree(uri,
DocumentsContract.getTreeDocumentId(uri));
Uri directoryUri = null;
try {
directoryUri = DocumentsContract
.createDocument(contentResolver, docUri, Document.MIME_TYPE_DIR, directoryName);
} catch (IOException e) {
Log.w(TAG, "IOException", e);
}
if (directoryUri != null) {
Log.i(TAG, String.format(
"Created directory : %s, Document Uri : %s, Created directory Uri : %s",
directoryName, docUri, directoryUri));
Toast.makeText(getActivity(), String.format("Created a directory [%s]",
directoryName), Toast.LENGTH_SHORT).show();
} else {
Log.w(TAG, String.format("Failed to create a directory : %s, Uri %s", directoryName,
docUri));
Toast.makeText(getActivity(), String.format("Failed to created a directory [%s] : ",
directoryName), Toast.LENGTH_SHORT).show();
}
}
public static Uri createFile(Context context, Uri self, String mimeType,
String displayName) {
return DocumentsContract.createDocument(context.getContentResolver(), self, mimeType,
displayName);
}