无法将图片保存在Firebase的本地存储中,并且无法在图库中看到

时间:2018-08-30 08:19:35

标签: android firebase picasso

我正在使用Picasso从Firebase数据库加载图像。 现在,我想将其保存在本地存储中。 但这不起作用。我已经尝试过其他关于StackOverflow的帖子,但是它们都无法正常工作。 其中一些存在运行时权限问题。P租赁帮助。 所以我需要将图像保存在本地存储中,以便用户可以在图库中找到它,并且应该在图库中可见 我已添加了在外部设备上进行读写的权限

  

代码不起作用,尽管说图像已保存,但未将图像保存在本地存储中,

public class SingleImageActivity extends AppCompatActivity {
    String imageurl;
    ImageView img;
    Button download,share;
    private Target target;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_image);
        imageurl = getIntent().getStringExtra("imageurl");
         img=findViewById(R.id.image);
         download=findViewById(R.id.download);
         share=findViewById(R.id.share);

        Picasso
                .with(getApplicationContext())
                .load(imageurl)
                .into(img);


        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getBaseContext(), "Download Started...", Toast.LENGTH_SHORT).show();
                boolean success = true;
                File wallpaperDirectory = new File(Environment.getExternalStorageDirectory().getPath()+File.separator+"Walls");
                if(!wallpaperDirectory.exists()){
                    success = wallpaperDirectory.mkdir();
                }
                if (success) {
                    downloadImg();
                    download.setEnabled(!download.isEnabled());
                }
                else{
                    Toast.makeText(getBaseContext(), "The app was not allowed to write to your storage. Please consider granting it this permission", Toast.LENGTH_LONG).show();
                }
            }
        });

         share.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {

             }
         });
         }
    private void downloadImg() {
        target = new Target() {
            @Override
            public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String filename = "img"+ new Date().getTime() + ".png";

                        File file = new File(Environment.getExternalStorageDirectory().getPath()+"/Walls"+File.separator, filename);
                        try {
                            file.createNewFile();
                            FileOutputStream ostream = new FileOutputStream(file);
                            bitmap.compress(Bitmap.CompressFormat.PNG,100,ostream);
                            ostream.close();
                        }
                        catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {}

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {}
        };

        Picasso
                .with(getApplicationContext())
                .load(imageurl)
                .into(target);

    }



}

0 个答案:

没有答案