Tìm hiểu cách thực hiện. Hy vọng rằng điều này có thể giúp người khác. Mỏ bên, một phần từ các bài viết khác. Nó nhằm mục đích xử lý các tệp đính kèm .gcsb.
Mục đích-lọc là
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="application/octet-stream" />
</intent-filter>
và mã trong hoạt động onCreate()/onRestart() là
Intent intent = getIntent();
InputStream is = null;
FileOutputStream os = null;
String fullPath = null;
try {
String action = intent.getAction();
if (!Intent.ACTION_VIEW.equals(action)) {
return;
}
Uri uri = intent.getData();
String scheme = uri.getScheme();
String name = null;
if (scheme.equals("file")) {
List<String> pathSegments = uri.getPathSegments();
if (pathSegments.size() > 0) {
name = pathSegments.get(pathSegments.size() - 1);
}
} else if (scheme.equals("content")) {
Cursor cursor = getContentResolver().query(uri, new String[] {
MediaStore.MediaColumns.DISPLAY_NAME
}, null, null, null);
cursor.moveToFirst();
int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
if (nameIndex >= 0) {
name = cursor.getString(nameIndex);
}
} else {
return;
}
if (name == null) {
return;
}
int n = name.lastIndexOf(".");
String fileName, fileExt;
if (n == -1) {
return;
} else {
fileName = name.substring(0, n);
fileExt = name.substring(n);
if (!fileExt.equals(".gcsb")) {
return;
}
}
fullPath = ""/* create full path to where the file is to go, including name/ext */;
is = getContentResolver().openInputStream(uri);
os = new FileOutputStream(fullPath);
byte[] buffer = new byte[4096];
int count;
while ((count = is.read(buffer)) > 0) {
os.write(buffer, 0, count);
}
os.close();
is.close();
} catch (Exception e) {
if (is != null) {
try {
is.close();
} catch (Exception e1) {
}
}
if (os != null) {
try {
os.close();
} catch (Exception e1) {
}
}
if (fullPath != null) {
File f = new File(fullPath);
f.delete();
}
}
Nó xuất hiện để làm việc trong gmail và email các ứng dụng tiêu chuẩn của Android. Tên tệp được lấy theo hai cách khác nhau tùy thuộc vào việc 'tải xuống' (tệp lược đồ) hoặc 'xem trước' (nội dung chương trình) đã được nhấn trong gmail.
Lưu ý rằng điều cực kỳ quan trọng là hoạt động không được đặt là một trường hợp duy nhất.
Cảm ơn bạn rất nhiều! Tôi đã bị mắc kẹt trên một cái gì đó như thế này trong nhiều giờ. – sigmabeta
Ngẫu nhiên, bạn có biết liệu có bất kỳ quyền nào được yêu cầu để làm cho mã trên hoạt động không? – sigmabeta
Cảm ơn bạn, đã làm việc ngay! – mifthi