Uploading a photo into WebView site directly from camera

+3 Monty Mason · May 30, 2016
Note: I have looked at whatever other questions I could on here and StackOverflow and have not come across a fully working solution to this problem. 

Been trying to learn how to upload a photo in a webview that is running a practice site URL (ie. http://postimage.org/).

Problem is, I can upload a photo just fine from the gallery/documents. When I take a picture with a camera, I can't seem to upload it. Nothing shows up in the "Browse" field after verifying a pic to upload.

I know a file is being created because I can see it in my physical device that I am testing on. It is just a matter of getting that file to the browse field that I can't figure out.

Important bits from the code are below (full code can be added if needed): 

Create Image File code:


private File createImageFile() throws IOException {
   // Create an image file name
   String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
   String imageFileName = "JPEG_" + timeStamp + "_";
   File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
   File imageFile = File.createTempFile(
           imageFileName,  /* prefix */
           ".jpg",         /* suffix */
           storageDir      /* directory */
   );

   return imageFile;
}


Code to access camera or gallery:


public class PQChromeClient extends WebChromeClient {

   // For Android 5.0
   public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> filePath, WebChromeClient.FileChooserParams fileChooserParams) {
       // Double check that we don't have any existing callbacks
       if (mFilePathCallback != null) {
           mFilePathCallback.onReceiveValue(null);
       }
       mFilePathCallback = filePath;

       Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

       if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
           // Create the File where the photo should go
           File photoFile = null;
           try {
               photoFile = createImageFile();
               takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
           } catch (IOException ex) {
               // Error occurred while creating the File
               Log.e(TAG, "Unable to create Image File", ex);
           }

           // Continue only if the File was successfully created
           if (photoFile != null) {
               mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
               takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                       Uri.fromFile(photoFile));
           } else {
               takePictureIntent = null;
           }
       }

       Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
       contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
       contentSelectionIntent.setType("image/*");

       Intent[] intentArray;
       if (takePictureIntent != null) {
           intentArray = new Intent[]{takePictureIntent};
       } else {
           intentArray = new Intent[0];
       }

       Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
       chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
       chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
       chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);

       startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE);

       return true;
   }
}


On Activity Result Code:


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

       if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) {
           super.onActivityResult(requestCode, resultCode, data);
           return;
       }

       Uri[] results = null;

       // Check that the response is a good one
       if (resultCode == MainActivity.RESULT_OK) {
           if (data == null) {
               // If there is not data, then we may have taken a photo
               if (mCameraPhotoPath != null) {
                   results = new Uri[]{Uri.parse(mCameraPhotoPath)};
               }
           } else {
               String dataString = data.getDataString();
               if (dataString != null) {
                   results = new Uri[]{Uri.parse(dataString)};
               }
           }
       }

       mFilePathCallback.onReceiveValue(results);
       mFilePathCallback = null;

   } else if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
       if (requestCode != FILECHOOSER_RESULTCODE || mUploadMessage == null) {
           super.onActivityResult(requestCode, resultCode, data);
           return;
       }

       if (requestCode == FILECHOOSER_RESULTCODE) {
           if (null == this.mUploadMessage) {
               return;
           }

           Uri result = null;

           try {
               if (resultCode != RESULT_OK) {
                   result = null;
               }
               else {
                   // Retrieve from the private variable if the intent is null
                   result = data == null ? mCapturedImageURI : data.getData();
               }
           }
           catch (Exception e) {
               Toast.makeText(getApplicationContext(), "activity :" + e,
                       Toast.LENGTH_LONG).show();
           }
           mUploadMessage.onReceiveValue(result);
           mUploadMessage = null;
       }
   }
   return;
}


So above is the relevant code I believe. If you need the full code, let me know and I'll post it.

PS: The tutorial I followed was this one. https://www.opengeeks.me/2015/08/filechooser-and-android-webview/

Thanks :).

Post a Reply

Replies

Oldest  Newest  Rating
0 Monty Mason · May 31, 2016
@B S, thanks. I will look into the resources you provided.
0 Ahsan Shahid · April 23, 2017
How to upload photo ???
  • 1

Java / Android Development

126,496 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator