显示从其图像路径上传的图像

时间:2018-03-21 09:40:56

标签: android image-uploading picasso

我在https://androidjson.com/android-upload-image-server-using-php-mysql/的帮助下成功将我的图片添加到了我的服务器。但它只会将图像上传到服务器。我必须显示我上传到图像视图的图像。我试过毕加索,但它不起作用。

Picasso.with(context).load(ImagePath).into(image);

我不知道在load方法中应该写什么。因为我被告知要写出图像的路径和名称,但没有一个正在工作。 我在PostExecute方法中添加了它。但它给了我错误

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)

这是我的PHP文件

<?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";
  }
 ?>

编辑:我正在为应用设计个人资料页面,因此我需要将照片保留在那里,直到更改完成。但是根据代码,它只会停留在那里,直到按下后退按钮。后面的按下图像消失了。

4 个答案:

答案 0 :(得分:1)

问题是Glide以原始大小加载内存中的图像,因此如果图像很大,则会抛出imageView

您可以使用不同的Stage并将图片加载到目标<div>的所需大小

https://github.com/bumptech/glide

答案 1 :(得分:1)

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

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

// videoThumb是您要设置图像的位置。 ImageView的

答案 2 :(得分:1)

毕加索的用户调整大小()。

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

你可以改变150到你想要的大小。

答案 3 :(得分:1)

对于内存不足错误,您可以使用以下解决方案之一 1.您可以将大堆添加到应用程序级别的gradle文件

android {
dexOptions {
javaMaxHeapSize "4g"
}
}

2。你可以添加android:largeHeap =&#34; true&#34;在清单文件中的应用程序标记中

<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内存不足错误也会因裁剪而最小化,并在加载时适合imageview,因此您可以使用下面的代码进行图像加载

Picasso.with(context).load(yourimageUrl).fit().centerCrop()
    .into(yourimageView);
相关问题