android – 如何将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转换为outputstream以保存在getCacheDir()中.我引用了这个站点How to get the file path for the picked files from the external storage in android?

请帮我如何将InputStream数据上传到离子库.

最佳答案
在下面的代码中,我已经将uri传递到输入流,然后创建文件,并将inputStream数据写入outputstream.
这100%尝试这种方法.

@Override
    protected void onActivityResult(int requestCode,Intent data) {
        super.onActivityResult(requestCode,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,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;
            }
        }
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...