【问题标题】:How to save the image contained in imageView to internal storage with Picasso如何使用 Picasso 将 imageView 中包含的图像保存到内部存储
【发布时间】:2021-03-09 07:12:25
【问题描述】:

好的,我在sn-p中运行

我有一个 imageView,它使用 picasso 随机获取其图像,我希望您帮助我使用按钮将该图像保存到设备的内部存储中

我生成随机图像的代码:


private fun imageRandomFun() {
        val quest1 = "https://proxxcraft.com/"
        Picasso.get().load(quest1).memoryPolicy(MemoryPolicy.NO_CACHE).into(memeRandomView)
        Picasso.get().isLoggingEnabled = true
    }

我的布局:


        <ImageView
            android:id="@+id/memeRandomView"
            android:layout_width="385dp"
            android:layout_height="452dp"
            android:layout_marginStart="32dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="32dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.578"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView8" />

        <Button
            android:id="@+id/nextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="40dp"
            android:clickable="true"
            android:text="Next"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/memeRandomView" />

        <TextView
            android:id="@+id/textView8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="32dp"
            android:text="Memes Random"
            android:textColor="#393636"
            android:textSize="20dp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/downloadButton"
            style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton.Icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="16dp"
            android:text="Descargar"
            app:icon="@drawable/download"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/memeRandomView" />

【问题讨论】:

  • 您尝试过与存储数据相关的任何事情吗?或者您只是提供了一个基本布局,寻找可以为您编写完整功能的人。只要问任何搜索引擎,HERE 你就有答案...
  • 事实上,我不会让自己发布所有代码,并且在发布之前我进行了配置,以便应用程序请求必要的权限以继续保存图像,但我不知道如何保存图像用毕加索生成

标签: android kotlin


【解决方案1】:

试试下面的,因为图片保存代码太长了,你可以使用这个库更方便你implementation 'com.blankj:utilcodex:1.30.0'

确保已授予存储权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

private fun downloadImage(){
    val quest1 = "https://proxxcraft.com/"
    Picasso.get().load(quest1).into(object : Target {
        override fun onBitmapLoaded(bitmap: Bitmap, from: LoadedFrom) {
            val file = ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.JPEG, 100)
            if (file!!.exists()) ToastUtils.showShort("Image Saved!")
        }
        override fun onBitmapFailed(e: Exception, errorDrawable: Drawable) {}
        override fun onPrepareLoad(placeHolderDrawable: Drawable) {}
    })
}

当图片加载完毕后,onBitmapLoaded 方法返回一个Bitmap,你可以将它作为文件保存到你的设备存储中

【讨论】:

  • 无论出于何种原因,您提供给我的代码的 kotlin 转换失败,Picasso.get().load(quest1).memoryPolicy(MemoryPolicy.NO_CACHE).into (object: Target()因为它没有检测到必须进入 into () 的 ImageView
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-28
  • 2021-06-02
  • 1970-01-01
  • 2013-10-28
  • 2015-10-24
相关资源
最近更新 更多