从Firebase数据库中设置图像的下载按钮

时间:2019-03-03 14:29:36

标签: android firebase

我正在尝试向我的应用程序添加一个“下载”按钮,当我从Firebase数据库中提取图像时将下载图像。到目前为止,这就是我要尝试的三步保存图像(墙纸)内部存储。首先获取LocalBitmap,其次获取SaveWallpaperAndGetUri,最后获取DownloadWallpaper,但它不起作用。我认为,有一个简单的问题。但是我没看见。

第一个getLocalBitmapUri

private Uri getLocalBitmapUri(Bitmap bmp) {
        Uri bmpUri = null;
        try {
            File file = new File(mCtx.getExternalFilesDir(Environment.DIRECTORY_PICTURES),
                    "Arutha" + System.currentTimeMillis() + ".png");
            FileOutputStream out = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.close();

            Uri.fromFile(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bmpUri;
    }

第二个saveWallpaperAndGetUri,

private Uri saveWallpaperAndGetUri(Bitmap bitmap, String id) {
        if (ContextCompat.checkSelfPermission(mCtx, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat
                    .shouldShowRequestPermissionRationale((Activity) mCtx, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);

                Uri uri = Uri.fromParts("package", mCtx.getPackageName(), null);
                intent.setData(uri);

                mCtx.startActivity(intent);

            } else {
                ActivityCompat.requestPermissions((Activity) mCtx, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
            }
            return null;
        }

        File folder = new File(Environment.getExternalStorageDirectory().toString() + "/wallpapers_hubs");
        folder.mkdirs();

        File file = new File(folder, id + ".jpg");
        try {
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();

            return Uri.fromFile(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

最后下载壁纸

private void downloadWallpaper(final Wallpaper wallpaper) {
        ((Activity) mCtx).findViewById(R.id.progressbar).setVisibility(View.VISIBLE);

        Glide.with(mCtx)
                .asBitmap()
                .load(wallpaper.url)
                .into(new SimpleTarget<Bitmap>() {
                          @Override
                          public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                              ((Activity) mCtx).findViewById(R.id.progressbar).setVisibility(View.GONE);

                              Intent intent = new Intent(Intent.ACTION_VIEW);

                              Uri uri = saveWallpaperAndGetUri(resource, wallpaper.id);

                              if (uri != null) {
                                  intent.setDataAndType(uri, "image/*");
                                  mCtx.startActivity(Intent.createChooser(intent, "Wallpapers Hub"));
                              }
                          }
                      }
                );
    }

0 个答案:

没有答案