本文整理汇总了Java中android.provider.DocumentsContract类的典型用法代码示例。如果您正苦于以下问题:Java DocumentsContract类的具体用法?Java DocumentsContract怎么用?Java DocumentsContract使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DocumentsContract类属于android.provider包,在下文中一共展示了DocumentsContract类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handleImageOnKitkat
import android.provider.DocumentsContract; //导入依赖的package包/类
@TargetApi(19)
private void handleImageOnKitkat(Intent data){
String imagePath=null;
Uri uri=data.getData();
if (DocumentsContract.isDocumentUri(this,uri)){
//如果是document類型的Uri。則通過document id 處理
String docId=DocumentsContract.getDocumentId(uri);
if ("com.android.providers.media.documents".equals(uri.getAuthority())){
String id=docId.split(":")[1];//解析出文字格式的id
String selection=MediaStore.Images.Media._ID+"="+id;
imagePath=getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);
} else if("com.android.providers.downloads.documents".equals(uri.getAuthority())){
Uri contentUri= ContentUris.withAppendedId(Uri.parse("content://downloads/public" +
"_downloads"),Long.valueOf(docId));
}
}else if ("content".equalsIgnoreCase(uri.getScheme())){
//如果是content類型的Uri,則是用普通方式處理
imagePath=getImagePath(uri,null);
}else if ("file".equalsIgnoreCase(uri.getScheme())){
//如果是file類型的Uri,直接獲取圖片路徑即可
imagePath=uri.getPath();
}
displayImage(imagePath);//根據圖片路徑顯示圖片
}
示例2: listFiles
import android.provider.DocumentsContract; //导入依赖的package包/类
public static Uri[] listFiles(Context context, Uri self) {
final ContentResolver resolver = context.getContentResolver();
final Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(self,
DocumentsContract.getDocumentId(self));
final ArrayList<Uri> results = new ArrayList<Uri>();
Cursor c = null;
try {
c = resolver.query(childrenUri, new String[] {
DocumentsContract.Document.COLUMN_DOCUMENT_ID }, null, null, null);
while (c.moveToNext()) {
final String documentId = c.getString(0);
final Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(self,
documentId);
results.add(documentUri);
}
} catch (Exception e) {
Log.w(TAG, "Failed query: " + e);
} finally {
closeQuietly(c);
}
return results.toArray(new Uri[results.size()]);
}
示例3: doMoveFilesAPI21
import android.provider.DocumentsContract; //导入依赖的package包/类
/**
* Move a file using the new API 21 methods.
*
* @param data File rename data info.
* @param oldFile Old file reference.
* @param newFile New file reference.
* @return True if the file was moved.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private boolean doMoveFilesAPI21(Uri oldUri, FileRenameData data, File oldFile, File newFile) throws FileNotFoundException {
File newParent = newFile.getParentFile();
Uri newParentUri = mApplication.getDocumentUri(mSelectedFolders, newParent.getAbsolutePath());
Uri newUri = DocumentsContract.createDocument(mContentResolver, newParentUri,
data.getMimeType(), newFile.getName());
boolean result = false;
long size = 0;
try {
if (oldUri != null && newUri != null) {
size = copyFileWithStreams(oldUri, newUri);
}
result = (size > 0);
} catch (Exception e) {
mApplication.logE(TAG, "doMoveFilesNewAPI " + oldFile + " to " + newFile, e);
}
if (result) {
result = DocumentsContract.deleteDocument(mContentResolver, oldUri);
if (result && newFile.exists()) {
newFile.setLastModified(data.getDateAdded());
}
}
return result;
}
示例4: onRun
import android.provider.DocumentsContract; //导入依赖的package包/类
@Override
public void onRun() throws Throwable {
os = new FileOutputStream(file);
if (file != null)
size = file.length();
Util.log("Delete ", file);
file.delete();
FileUtils.forceDelete(file);
if (uri != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
DocumentsContract.deleteDocument(context.getContentResolver(), uri); //For kitkat users
context.getContentResolver().delete(uri, null, null); //Try to delete under content resolver
}
new SingleMediaScanner(context, file); //Rescan and remove from gallery
Storage.shredFile(os, size, file);
file.delete();
FileUtils.forceDelete(file);
}
示例5: getRealPath
import android.provider.DocumentsContract; //导入依赖的package包/类
@SuppressLint("NewApi")
private String getRealPath(Uri uri){
String filePath = null;
String uriString = uri.toString();
if(uriString.startsWith("content://media")){
filePath = getDataColumn(context, uri, null, null);
} else if (uriString.startsWith("file")){
filePath = uri.getPath();
} else if (uriString.startsWith("content://com")){
String docId = DocumentsContract.getDocumentId(uri);
String[] split = docId.split(":");
Uri contentUri = null;
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String selection = "_id=?";
String[] selectionArgs = new String[] {split[1]};
filePath = getDataColumn(context, contentUri, selection, selectionArgs);
}
return filePath;
}
示例6: handleImage
import android.provider.DocumentsContract; //导入依赖的package包/类
private void handleImage(Uri uri){
String imagePath = null;
if(DocumentsContract.isDocumentUri(getActivity(), uri)){
String docId = DocumentsContract.getDocumentId(uri);
if("com.android.providers.media.documents".equals(uri.getAuthority())){
String id = docId.split(":")[1];
String selection = MediaStore.Images.Media._ID+"="+id;
imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
}
else if("com.android.providers.downloads.documents".equals(uri.getAuthority())){
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
imagePath = getImagePath(contentUri, null);
}
}
else if("content".equalsIgnoreCase(uri.getScheme())){
imagePath = getImagePath(uri, null);
}
else if("file".equalsIgnoreCase(uri.getScheme())){
imagePath = uri.getPath();
}
filePath = imagePath;
Log.i(TAG, "handleImage: Album filepath"+filePath);
getQiniuUpToken();
}
示例7: handleImageOnKitKat
import android.provider.DocumentsContract; //导入依赖的package包/类
/**
* 执行打开相册逻辑
*/
@TargetApi(19)
private String handleImageOnKitKat(Intent data) {
String imagePath = null;
Uri uri = data.getData();
if (DocumentsContract.isDocumentUri(this, uri)) {
String docId = DocumentsContract.getDocumentId(uri);
if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
String id = docId.split(":")[1];
String selection = MediaStore.Images.Media._ID + "=" + id;
imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
} else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
Long.valueOf(docId));
imagePath = getImagePath(contentUri, null);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
imagePath = getImagePath(uri, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
imagePath = uri.getPath();
}
return imagePath;
}
示例8: handleImageOnKitKat
import android.provider.DocumentsContract; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.KITKAT)
public void handleImageOnKitKat(Intent data) {
String imagePath=null;
Uri uri=data.getData();
if (DocumentsContract.isDocumentUri(this,uri)){
//如果是document类型的Uri,则通过document id处理
String docId=DocumentsContract.getDocumentId(uri);
if ("com.android.providers.media.documents".equals(uri.getAuthority())){
String id=docId.split(":")[1];//解析数字可是的id
String selection=MediaStore.Images.Media._ID+"="+id;
imagePath=getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);
}else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())){
Uri contentUri= ContentUris.withAppendedId(Uri.parse("content://downloads/punlic_downloads"),Long.valueOf(docId));
imagePath=getImagePath(contentUri,null);
}
}else if ("content".equalsIgnoreCase(uri.getScheme())){
//如果是content类型的Uri,则使用普通方法
imagePath=getImagePath(uri,null);
}else if ("file".equalsIgnoreCase(uri.getScheme())){
imagePath=uri.getPath();
}
displayImage(imagePath);//根据图片路径显示图片
}
示例9: getRealPathFromURI_API19
import android.provider.DocumentsContract; //导入依赖的package包/类
@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri){
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(uri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{ id }, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}
示例10: handleImageOnKitKat
import android.provider.DocumentsContract; //导入依赖的package包/类
@TargetApi(19)
private void handleImageOnKitKat(Intent data){
String imagePath=null;
Uri uri=data.getData();
if (DocumentsContract.isDocumentUri(getActivity(),uri)){
//如果是 document 类型的 Uri,则通过document id处理
String docId=DocumentsContract.getDocumentId(uri);
if ("com.android.providers.media.documents".equals(uri.getAuthority())){
String id=docId.split(":")[1]; //解析出数字格式的id
String selection=MediaStore.Images.Media._ID+"="+id;
imagePath=getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);
}else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())){
Uri contentUri= ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
Long.valueOf(docId));
imagePath=getImagePath(contentUri,null);
}
}else if ("content".equalsIgnoreCase(uri.getScheme())){
//如果是content类型的Uri 则使用普通方式处理
imagePath=getImagePath(uri,null);
}else if ("file".equalsIgnoreCase(uri.getScheme())){
//如果是file类型的Uri,直接获取图片路径即可
imagePath=uri.getPath();
}
displayImage(imagePath); //根据图片路径显示图片
}
示例11: canWrite
import android.provider.DocumentsContract; //导入依赖的package包/类
public static boolean canWrite(Context context, Uri self) {
// Ignore if grant doesn't allow write
if (context.checkCallingOrSelfUriPermission(self, Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
!= PackageManager.PERMISSION_GRANTED) {
return false;
}
final String type = getRawType(context, self);
final int flags = queryForInt(context, self, DocumentsContract.Document.COLUMN_FLAGS, 0);
// Ignore documents without MIME
if (TextUtils.isEmpty(type)) {
return false;
}
// Deletable documents considered writable
if ((flags & DocumentsContract.Document.FLAG_SUPPORTS_DELETE) != 0) {
return true;
}
if (DocumentsContract.Document.MIME_TYPE_DIR.equals(type)
&& (flags & DocumentsContract.Document.FLAG_DIR_SUPPORTS_CREATE) != 0) {
// Directories that allow create considered writable
return true;
} else if (!TextUtils.isEmpty(type)
&& (flags & DocumentsContract.Document.FLAG_SUPPORTS_WRITE) != 0) {
// Writable normal files considered writable
return true;
}
return false;
}
示例12: getPicturePathFromUriAboveApi19
import android.provider.DocumentsContract; //导入依赖的package包/类
private static String getPicturePathFromUriAboveApi19(Context context, Uri uri) {
String filePath = null;
if (DocumentsContract.isDocumentUri(context, uri)) {
// 如果是document类型的 uri, 则通过document id来进行处理
String documentId = DocumentsContract.getDocumentId(uri);
if (isMediaDocument(uri)) { // MediaProvider
// 使用':'分割
String id = documentId.split(":")[1];
String selection = MediaStore.Images.Media._ID + "=?";
String[] selectionArgs = {id};
filePath = getDataColumn(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection, selectionArgs);
} else if (isDownloadsDocument(uri)) { // DownloadsProvider
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(documentId));
filePath = getDataColumn(context, contentUri, null, null);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
// 如果是 content 类型的 Uri
filePath = getDataColumn(context, uri, null, null);
} else if ("file".equals(uri.getScheme())) {
// 如果是 file 类型的 Uri,直接获取图片对应的路径
filePath = uri.getPath();
}
return filePath;
}
示例13: handleImageOnKitKat
import android.provider.DocumentsContract; //导入依赖的package包/类
@TargetApi(19)
private void handleImageOnKitKat(Intent data){
String imagePath = null;
Uri uri = data.getData();
if(DocumentsContract.isDocumentUri(this,uri)){
String docId = DocumentsContract.getDocumentId(uri);
if("com.android.provider.media,documents".equals(uri.getAuthority())){
String id = docId.split(":")[1];
String selection = MediaStore.Images.Media._ID + "=" + id;
imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);
}else if("com.android.provider.downloads.documents".equals(uri.getAuthority())){
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),Long.valueOf(docId));
imagePath = getImagePath(contentUri,null);
}
}else if("content".equalsIgnoreCase(uri.getScheme())){
imagePath = getImagePath(uri,null);
}else if("file".equalsIgnoreCase(uri.getScheme())){
imagePath = uri.getPath();
}
displayImage(imagePath);//根据图片路径显示图片
}
示例14: getPath
import android.provider.DocumentsContract; //导入依赖的package包/类
@Nullable public static String getPath(@NonNull Context context, @NonNull Uri uri) {
String filePath = null;
try {
String wholeID = DocumentsContract.getDocumentId(uri);
String id = wholeID.split(":")[1];
String[] column = {MediaStore.Images.Media.DATA};
String sel = MediaStore.Images.Media._ID + "=?";
try (Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{id}, null)) {
if (cursor != null) {
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
}
}
} catch (Exception ignored) {}
return filePath;
}
示例15: handleImage
import android.provider.DocumentsContract; //导入依赖的package包/类
private String handleImage(Intent data) {
imagePath = null;
Uri uri = data.getData();
if (DocumentsContract.isDocumentUri(this, uri)) {
String docId = DocumentsContract.getDocumentId(uri);
if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
String id = docId.split(":")[1];
String selection = MediaStore.Images.Media._ID + "=" + id;
imagePath = getPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
} else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),
Long.valueOf(docId));
imagePath = getPath(contentUri, null);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
imagePath = getPath(uri, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
imagePath = uri.getPath();
}
return imagePath;
}