How to select multiple device images and upload them?

Asked

Viewed 130 times

0

How to pick multiple images from what user select and send to mysql database by php?

The most that got was a single image, but the user can select several on android and wanted to send to online repository and save their directories at the same time, is possible?

  • Dear Robert phpmyadmin has nothing to do with all this: https://answall.com/a/115692/3635

  • Using what? Java with the pure Android API or using something else or lib the most? Give details, there is no unique way to do something.

  • Edited friend, I will post the code on which you managed to capture only one image.

1 answer

0


To select multiple images use:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(intent, "Selecione suas fotos"), SELECT_PICTURES);

As described in the documentation:

And to get the photos you must use the https://developer.android.com/reference/android/content/Intent#getClipData()

And to get the selected photos use:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultIntent)
{
    super.onActivityResult(requestCode, resultCode, resultIntent);

    if(requestCode == SELECT_PICTURES && resultCode == Activity.RESULT_OK && data.getClipData() != null) {
        int j = data.getClipData().getItemCount();

        for (int i = 0; i < j; i++)  
            Uri imageUri = data.getClipData().getItemAt(i).getUri();

            //Faça o upload aqui
        }
    }
}

This will upload one by one. of course the HTTP and your server part is up to you, because it depends on what you did.

Vale remembers that there is the https://square.github.io/retrofit/ that can help you work with sending, if you have little experience with HTTP (most people spend years and don’t really master this, because it’s more complex than the word goes around)

So if you were to use Retrofit, you could get the images like this:

List<MultipartBody.Part> uploads = new ArrayList<>();

int j = data.getClipData().getItemCount();

for (int i = 0; i < j; i++)  
    File file = new File(data.getClipData().getItemAt(i).getUri());

    RequestBody requestFile = RequestBody.create(MEDIA_TYPE_IMG, file);

    uploads.add(MultipartBody.Part.createFormData("foto[]", file.getName(), requestFile));
}

Note that I applied foto[], but I didn’t really test the "Retrofit 2", if there is something wrong let me know.

  • Friend, whenever I select a single image this returning null the result

  • @robertsouzalopesify back NULL where exactly? On what part did you accuse NULL? Was it a nullpointerexception or was it NULL itself? Explain

  • Friend, I used your suggestion Retrofit was excellent

  • @sourobertzalopesify that good that solved the problem, mark the answer as correct, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.