2017-05-04 161 views
4

我正在從圖庫中挑選文件並將拾取的文件上傳到服務器。如何將uri轉換爲inputStream數據並將流數據上傳到服務器?

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == Activity.RESULT_OK) { 
     if (requestCode == PICK_FILE_REQUEST) { 
      if (data != null) { 
       //no data present 
       Uri uri = data.getData(); 
       String filePath = data.getData().getPath(); 
     //  String path = uri.getPath(); 
       file = new File(filePath); 

       String name = getContentName(getContentResolver(), uri); 
       try { 
        InputStream inStream = getContentResolver().openInputStream(uri); 

       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } 
       try { 
        bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri); 

       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

       LinearLayout linearLayout = new LinearLayout(this); 
       linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
         LinearLayout.LayoutParams.WRAP_CONTENT)); 
       linearLayout.setOrientation(LinearLayout.VERTICAL); 

       ImageView imageView = new ImageView(this); 
       imageView.setImageBitmap(bitmap); 
       attachFile.addView(imageView); 


       TextView textView = new TextView(this); 
       textView.setText(name); 
       attachFile.addView(textView); 

       return; 
      } 

     } 
    } 

我使用意圖

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
     intent.setType("*/*"); 
     intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
     //intent.addFlags(ST) 
     startActivityForResult(Intent.createChooser(intent, "Choose File to Upload.."), PICK_FILE_REQUEST); 

我的問題是URI數據轉換InputStream的,但我使用該文件的InputStream離子庫不能被上傳到服務器poicked文件。 如何將inputStream轉換爲outputstream以保存在getCacheDir()中我引用此網站How to get the file path for the picked files from the external storage in android?

請幫助我如何將InputStream數據上載到離子庫中。

+0

使用'的InputStream inStream'閱讀,現在你不使用它 – pskink

+0

@ pskink我不知道如何使用InputStream的..如果你知道方法請幫我 –

+0

https://developer.android請參閱'read()'方法(當然這兩個方法是'byte [] b''是你應該使用的變體) – pskink

回答

3

在下面的代碼中,我已經將uri傳入輸入流,然後將文件和輸入流數據寫入輸出流。 這工作100%嘗試這種方法。

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == Activity.RESULT_OK) { 
      if (requestCode == PICK_FILE_REQUEST) if (data != null) { 
       //no data present 
       Uri uri = data.getData(); 
       String filePath = data.getData().getPath(); 

       String name = getContentName(getContentResolver(), uri); 
      File file = new File(getCacheDir(),name); 

       int maxBufferSize = 1 * 1024 * 1024; 

       try { 
        InputStream inputStream = getContentResolver().openInputStream(uri); 
        Log.e("InputStream Size","Size " + inputStream); 
        int bytesAvailable = inputStream.available(); 
//     int bufferSize = 1024; 
        int bufferSize = Math.min(bytesAvailable, maxBufferSize); 
        final byte[] buffers = new byte[bufferSize]; 

        FileOutputStream outputStream = new FileOutputStream(file); 
        int read = 0; 
        while ((read = inputStream.read(buffers)) != -1) { 
         outputStream.write(buffers, 0, read); 
        } 
        Log.e("File Size","Size " + file.length()); 
        inputStream.close(); 
        outputStream.close(); 

        file.getPath(); 
        Log.e("File Path","Path " + file.getPath()); 
        file.length(); 
        Log.e("File Size","Size " + file.length()); 

        if(file.length() > 0){ 
         attachementImage.setVisibility(View.INVISIBLE); 
        } 


       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       try { 
        bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri); 

       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       imageView.setImageBitmap(bitmap); 
       attachFile.addView(imageView); 

       attachFile.addView(textView); 

       return; 
      } 
     } 
} 
+0

是的,但它的作品,但複製視頻文件不會播放任何球員它會被損壞。 – androidXP

+0

你有沒有用SD卡測試這個代碼android 7.0? – Hamza

相關問題