I have a code here that uploads image from internal storage to server
Handler handler1 = new Handler();
Context c = data_syncing.this;
final File directory = c.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
final File[] files = directory.listFiles();
final int x = files.length;
int i;
for (i = 0; i < files.length; i++) {
final int finalI = i;
final int finalI1 = i;
final int finalI2 = i;
handler1.postDelayed(new Runnable() {
@Override
public void run() {
imgPath = directory + "/" + files[finalI].getName();
if (!imgPath.contains(".png")) {
fileName = files[finalI].getName();
image_view.setImageBitmap(BitmapFactory.decodeFile(imgPath));
image_view.buildDrawingCache();
Bitmap bitmap = ((BitmapDrawable) image_view.getDrawable()).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
byte[] image = stream.toByteArray();
ConvertImage = Base64.encodeToString(image, Base64.DEFAULT);
StringRequest stringRequest = new StringRequest(Method.POST, SYNC_IMAGES,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("image_tag", fileName.toString());
params.put("image_data", ConvertImage.toString());
return params;
}
};
VolleySingleton.getInstance(data_syncing.this).addToRequestQueue(stringRequest);
}
}
}, 1000 * i);
}
but i have multiple errors here.
Failed to allocate a 84652044 byte allocation with 4187552 free bytes and 73MB until OOM
Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
I dont know whats the problem with my code. My target here is to upload the image inside the internal storage
My Question is how can I upload my Images from internal storage to Server and also how can I check if the loop is done?
by the way here is my php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include_once('conn.php');
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$DefaultId = 0;
$ImageData = $_POST['image_data'];
$ImageName = $_POST['image_tag'];
$ImagePath = "upload/$ImageName";
$ServerURL = "=AwesomeLinkHere/$ImagePath";
$InsertSQL = "REPLACE INTO upload (image_path,imagename) values('$ServerURL','$ImageName')";
if(mysqli_query($con, $InsertSQL)){
file_put_contents($ImagePath,base64_decode($ImageData));
echo "Your Image Has Been Uploaded.";
mysqli_close($conn);
}else{
echo "Please Try Again";
}
}
?>