How to convert the uri to inputStream data and upload the stream data into server?
Asked Answered
Q

1

17

I am working the task in picking the file from the gallery and upload the picked file to the server.

    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;
           }

       }
   }

I poicked the file by using intent

 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);

My issue is the uri data is converted the inputStream but i am using the ion library in that inputstream files cannot be uploaded to server. How to convert the inputStream to outputstream to save in getCacheDir().I refered this site How to get the file path for the picked files from the external storage in android?

Please help me how to upload the InputStream data into the ion library.

Quiles answered 4/5, 2017 at 5:7 Comment(9)
use InputStream inStream for reading, now you are not using itIndignity
@ pskink I don't know how to use the InputStream ..if you know means please help meQuiles
developer.android.com/reference/java/io/InputStream.html, see read() methods (of course the two with byte[] b are the variants you should use)Indignity
Then how to use the outputstreamQuiles
by reading OutputStream documentation first?Indignity
I read the outputstream but i dont know how to upload the inputstream data into serverQuiles
no, you read the inputstreamIndignity
i read the inputStreamQuiles
and what methods of OutputStream are you using to write your data?Indignity
Q
18

In the below code i have passed the uri into the inputstream and and then crated the file and inputStream data are written in outputstream. This works 100% try this method.

@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;
            }
        }
}
Quiles answered 5/5, 2017 at 12:46 Comment(2)
yes its works but copying video files won't play by any player it get corrupted.Pacificism
With Large files it will Throw OutOfMemoryExceptionCirrose

© 2022 - 2024 — McMap. All rights reserved.