我正在尝试将文件从通用托管文件服务作为 Dropbox/Drive/Onedrive 复制到我设备中的特定路径。
我创建了一个工作示例来复制 jpg 文件,但我想要创建的是用于复制以下类型的版本:.docx、.pdf、.pptx、.jpg、.jpeg、.png、.gif、.mp3、 .mp4、.xlsx。
我需要做什么?如何修改我的代码?
谢谢
// the flow start from here
public void newFile() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select file"), NEW_FILE_PRIVATE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == NEW_FILE_PRIVATE && resultCode == Activity.RESULT_OK) {
if (data != null) {
Uri currentUri = null;
currentUri = data.getData();
String contentString = currentUri.toString();
if (contentString.contains("com.dropbox.android") ||
contentString.contains("com.microsoft.skydrive") ||
contentString.contains("com.google.android.apps.docs.storage")) {
try {
copyFileContent(currentUri);
} catch (IOException e) {
Log.e("cloud error", "error");
}
}
}
}
}
private String copyFileContent(Uri uri) throws IOException {
ParcelFileDescriptor pFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = pFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
pFileDescriptor.close();
/* temporary path */
String extension = getMimeType(getApplicationContext(), uri);
String temporaryFilePath = Environment.getExternalStorageDirectory().toString()
.concat("/").concat("temporaryfile")
.concat(".").concat(extension);
File file = new File(temporaryFilePath);
OutputStream outStream = null;
try {
/* make a new bitmap from your file */
outStream = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return temporaryFilePath;
}
编辑感谢@CommonsWare,我编写了一个使用此代码获取文件扩展名的解决方案:
public String getExtension(Context context, Uri uri) {
String extension;
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
final MimeTypeMap mime = MimeTypeMap.getSingleton();
extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uri));
} else {
extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString());
}
return extension;
}
编辑 2 在@Greenapps 的帮助下,我实现了这个解决方案:
private String copyFileContent(Uri uri) throws IOException {
InputStream inputStream = getContentResolver().openInputStream(uri);
/* temporary path */
String extension = getMimeType(getApplicationContext(), uri);
String temporaryFilePath = Environment.getExternalStorageDirectory().toString()
.concat("/").concat("temporaryfile")
.concat(".").concat(extension);
OutputStream outStream = new FileOutputStream(temporaryFilePath);
final byte[] b = new byte[8192];
for (int r;(r = inputStream.read(b)) != -1;) outStream.write(b, 0, r);
return temporaryFilePath;
}
是否存在获取文件名而不是将其命名为“临时文件”的方法?
编辑 3
private String copyFileContent(Uri uri) throws IOException {
InputStream inputStream = getContentResolver().openInputStream(uri);
String uriString = uri.toString();
String fileName = "";
if (!uriString.contains("com.dropbox.android")) {
Cursor returnCursor = getContentResolver().query(uri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
fileName = returnCursor.getString(nameIndex);
} else {
fileName = uriString.substring(uriString.lastIndexOf("/") + 1, uriString.lastIndexOf("."));
}
/* temporary path */
String extension = getMimeType(getApplicationContext(), uri);
String temporaryFilePath = Environment.getExternalStorageDirectory().toString()
.concat("/").concat(egoName)
.concat("/").concat("PRIVATE")
.concat("/").concat(fileName)
.concat(".").concat(extension);
OutputStream outStream = new FileOutputStream(temporaryFilePath);
final byte[] b = new byte[8192];
for (int r;
(r = inputStream.read(b)) != -1;) outStream.write(b, 0, r);
return temporaryFilePath;
}