I successfully added my image to my server with the help of this https://androidjson.com/android-upload-image-server-using-php-mysql/. But it only helps in uploading the image to the server. I have to display the image I have uploaded to the image view. I tried Picasso but it's not working.
Picasso.with(context).load(ImagePath).into(image);
I don't know what should be written in the load method. Because I was told to write the path and name of the image but none is working. I added it in PostExecute method.But it is giving me error of
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.prajakta.truckloaderowner, PID: 32651
java.lang.OutOfMemoryError: Failed to allocate a 4192268 byte allocation with 3077040 free bytes and 2MB until OOM
at java.io.ByteArrayOutputStream.toByteArray(ByteArrayOutputStream.java:122)
at com.prajakta.truckloaderowner.FragProfile.ImageUploadToServerFunction(FragProfile.java:137)
at com.prajakta.truckloaderowner.FragProfile$2.onClick(FragProfile.java:93)
at android.view.View.performClick(View.java:5721)
at android.widget.TextView.performClick(TextView.java:10949)
at android.view.View$PerformClick.run(View.java:22624)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7410)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
This my PHP file
<?php
$username ='root';
$password ='';
$hostname ='localhost';
$database ='truck_loader';
$con=mysqli_connect($hostname,$username,$password,$database) or die("unable
to connect");
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$DefaultId = 0;
$ImageData = $_POST['image_path'];
$ImageName = $_POST['image_name'];
$GetOldIdSQL ="SELECT id FROM users ORDER BY id ASC";
$Query = mysqli_query($con,$GetOldIdSQL);
while($row = mysqli_fetch_array($Query))
{
$DefaultId = $row['id'];
}
$ImagePath = "images/$DefaultId.png";
$ServerURL = "$ImagePath";
$InsertSQL = "insert into users (image_path,image_name) values
('$ServerURL','$ImageName')" ;
if(mysqli_query($con, $InsertSQL))
{
file_put_contents($ImagePath,base64_decode($ImageData));
echo "Your Image Has Been Uploaded.";
}
mysqli_close($con);
}
else
{
echo "Not Uploaded";
}
?>
Edit:I am designing a profile page for an app so I need the photo to remain there until the changes are done. But according to the code, it only stays there until back button is pressed.After back press image is gone.