gpt4 book ai didi

android - 如何下载。使用 Intent 选择器从 Google Drive 中选择的 txt 或 .pdf 文件

转载 作者:行者123 更新时间:2023-11-29 19:28:25 26 4
gpt4 key购买 nike

我想从驱动器下载文本和 pdf 文件并将其保存在设备中。我尝试了很多链接,但找不到正确的解决方案。我试过的代码如下所示

我尝试使用以下代码下载 google drive 文件。

 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/*");
Intent i = Intent.createChooser(intent, "Choose an application");
startActivityForResult(i, RC_FILE);

在 onActivityResult 中

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == RC_PICK_A_FILE) {

Uri inputStreamUri = data.getData();
String filePath = FilePickUtils.getPath(
getApplicationContext(), inputStreamUri);
}
}
}

和 FilePickutils

public class FilePickUtils {

@SuppressLint("NewApi")
public static String getPath(final Context context, final Uri uri) {

// check here to KITKAT or new version
final boolean isKitKatorUp = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

// DocumentProvider
if (isKitKatorUp && DocumentsContract.isDocumentUri(context, uri)) {

// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];

if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/"
+ split[1];
}
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {

final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),
Long.valueOf(id));

return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];

Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}

final String selection = "_id=?";
final String[] selectionArgs = new String[] { split[1] };

return getDataColumn(context, contentUri, selection,
selectionArgs);
}else if(isGoogleFileUri(uri))
{
Log.d("tag","====isGoogleFileUri========="+uri);
return getDataColumn(context, uri, null, null);
}
}

// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {

// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();


return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}

return null;
}

/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context
* The context.
* @param uri
* The Uri to query.
* @param selection
* (Optional) Filter used in the query.
* @param selectionArgs
* (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri,
String selection, String[] selectionArgs) {

Cursor cursor = null;
final String column = "_data";
final String[] projection = { column };

try {
cursor = context.getContentResolver().query(uri,new String[] { android.provider.MediaStore.Files.FileColumns.DATA },
null, null, null);
Log.d("tag","=====cursor===="+cursor);

/* Cursor cursor = this.getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
filePath = cursor.getString(0);
cursor.close();*/
if (cursor != null && cursor.moveToFirst()) {
int idx = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
// final int index = cursor.getColumnIndexOrThrow(column);
Log.d("tag","========index==="+idx);
Log.d("tag","======cursor.getString(index)====="+cursor.getString(idx));
return cursor.getString(idx);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}

/**
* @param uri
* The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri
.getAuthority());
}

/**
* @param uri
* The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri
.getAuthority());
}

/**
* @param uri
* The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri
.getAuthority());
}

/**
* @param uri
* The Uri to check.
* @return Whether the Uri authority is Google Photos.
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri
.getAuthority());
}

public static boolean isGoogleFileUri(Uri uri) {
return "com.google.android.apps.docs.storage".equals(uri
.getAuthority());
}

对于以下内容 uri,我最终失败了

content://com.google.android.apps.docs.storage/document/acc%3D19%3Bdoc%3D10211

文件路径返回为“null”

最佳答案

给定一个 Uri,要制作本地文件副本,您:

  • 使用 ContentResolveropenInputStream() 获取 Uri 表示的内容的 InputStream >

  • 在所需文件上创建一个FileOutputStream

  • 使用标准 Java I/O 将字节从 InputStream 复制到 FileOutputStream

你最终得到这样的代码:

    FileOutputStream fos=new FileOutputStream(f);
BufferedOutputStream out=new BufferedOutputStream(fos);
InputStream in=
getContentResolver().openInputStream(document);

try {
byte[] buffer=new byte[8192];
int len=0;

while ((len=in.read(buffer))>=0) {
out.write(buffer, 0, len);
}

out.flush();
}
finally {
fos.getFD().sync();
out.close();
in.close();
}

(其中 documentUrif 是目标 File)

关于android - 如何下载。使用 Intent 选择器从 Google Drive 中选择的 txt 或 .pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40845939/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com