I can use camera capture of images on iPhone and send to PHP and this works fine. However, I have run into issues trying to do the same on Android.
I will provide code below but here is the logic. I use FileProvider in the Activity, manifest.xml and then in a Fragment. I create an image file Uri and then an intent for new Intent(MediaStore.ACTION_IMAGE_CAPTURE).
In the onActivityResult I use the Uri of file to get a Bitmap. I use compress(Bitmap.CompressFormat.JPEG,70,stream) to get a jpeg in a ByteArrayOutputStream().
I then attach this ByteArrayOutputStream() to text using toString and send it to PHP on server using a HttpURLConnection and BufferedWriter.
In the PHP I detach the jpeg from the text using substr and save to a file and it says JIFI in beginning but the file is not a valid jpeg. Using getimagesize returns null.
While I am sure this should be done multipart, but doing this on iPhone works fine. I don't think what I am doing on PHP is wrong. I suspect I am not sending the right form of image to PHP.
When I encode before sending in Android it looks like an encoded jpeg.
Here is the code. I have not posted in a very long time so please be patient and kind.
This is the File Provider
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.afs.genapp"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
This is Intent and I know I have issue with requesting permission
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
photoURI = FileProvider.getUriForFile(context,
"com.afs.genapp",
photoFile);
if (ContextCompat.checkSelfPermission(
context,
Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED
) {
// permission granted;
} else {
// permission not granted
}
int ALL_PERMISSIONS = 101;
final String[] permissions = new String[] {Manifest.permission.CAMERA};
final String[] requestPermissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (!ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.CAMERA)) {
ActivityCompat.requestPermissions(getActivity(),permissions, ALL_PERMISSIONS);
}
if (!ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
ActivityCompat.requestPermissions(getActivity(),requestPermissions, ALL_PERMISSIONS);
}
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
// startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
Here is the onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case IntentIntegrator.REQUEST_CODE:
IntentResult result = IntentIntegrator.parseActivityResult(resultCode, data);
if (result.getContents() != null) {
System.out.println("Into afsConfigure 1" + result.toString());
scanView.QF.setText(result.getContents());
} else {
scanView.QF.setText("Click Above to Scan");
}
break;
case REQUEST_IMAGE_CAPTURE:
System.out.println("Into REQUEST_IMAGE_CAPTURE");
// if (resultCode==Activity.RESULT_OK && data!=null)
if (resultCode==Activity.RESULT_OK)
{
try {
// Bitmap bitmap = (Bitmap) data.getExtras().get("data");
// file = new File(currentPhotoPath);
Bitmap bitmap= MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), photoURI);
// initialize byte stream
//ByteArrayOutputStream stream=new ByteArrayOutputStream();
stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,70,stream);
sImage = stream.toString();
camView.CF.setText("Success");
} catch (IOException e) {
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
}
break;
}
}
Here is how I attach it to the text:
if(sImage!= null)
passit += sImage;
This is the Http
@Override
protected String doInBackground(String... Strings) {
try {
URL urlObj = new URL(Strings[0]);
HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "multipart/form-data");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Accept", "image/jpeg");
conn.setRequestProperty("Accept-Charset", "UTF-8");
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
wr.write(passit);
wr.flush();
wr.close();
conn.connect();
System.out.println("Response" + conn.getResponseMessage());
} catch (Exception e) {
}
return "done";
}
}
}