0

I am developing an Android app. In my app, I am uploading multiple images to server using Retrofit network library. Before I uploading file I create a temporary file from bitmaps. Then delete them after uploaded.

 photoFiles = new ArrayList<File>();
        MultipartBody.Builder requestBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        int index = 0;
        for(Bitmap bitmap: previewBitmaps)
        {
            File file = null;
            try{
                String fileName = String.valueOf(System.currentTimeMillis())+".jpeg";
                file = new File(Environment.getExternalStorageDirectory(), fileName); // create temporary file start from here
                if(file.exists())
                {
                    file.delete();
                }
                OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
                os.close();

                photoFiles.add(file);
                requestBodyBuilder.addFormDataPart("files",file.getName(), RequestBody.create(MediaType.parse("image/png"),file));
            }
            catch (Exception e)
            {
                Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
            }
            index++;
        }
//Upload process goes here and delete files back after upload

Using above code, all working fine. But the problem is I have to create temporary files. I do not want to create temporary files. What I want to do is I create array list of Uri string when I pick up the file. Then on file upload, I will convert them to file back and do the upload process.

 photoFiles = new ArrayList<File>();
        MultipartBody.Builder requestBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        int index = 0;
        for(Bitmap bitmap: previewBitmaps)
        {
            File file = null;
            try{

                Uri uri = Uri.parse(photosUriStrings.get(index));
                file = new File(getPathFromUri(uri));
                Toast.makeText(getBaseContext(),getPathFromUri(uri),Toast.LENGTH_SHORT).show();
                photoFiles.add(file);
                requestBodyBuilder.addFormDataPart("files",file.getName(), RequestBody.create(MediaType.parse("image/png"),file));
            }
            catch (Exception e)
            {
                Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
            }
            index++;
        }

As you can see in the above, I am converting the URI string back to file and then upload it. But this time retrofit unable to upload the file. File is not null as well. So I am pretty sure the error is with converting uri string back to image file back because my old code above working fine. Why can I not do that? How can I successfully convert from URI to image file back please?

I found this

Convert file: Uri to File in Android

and

Create File from Uri type android

both not working.

halfer
  • 19,824
  • 17
  • 99
  • 186
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
  • Unclear what you want. Lets start at the beginning. What is it that you have that you want to upload? Files or bitmaps or what? – greenapps Oct 17 '16 at 08:24
  • I want to upload File. But I need to show preview image. So I converted to Bitmaps. But I saved Uri strings on the result callback of file picker. – Wai Yan Hein Oct 17 '16 at 08:33
  • If you want to upload a file then upload that file. What does it matter that you have to show a preview image too? Just upload the file. Why arent yoy doing that? – greenapps Oct 17 '16 at 08:35
  • About which uri strings are you talking? – greenapps Oct 17 '16 at 08:36
  • I am not directly uploading in activity. I sent the list of Uri string to receiver and then convert back to files in receiver. Then do upload process. – Wai Yan Hein Oct 17 '16 at 08:36
  • If you have a list of uris then you can directly upload from the uri as the uri points to the file. No need to create another file or trying to convert to file path. Use the uri directly to upload the file. Very simple. You make things so complicated now. – greenapps Oct 17 '16 at 08:38
  • No uri strings. I pass to them from intent. Then retrieve as string ArrayList in receiver. Then parse back that string to Uri. Then when I create from uri to file back, this is where issue starts. – Wai Yan Hein Oct 17 '16 at 08:40
  • I asked you to tell the beginning. First you told that you wanted to upload a file. And now you tell that you have a list of uris. I still dont know with what this all begins so you have a list of uris. Where do these uris come from? – greenapps Oct 17 '16 at 08:42
  • `this is where issue starts`. No. Not there. Not after you did already so much wrong. The issue start at the moment you have a list of uris. Just use the list. – greenapps Oct 17 '16 at 09:09

1 Answers1

0

I am not clear about your question but I think this may help you. This single line code will help you to convert URI to file and show in your view.

Picasso.with(getContext()).load("URI path").into(holder.imgID);
0xAliHn
  • 18,390
  • 23
  • 91
  • 111