gpt4 book ai didi

android - 在 Android 版本 4.4.2 中使用 GoogleDrive 时无法获取文件路径

转载 作者:行者123 更新时间:2023-11-29 20:47:12 24 4
gpt4 key购买 nike

在我的 android 应用程序中,我使用 google drive 为我的应用程序选择图像和文件,它在除 4.4.2 之外的所有 API 版本中都能完美运行,每当我尝试选择图像或文件时,我都能得到文件名但不能能够获取文件路径,总是返回空路径

我的代码:

   // Get real path from Google Drive
public String getPathfromGoogleDrive(Intent data, Uri contentURI) {

if (contentURI == null) {
return null;
}
String[] filePathColumn = { MediaStore.Images.Media.DATA };

String mCurrentPhotoPath = new String();

Cursor cursor = null;

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
LogUtil.d("currentapiVersion" + currentapiVersion);

if (currentapiVersion == 19) {

String wholeID = DocumentsContract.getDocumentId(contentURI);

// Split at colon, use second item in the array
String id = wholeID.split(";")[0];

// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";

cursor = getActivity().getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
filePathColumn, sel, new String[] { id }, null);

LogUtil.d("Cursor Count" + cursor.getCount());

if (cursor.getCount() > 0 && cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
mCurrentPhotoPath = cursor.getString(columnIndex);
cursor.close();
}

}

我的 Intent :

        int currentapiVersion = android.os.Build.VERSION.SDK_INT;

if (currentapiVersion == 19) {

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
String strType = "*/*";
intent.setDataAndType(null, strType);
startActivityForResult(intent, Gallery);

} else {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setPackage("com.google.android.apps.docs");
String strType = "*/*";
intent.setDataAndType(null, strType);
startActivityForResult(intent, Gallery);
}

如有错误请指正提前致谢

最佳答案

我们可以像下面这样使用输入流,而不是获取文件的真实路径

  Bitmap bitmap = null;
InputStream input = null;
try {
input = getActivity().getContentResolver().openInputStream(selectedImageURI);
bitmap = BitmapFactory.decodeStream(input);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

从驱动器获取文件并将其写入语言环境(sd 卡)

sourceuri - your cnontent uri
destination - path where you want to save in sd card
public boolean savefile(String name, Uri sourceuri, String destination)
throws IOException {
// String sourceFilename = sourceuri.getPath();

int originalsize = 0;
InputStream input = null;
try {
input = getContentResolver().openInputStream(sourceuri);

Log.Logger().finest("input in profileview Activity" + input);

} catch (FileNotFoundException e) {
e.printStackTrace();

filenotfoundexecption = true;

}

try {
originalsize = input.available();

Log.Logger().finest(
"Profile view activity originalsize" + originalsize);

BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(input);
bos = new BufferedOutputStream(new FileOutputStream(
destination, false));
byte[] buf = new byte[originalsize];
bis.read(buf);
do {
bos.write(buf);
} while (bis.read(buf) != -1);

} catch (IOException e) {
Mint.logException(e);

filenotfoundexecption = true;

return false;

}

} catch (NullPointerException e1) {
Mint.logException(e1);
filenotfoundexecption = true;
}

/*
* String[] cmd = new String[] { "logcat", "-f", GridViewDemo_LOGPATH,
* "-v", "time", "ActivityManager:W", "myapp:D" };
*
* Runtime.getRuntime().exec(cmd);
*/

return true;
}

关于android - 在 Android 版本 4.4.2 中使用 GoogleDrive 时无法获取文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30072936/

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