2

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.

  • Try this https://stackoverflow.com/questions/32244851/androidjava-lang-outofmemoryerror-failed-to-allocate-a-23970828-byte-allocatio/32245018 – sak Mar 21 '18 at 09:43

4 Answers4

1

The problem is that Picasso loads the image on memory with its original size so if the image is large it will throw OutOfMemoryException

You can use Glide which is different and loads the image with the wanted size of the target imageView

https://github.com/bumptech/glide

1
 compile 'com.github.bumptech.glide:glide:3.5.2'

Glide.with(context).load("YOUR IMAGE PATH").diskCacheStrategy(DiskCacheStrategy.NONE).into(videoThumb);

//videoThumb is where you want to set image ex. imageView

Vinesh Chauhan
  • 1,288
  • 11
  • 27
1

User resize() from Picasso.

Picasso.with(context).load(ImagePath).resize(150,150).into(image);

you can change 150 to your desire size.

Dhrumil Shah - dhuma1981
  • 15,166
  • 6
  • 31
  • 39
1

For Out of memory error you can use one of the following solution 1. You can add large heap to app level gradle file

android {
dexOptions {
javaMaxHeapSize "4g"
}
}

2. You can add android:largeHeap="true" in application tag in in manifest file

<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">

3. Picasso out of memory error also minimize by crop and fit the imageview while loading so you can use below code for image load

Picasso.with(context).load(yourimageUrl).fit().centerCrop()
    .into(yourimageView);
chetan prajapat
  • 311
  • 1
  • 9