2018/11/01

[Android] Attach File from Device Storage

如果要在 App 使用附加檔案的功能,或是想要在操作介面手動選擇檔案,
Android 裡面有個 folder picker 工具可以用。
一開始先在 app 的 build.gradle 裡面的 dependencies 加入工具:
  1. compile 'lib.kashif:folderpicker:2.4'

這邊簡單寫一個可以選擇影片檔案播放的程式。
activity_main.xml:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.example.chris.myapplication.MainActivity">
  8.  
  9. <TextView
  10. android:id="@+id/id_tv_filename"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_marginEnd="8dp"
  14. android:layout_marginStart="8dp"
  15. android:layout_marginTop="8dp"
  16. android:text="MainActivity"
  17. app:layout_constraintEnd_toEndOf="parent"
  18. app:layout_constraintStart_toStartOf="parent"
  19. app:layout_constraintTop_toTopOf="parent" />
  20.  
  21. <Button
  22. android:id="@+id/id_btn_Attach"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_marginEnd="8dp"
  26. android:layout_marginStart="8dp"
  27. android:layout_marginTop="8dp"
  28. android:text="File"
  29. android:onClick="onClickAttachFile"
  30. app:layout_constraintEnd_toEndOf="parent"
  31. app:layout_constraintStart_toStartOf="parent"
  32. app:layout_constraintTop_toBottomOf="@+id/id_tv_filename" />
  33.  
  34. <Button
  35. android:id="@+id/id_btn_Play"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:layout_marginEnd="8dp"
  39. android:layout_marginStart="8dp"
  40. android:layout_marginTop="8dp"
  41. android:onClick="onClick"
  42. android:text="Play"
  43. app:layout_constraintEnd_toEndOf="parent"
  44. app:layout_constraintStart_toStartOf="parent"
  45. app:layout_constraintTop_toBottomOf="@+id/id_vVideo" />
  46.  
  47. <VideoView
  48. android:id="@+id/id_vVideo"
  49. android:layout_width="wrap_content"
  50. android:layout_height="300dp"
  51. android:layout_marginEnd="8dp"
  52. android:layout_marginStart="8dp"
  53. android:layout_marginTop="8dp"
  54. app:layout_constraintEnd_toEndOf="parent"
  55. app:layout_constraintStart_toStartOf="parent"
  56. app:layout_constraintTop_toBottomOf="@+id/id_btn_Attach" />
  57.  
  58. </android.support.constraint.ConstraintLayout>

程式碼的部分
MainActivity.java
  1. import android.app.Activity;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9. import android.widget.VideoView;
  10.  
  11. import lib.folderpicker.FolderPicker;
  12.  
  13. public class MainActivity extends AppCompatActivity{
  14.  
  15. private int PICKFILE_RESULT_CODE = 1;
  16.  
  17. private Button btnPlay;
  18. private TextView tv_filename;
  19. private VideoView vVideoView;
  20.  
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25.  
  26. btnPlay = (Button)findViewById(R.id.id_btn_Play);
  27. tv_filename = (TextView)findViewById(R.id.id_tv_filename);
  28. vVideoView = (VideoView)findViewById(R.id.id_vVideo);
  29. }
  30.  
  31. public void onClick(View v){
  32. if(vVideoView.isPlaying()){
  33. vVideoView.pause();
  34. btnPlay.setText("Play");
  35. }else {
  36. vVideoView.start();
  37. btnPlay.setText("Pause");
  38. }
  39. }
  40.  
  41. // open attach file folder
  42. public void onClickAttachFile(View v){
  43. Intent intent = new Intent(this, FolderPicker.class);
  44. intent.putExtra("title", "Select file to upload");
  45. intent.putExtra("pickFiles", true);
  46. startActivityForResult(intent, PICKFILE_RESULT_CODE);
  47. }
  48.  
  49. // after select the file
  50. @Override
  51. protected void onActivityResult(int requestCode, int resultCode, Intent intent)
  52. {
  53. if (requestCode == PICKFILE_RESULT_CODE && resultCode == Activity.RESULT_OK) {
  54. String readFile = intent.getExtras().getString("data");
  55. tv_filename.setText(readFile);
  56. vVideoView.setVideoURI(Uri.parse(readFile));
  57. }
  58. }
  59. }

最後記得在AndroidManifest.xml 加入讀取檔案的權限:
  1. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

用這個方法就可以選擇 attach 的檔案,不管是影片、圖片或是音檔都可以。

END

沒有留言:

張貼留言