【问题标题】:Image not saved to storage when loading with picasso使用毕加索加载时图像未保存到存储中
【发布时间】:2018-08-16 21:16:44
【问题描述】:

我有一个使用 Django rest 框架将其数据库与 Django 服务器同步的应用程序。我可以将图像上传到服务器。

问题是图像是从其他设备上传还是直接通过网络应用上传。当我尝试使用“since”过滤器重新同步时,它显示它正在调用获取图像(不是在当前设备上制作的),但它永远不会被保存。我发现修复它的唯一方法是清除 android 缓存并开始与服务器的初始同步。

我正在使用 Picasso 将图像加载到新目标中:

private void saveImage(final String url){
    Picasso.get().load(url).into(new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            try {
                String root = Environment.getExternalStorageDirectory().toString();
                File directory = new File(root + "/instrumentImages");
                if (!directory.exists()) {
                    directory.mkdirs();
                }
                String fileName = url.substring( url.lastIndexOf('/')+1, url.length() );
                //String fileNameWithoutExtn = fileName.substring(0, fileName.lastIndexOf('.'));
                directory = new File(directory, fileName);
                FileOutputStream out = new FileOutputStream(directory);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                out.flush();
                out.close();
            } catch(Exception e){
                Log.v("ActionSync", "Failed to save or load image.");
            }
        }

我不明白为什么如果我获取 URL,它就无法保存图像。如果我不清除缓存,我会得到 304 的 HTTP 状态。 在 Django 测试服务器上,它显示: /media/instrument_image/1534360416819.jpg HTTP/1.1" 200 305999 举个例子。

【问题讨论】:

    标签: android django django-rest-framework synchronization picasso


    【解决方案1】:

    许可呢?您正在尝试保存 externalStorage,我遇到了同样的问题,这是权限问题。我首先推荐:

    boolean create = File directory = new File(root + "/instrumentImages");
    

    这是真的吗?如果没有,您可以通过手机或模拟器设置中的 Settings/Apps&Notifications/YourApp/Permissions 向您授予应用存储权限。

    【讨论】:

    • 我相信该目录存在,因为我正在通过android设备监视器查看它。初始同步的所有图像都在里面。此外,该应用还具有“修改或删除您的 SD 卡内容”、“读取您的 SD 卡内容”的权限
    【解决方案2】:

    我已经解决了这个问题!

    问题在于毕加索对目标的引用很弱。在加载图像之前,它将被垃圾收集。

    我使用 Glide 将图像加载到 SimpleTarget 中解决了这个问题:

    private void saveImage(final String url){
        Glide.with((Context)activity).asBitmap().load(url).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                try {
                    String root = Environment.getExternalStorageDirectory().toString();
                    File directory = new File(root + "/instrumentImages");
                    if (!directory.exists()) {
                        directory.mkdirs();
                    }
                    String fileName = url.substring(url.lastIndexOf('/') + 1, url.length());
                    //String fileNameWithoutExtn = fileName.substring(0, fileName.lastIndexOf('.'));
                    directory = new File(directory, fileName);
                    FileOutputStream out = new FileOutputStream(directory);
                    resource.compress(Bitmap.CompressFormat.JPEG, 100, out);
                    out.flush();
                    out.close();
                } catch (Exception e) {
                    Log.v("ActionSync", "Failed to save or load image.");
                }
            }
    
            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {
                super.onLoadFailed(errorDrawable);
                Log.v("ActionSync", "Failed to load image.");
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      • 2015-12-24
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多