2 голоса
 
616 просмотров
2.09.2018 / 09:58  garfild63

Как создать приложение на Android для открытия файлов определённого типа

Как создать приложение на Android для открытия файлов определённого типа? Чтобы при открытии файла этого типа в браузере или диспетчере файлов система предлагала открыть файл с помощью этого приложения? Как это сделать на Android? Что-то наподобие библиотеки CHAPI на Java 2 MicroEdition.
Ответы
 
1 голос
 
# 2.09.2018 / 11:24  aNNiMON
В AndroidManifest intent-filter c action VIEW для активити ставь:
  1. <activity
  2.        android:name=".MainActivity"
  3.        android:label="@string/app_name">
  4.     <intent-filter>
  5.         <action android:name="android.intent.action.MAIN"/>
  6.         <category android:name="android.intent.category.LAUNCHER"/>
  7.     </intent-filter>
  8.  
  9.     <intent-filter>
  10.         <action android:name="android.intent.action.VIEW"/>
  11.         <data
  12.                android:scheme="file"
  13.                android:host="*"
  14.                android:mimeType="*/*"
  15.                android:pathPattern=".*\\.lis"/>
  16.         <category android:name="android.intent.category.DEFAULT"/>
  17.     </intent-filter>
  18. </activity>

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3.     // ...
  4.     final Intent intent = getIntent();
  5.     if (intent.getData() != null) {
  6.         final Uri fileUri = intent.getData();
  7.         try (InputStream is = getContentResolver().openInputStream(fileUri)) {
  8.             // read file
  9.         } catch (IOException e) {
  10.             // ...
  11.         }
  12.     }
  13. }
2.09.2018 / 13:20  garfild63
Спасибо большое)
28.11.2018 / 10:33  danfedotov45
Довольно полезненько! Спс
Всего: 1

Реклама

Мы в соцсетях

tw tg yt gt