Android--多媒體操作

來源:互聯網
上載者:User

標籤:

---恢複內容開始---

1、拍照,這裡直接上代碼,看注釋就好

public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    takePhoto = (Button) findViewById(R.id.take_photo);    photo = (ImageView) findViewById(R.id.photo);    takePhoto.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View view) {            File outputImage = new File(Environment.getExternalStorageDirectory(), "tempImage.jpg");//這裡建立一個檔案類,該檔案用於儲存拍照所得的圖片,getExternalStorageDirectory(),表示存在手機SD卡根目錄            try{                if(outputImage.exists()){                    outputImage.delete();                }                outputImage.createNewFile();            }catch (IOException e){                e.printStackTrace();            }            imageUri = Uri.fromFile(outputImage);//將檔案轉換Uri對象            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");//啟動照相機程式的action            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//指定拍照後圖片輸出地址為前面建立的Uri對象            startActivityForResult(intent, TAKE_PHOTO);//啟動照相機        }     });}

  --檔案儲存體需要由許可權:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2、相簿選擇照片並顯示:

choosePhoto = (Button)findViewById(R.id.choose_photo);choosePhoto.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View view) {        Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//開啟相簿並,返回選中照片的Uri        startActivityForResult(intent, CHOOSE_PHOTO);    }});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    switch(requestCode){        case CHOOSE_PHOTO:            if(resultCode == RESULT_OK){                try{                    Uri uri = data.getData();//擷取選中圖片的Uri                    Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));//根據Uri載入圖片為Bitmap                    photo.setImageBitmap(bitmap);                }catch (FileNotFoundException e){                    e.printStackTrace();                }            }            break;    }}    

--上述操作中,由於選中的圖片可能會很大,通常需要壓縮後再進行顯示;

3、音頻播放

  1)Android 中通過使用 MediaPlayer 類來實現音頻播放,它的常用控制方法有:

    --setDataSource():設定要播放的音頻檔案的位置;

    --prepare():在開始播放之前調用這個方法完成準備工作;

    --start():開始或繼續播放音頻;

    --pause():暫停播放音頻;

    --reset():將MediaPlayer 對象重設到剛剛建立的狀態;

    --seekTo():從指定的位置開始播放音頻;

    --stop():停止播放音頻。調用這個方法後的MediaPlayer 對象無法再播放音頻

    --release():釋放掉與MediaPlayer 對象相關的資源;

    --isPlaying():判斷當前MediaPlayer 是否現正播放音頻;

    --getDuration():擷取載入的音頻檔案的時間長度;

4、播放視頻:

  1)Android 中通過 VideoView 類來實現視頻的操作,它的常用方法有:

    --setVideoPath():設定要播放的視頻檔案的位置;

    --start():開始或繼續播放視頻;

    --pause():暫停播放視頻;

    --resume():將視頻重頭開始播放;

    --seekTo():從指定的位置開始播放視頻;

    --isPlaying():判斷當前是否現正播放視頻;

    --getDuration():擷取載入的視頻檔案的時間長度;

    --suspend():釋放視頻資源;

Android--多媒體操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.