2013-03-27 28 views
23

tôi có tệp video trong thư mục bên ngoài của mình. làm thế nào tôi có thể tải nó vào biến inputstream. Hiện tại tôi đang đọc tệp trong thư mục res/raw nhưng tôi muốn đọc nó từ sdcard. tôi cũng không biết về tên của tập tin nhưng đường dẫn của nó sẽ được nhận thông qua ý định. kiểm tra mã sauTải tệp từ bộ nhớ ngoài vào Inputstream

public class SonicTest extends Activity 
{ 
VideoView videoView; 
String uri; 
InputStream soundFile; 
File file; 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState);      
    setContentView(R.layout.main); 

} 

public void play(View view) 
{ 
    new Thread(new Runnable() 
    { 
     public void run() 
     { 
      float speed= (float) 1.0; 
      float pitch= (float) 1.5; 
      float rate= (float) 1.0; 
      uri= Environment.getExternalStorageDirectory().toString(); 
      uri=uri+"/video.3gp"; 
      AndroidAudioDevice device = new AndroidAudioDevice(22050, 1); 
      Sonic sonic = new Sonic(22050, 1); 
      byte samples[] = new byte[4096]; 
      byte modifiedSamples[] = new byte[2048]; 
      InputStream soundFile = null; 

    //soundFile = getContentResolver().openInputStream(Uri.parse(uri)); 
    soundFile=getResources().openRawResource(R.raw.video3); 
    Log.i("testing","check if SoundFile is correct "+soundFile); 

      int bytesRead; 

      if(soundFile != null) { 
       sonic.setSpeed(speed); 
       sonic.setPitch(pitch); 
       sonic.setRate(rate); 
       do { 
        try { 
         bytesRead = soundFile.read(samples, 0, samples.length); 
        } catch (IOException e) { 
         e.printStackTrace(); 
         return; 
        } 
        if(bytesRead > 0) { 
         sonic.putBytes(samples, bytesRead); 
        } else { 
         sonic.flush(); 
        } 
        int available = sonic.availableBytes(); 
        if(available > 0) { 
         if(modifiedSamples.length < available) { 
          modifiedSamples = new byte[available*2]; 
         } 
         sonic.receiveBytes(modifiedSamples, available); 
         device.writeSamples(modifiedSamples, available); 
        } 
       } while(bytesRead > 0); 
       device.flush(); 
      } 
     } 
    }).start(); 
}} 
+0

Cả ba câu trả lời không hiểu những gì một URI là gì và rằng họ không phải luôn luôn là một đường dẫn tập tin. Nếu bạn có đường dẫn tệp bạn đã tự tìm ra, nhưng câu trả lời được đăng không trả lời câu hỏi thực tế. – BooleanCheese

Trả lời

54

Hãy thử

File file = new File(Uri.toString()); 
FileInputStream fileInputStream = new FileInputStream(file); 

Sau đó, bạn có thể đọc từ stream.

+0

cũng nhờ hoạt động của nó. nhưng tôi không thể ở đây bất cứ điều gì có quá nhiều tiếng ồn. Bất kỳ ý tưởng tại sao nó như thế này? –

+6

Lưu ý: câu trả lời này chỉ hoạt động cho câu hỏi của OP (OP có một uri là một đường dẫn tập tin). Trong trường hợp tổng quát hơn, Uri.toString() không nhất thiết trả về một đường dẫn tệp (uri có thể thay vào đó là một đường dẫn nội dung, hoặc url, v.v.). Phương thức này chỉ hoạt động cho Uri, đó là đường dẫn tệp. kiểm tra câu trả lời cho câu hỏi này: http://stackoverflow.com/questions/5657411/android-getting-a-file-uri-from-a-content-uri –

+1

Câu hỏi đặt ra là "Tải tệp từ bộ nhớ ngoài". Tôi nghĩ rằng giả định uri đến là một tập tin được an toàn trong trường hợp đó. –

0
soundFile = openFileInput(uri); 
6
String fileName = "OfflineMap/maps.xml"; 
String path = Environment.getExternalStorageDirectory()+"/"+fileName; 
File file = new File(path); 
FileInputStream fileInputStream = new FileInputStream(file);