Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
直接从Android活动下载图像_Android_Download - Fatal编程技术网

直接从Android活动下载图像

直接从Android活动下载图像,android,download,Android,Download,我在我的应用程序中显示图像,我想在每个图像后添加下载按钮,当用户点击它时,图像将自动保存到文件夹中。有可能吗?您可以使用毕加索库来显示图像。在build.gradle依赖项中添加以下代码: compile 'com.squareup.picasso:picasso:2.4.0' 现在使用它来显示图像,您可以在按钮的onClick方法中添加此代码 File file = new File(imagePath); if(file.exists()) { Picasso.with(conte

我在我的应用程序中显示图像,我想在每个图像后添加下载按钮,当用户点击它时,图像将自动保存到文件夹中。有可能吗?

您可以使用毕加索库来显示图像。在build.gradle依赖项中添加以下代码:

compile 'com.squareup.picasso:picasso:2.4.0'
现在使用它来显示图像,您可以在按钮的onClick方法中添加此代码

File file = new File(imagePath);
if(file.exists()) {
    Picasso.with(context).load(file).skipMemoryCache().placeholder(R.drawable.placeholder).into(yourImageView);
}
else {
    Picasso.with(context).load(imageUrl).skipMemoryCache().placeholder(R.drawable.placeholder).into(yourImageView, new PicassoCallBack(yourImageView,imagePath));
}
picassoCallBack类将如下所示:

public class PicassoCallBack extends Callback.EmptyCallback {
    ImageView imageView;
    String filename;
    public PicassoCallBack(ImageView imageView, String filename) {
        this.imageView = imageView;
        this.filename = filename;
    }
    @Override public void onSuccess() {
        // Log.e("picasso", "success");
        Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
        try {
            ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos1);
            // FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
            File file = new File(filename);
            FileOutputStream outStream = new FileOutputStream(file);
            outStream.write(baos1.toByteArray());
            outStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onError() {
        Log.e("picasso", "error");
    }
}

希望它能完成您的工作。

如果您的应用程序中已经保存了该文件,请将其复制到此公用文件夹 文件imagePath=Environment.getExternalStoragePublicDirectoryEnvironment.DIRECTORY\u图片;

然后使用提供的技术将图片扫描到图库中。现在,当用户打开图库时,他们将看到图片。

您可以使用Glide下载图像,我认为它比毕加索更好,因为它扩展了毕加索。有关更多信息,请参阅。
private static void persistImage(Bitmap bitmap, String name) {
    File filesDir = getAppContext().getFilesDir();
    File imageFile = new File(filesDir, name + ".jpg");

    OutputStream os;
    try {
        os = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
        os.flush();
        os.close();
    } catch (Exception e) {
        Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
    }
}
为此,您只需将compile'com.github.bumptech.glide:glide:3.6.1'包含到依赖项中,然后简单地添加此代码行

Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView);`
在哪里http://goo.gl/gEgYUd 是要传递的URL。使用此URL后,您不必维护缓存

享受你的代码:

我已经试过了。 你知道你需要巫婆,一个就足够了。 还要有更好的决策和阅读。 它可能会帮助您:


这会允许用户下载图片吗?不,你应该自己下载,如果你需要的话,我也可以提供代码。但是你不是已经有了httpGet的代码吗?他的问题是关于下载图片而不是显示它们。他不想在图片加载时下载。他想把它下载到用户的手机上。如果用户按下下载按钮,上面的代码就可以了。如果图像存在于imagePath中,则它将在imageview中显示,否则它将下载并保存到imagePath,然后在imageview中显示。piccasoCallBack类用于下载并将其保存到imagePath。仅当图像不在该路径中时才会调用它。图像是来自internet还是在你的应用程序中?这些图像来自何处?它是来自服务器还是您的应用程序?