android調用本地錄製程式擷取錄製檔案路徑的問題(摘)

來源:互聯網
上載者:User

I use the MediaStore.ACTION_VIDEO_CAPTURE intent class to capture the video, the video stored in default location(gallery),i want to store the video in specific location with specific name.
I use MediaStore.EXTRA_MEDIA_TITLE and MediaStore.EXTRA_MEDIA_OUTPUT but I don`t get the video at correct location, at least I need the path of recorded video.
 方案一:
Trick is to insert media into database before recording:
String fileName = "captureTemp.mp4";  
ContentValues values = new ContentValues();  
values.put(MediaStore.Video.Media.TITLE, fileName);  
cameraVideoURI = getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);  
 
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);  
intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraVideoURI);  
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, MAXIMUM_VIDEO_SIZE);               
startActivityForResult(intent, CAPTURE_VIDEO_INTENT);
and then onActivityResult() use saved cameraVideoUri to reference recorded video:
       String[] projection = { MediaStore.Video.Media.DATA, MediaStore.Video.Media.SIZE  }; 
       Cursor cursor = managedQuery(cameraVideoURI, projection, null, null, null); 
       int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA); 
       int column_index_size = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE); 
       cursor.moveToFirst(); 
       String recordedVideoFilePath = cursor.getString(column_index_data);
       int recordedVideoFileSize = cursor.getInt(column_index_size);
方案二:
The solution from Zelimir doesn't work in my case (the videos were at the right location but had a size of zero bytes). So i've found another solution:
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
  super.onActivityResult(requestCode, resultCode, intent);
 
  if (resultCode != RESULT_OK) return;
 
  try {
    AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(intent.getData(), "r");
    FileInputStream fis = videoAsset.createInputStream();
    File tmpFile = new File(Environment.getExternalStorageDirectory(),"VideoFile.3gp"); 
    FileOutputStream fos = new FileOutputStream(tmpFile);
 
    byte[] buf = new byte[1024];
    int len;
    while ((len = fis.read(buf)) > 0) {
        fos.write(buf, 0, len);
    }       
    fis.close();
    fos.close();
  } catch (IOException io_e) {
    // TODO: handle error
  }
}
The MediaStore.EXTRA_OUTPUT and the Uri aren't necessary in this case.

作者“fxrz12”
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.