1. 程式人生 > >獲取本地圖片/視訊

獲取本地圖片/視訊

 獲取手機哩的照片或視訊

佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="test.com.photo.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="拍照"
        android:textSize="16sp"
        android:textStyle="bold" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp">

        <LinearLayout
            android:id="@+id/check"
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:layout_centerHorizontal="true"
            android:background="@drawable/textviewborder"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="請選擇" />

            <TextView
                android:id="@+id/checkphoto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="照片"
                android:textColor="@color/colorAccent" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="/" />

            <TextView
                android:id="@+id/checkvideo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="視訊"
                android:textColor="@color/colorAccent" />
        </LinearLayout>

        <ImageView
            android:id="@+id/checkedimg"
            android:layout_width="wrap_content"
            android:layout_height="300dp"
            android:layout_centerInParent="true"
            android:visibility="invisible" />

        <VideoView
            android:id="@+id/video"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:visibility="invisible" />
    </RelativeLayout>

    <Button
        android:id="@+id/checktrue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="確定"
        android:visibility="invisible" />
</LinearLayout>

程式碼

package test.com.photo;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.annotation.MainThread;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.FileNotFoundException;

public class MainActivity extends AppCompatActivity {
    LinearLayout check;
    VideoView videoView;
    Button checktrue;
    TextView checkvideo, checkphoto;
    Uri uri;
    String name, number, path, size;
    ImageView checkedimg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控制元件
        check = findViewById(R.id.check);
        checkvideo = findViewById(R.id.checkvideo);
        checkphoto = findViewById(R.id.checkphoto);
        videoView = findViewById(R.id.video);
        checktrue = findViewById(R.id.checktrue);
        checkedimg = findViewById(R.id.checkedimg);
        checkvideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("video/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(intent, 1);
            }
        });
        checkphoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //開啟本地相簿
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                //設定結果返回
                startActivityForResult(intent, 2);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                uri = data.getData();
                Cursor cursor = getContentResolver().query(uri, null, null, null, null);
                cursor.moveToFirst();
                number = cursor.getString(0); // 視訊編號
                path = cursor.getString(1); // 視訊檔案路徑
                size = cursor.getString(2); // 視訊大小
                name = cursor.getString(3); // 視訊檔名
                cursor.close();
                //設定視訊控制器
                videoView.setMediaController(new MediaController(this));

                //播放完成回撥
                videoView.setOnCompletionListener(new MyPlayerOnCompletionListener());

                //設定視訊路徑
                videoView.setVideoURI(uri);
                //設定控制元件的顯示隱藏
                check.setVisibility(View.INVISIBLE);
                checkedimg.setVisibility(View.INVISIBLE);
                videoView.setVisibility(View.VISIBLE);
                //點選播放
                checkvideo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //開始播放視訊
                        videoView.start();
                    }
                });
            }
        }
        if (requestCode == 2) {
            if (resultCode == RESULT_OK) {
                uri = data.getData();
                Cursor cursor = getContentResolver().query(uri, null, null, null, null);
                cursor.moveToFirst();
                number = cursor.getString(0); // 圖片編號
                path = cursor.getString(1); // 圖片檔案路徑
                size = cursor.getString(2); // 圖片大小
                name = cursor.getString(3); // 圖片檔名
                cursor.close();
                //設定控制元件的顯示隱藏
                check.setVisibility(View.INVISIBLE);
                videoView.setVisibility(View.INVISIBLE);
                checkedimg.setVisibility(View.VISIBLE);
                ContentResolver cr = MainActivity.this.getContentResolver();
                try {
                    checkedimg.setImageBitmap(BitmapFactory.decodeStream(cr.openInputStream(uri)));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

            }
        }
        checktrue.setVisibility(View.VISIBLE);
        checktrue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (uri != null) {
                    Toast.makeText(MainActivity.this, "即將傳遞" + name + path + ",將浪費流量:" + size, Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this, "請選擇圖片/視訊", Toast.LENGTH_LONG).show();
                }
                //設定控制元件的顯示隱藏
                check.setVisibility(View.VISIBLE);
                checkedimg.setVisibility(View.INVISIBLE);
                videoView.setVisibility(View.INVISIBLE);
            }
        });

    }

    class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener {
        @Override
        public void onCompletion(MediaPlayer mp) {
            Toast.makeText(MainActivity.this, "播放完成了", Toast.LENGTH_SHORT).show();
        }
    }
}

textviewborder.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <solid android:color="#ffffff" />
    <stroke android:width="1dip" android:color="#000"/>
</shape>