2018/11/01

[Android] Attach File from Device Storage

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

這邊簡單寫一個可以選擇影片檔案播放的程式。
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.chris.myapplication.MainActivity">

    <TextView
        android:id="@+id/id_tv_filename"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="MainActivity"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/id_btn_Attach"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="File"
        android:onClick="onClickAttachFile"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/id_tv_filename" />

    <Button
        android:id="@+id/id_btn_Play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:onClick="onClick"
        android:text="Play"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/id_vVideo" />

    <VideoView
        android:id="@+id/id_vVideo"
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/id_btn_Attach" />

</android.support.constraint.ConstraintLayout>

程式碼的部分
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.VideoView;

import lib.folderpicker.FolderPicker;

public class MainActivity extends AppCompatActivity{

    private int PICKFILE_RESULT_CODE = 1;

    private Button btnPlay;
    private TextView tv_filename;
    private VideoView vVideoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnPlay = (Button)findViewById(R.id.id_btn_Play);
        tv_filename = (TextView)findViewById(R.id.id_tv_filename);
        vVideoView = (VideoView)findViewById(R.id.id_vVideo);
    }

    public void onClick(View v){
        if(vVideoView.isPlaying()){
            vVideoView.pause();
            btnPlay.setText("Play");
        }else {
            vVideoView.start();
            btnPlay.setText("Pause");
        }
    }

    // open attach file folder
    public void onClickAttachFile(View v){
        Intent intent = new Intent(this, FolderPicker.class);
        intent.putExtra("title", "Select file to upload");
        intent.putExtra("pickFiles", true);
        startActivityForResult(intent, PICKFILE_RESULT_CODE);
    }

    // after select the file
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
        if (requestCode == PICKFILE_RESULT_CODE && resultCode == Activity.RESULT_OK) {
            String readFile = intent.getExtras().getString("data");
            tv_filename.setText(readFile);
            vVideoView.setVideoURI(Uri.parse(readFile));
        }
    }
}

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

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

END

沒有留言:

張貼留言