从Firebase下载图片

时间:2018-11-06 17:23:54

标签: java android firebase

当我点击下载按钮时,我正在从Firebase下载图片。一切正常,直到我按下按钮,然后应用崩溃。我使用了调试器,并粘贴了显示的内容。

我今天刚刚升级了Android Studio,以查看是否有任何更改但仍然无效。据我所知,一切都已更新。

这是我项目的最后一步,有什么想法吗?谢谢。

public class WallpapersAdapter extends RecyclerView.Adapter<WallpapersAdapter.WallpaperViewHolder> {


private Context mCtx;
private List<Wallpaper> wallpaperList;




public WallpapersAdapter(Context mCtx, List<Wallpaper> wallpaperList) {
    this.mCtx = mCtx;
    this.wallpaperList = wallpaperList;

}

@NonNull
@Override
public WallpaperViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mCtx).inflate(R.layout.recyclerview_wallpapers, parent, false);
    return new WallpaperViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull WallpaperViewHolder holder, int position) {

    Wallpaper w = wallpaperList.get(position);
    holder.textView.setText(w.title);
    Glide.with(mCtx)
            .load(w.url)
            .into(holder.imageView);
    if(w.isFavorite){
        holder.checkBoxFav.setChecked(true);
    }
}

@Override
public int getItemCount() {
    return wallpaperList.size();
}

class WallpaperViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, CompoundButton.OnCheckedChangeListener{

    TextView textView;
    ImageView imageView;
    CheckBox checkBoxFav;
    ImageButton buttonShare;
    Button buttonDownload ;



    public WallpaperViewHolder(View itemView) {
        super(itemView);

        textView = itemView.findViewById(R.id.text_view_title);
        imageView = itemView.findViewById(R.id.image_view);
        checkBoxFav = itemView.findViewById(R.id.checkbox_favorite);
        buttonShare = itemView.findViewById(R.id.button_share);
        buttonDownload = itemView.findViewById(R.id.download);
        /*setWallpaper = itemView.findViewById(R.id.set_wallpaper);*/
        /*setWallpaper.setOnClickListener(this);*/
        checkBoxFav.setOnCheckedChangeListener(this);
        /*buttonShare.setOnClickListener(this);*/
        buttonDownload.setOnClickListener(this);





    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.button_share:

                shareWallpaper(wallpaperList.get(getAdapterPosition()));
                break;
            case R.id.download:

                downloadWallpaper(wallpaperList.get(getAdapterPosition()));

                break;


        }

    }


    private void shareWallpaper(Wallpaper w){
        ((Activity) mCtx).findViewById(R.id.progressbar).setVisibility(View.VISIBLE);

        Glide.with(mCtx)
                .asBitmap()
                .load(w.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_SEND);
                              intent.setType("image/*");
                              intent.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(resource));
                              mCtx.startActivity(Intent.createChooser(intent, "The Wallpaper App"));

                    }


                });
    }

    private Uri getLocalBitmapUri(Bitmap bmp){
        Uri bmpUri = null;


        try {
            File file = new File(mCtx.getExternalFilesDir(Environment.DIRECTORY_PICTURES),
                    "the_wallpaper_app_" + System.currentTimeMillis() + ".png");
            FileOutputStream out = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.close();
            bmpUri = Uri.fromFile(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bmpUri;
    }


    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, "The Wallpaper App"));
                        }
                    }



                });
    }

    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() + "/the_wallpaper_app" );
        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;


    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

        if(FirebaseAuth.getInstance().getCurrentUser() == null){

            Toast.makeText(mCtx, "Please login first", Toast.LENGTH_LONG).show();
            compoundButton.setChecked(false);
            return;
        }



        int position = getAdapterPosition();
        Wallpaper w = wallpaperList.get(position);

        DatabaseReference dbFavs = FirebaseDatabase.getInstance().getReference("users")
                .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                .child("favorites")
                .child(w.category);



        if(b){
            dbFavs.child(w.id).setValue(w);
        }else{
            dbFavs.child(w.id).setValue(null);
        }

    }


}


}

调试报告:

android.os.FileUriExposedException: file:///storage/emulated/0/the_wallpaper_app/-LQBBi2ko8Z7x3-hQRa5.jpg exposed beyond app through Intent.getData()
    at android.os.StrictMode.onFileUriExposed(StrictMode.java:1958)
    at android.net.Uri.checkFileUriExposed(Uri.java:2356)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:10511)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:10486)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:10465)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1616)
    at android.app.Activity.startActivityForResult(Activity.java:4564)
    at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:68)
    at android.app.Activity.startActivityForResult(Activity.java:4522)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:751)
    at android.app.Activity.startActivity(Activity.java:4883)
    at android.app.Activity.startActivity(Activity.java:4851)
    at com.freedomsled.thewallpaperapp.adapters.WallpapersAdapter$WallpaperViewHolder$2.onResourceReady(WallpapersAdapter.java:205)
    at com.freedomsled.thewallpaperapp.adapters.WallpapersAdapter$WallpaperViewHolder$2.onResourceReady(WallpapersAdapter.java:194)
    at com.bumptech.glide.request.SingleRequest.onResourceReady(SingleRequest.java:568)
    at com.bumptech.glide.request.SingleRequest.onResourceReady(SingleRequest.java:530)
    at com.bumptech.glide.load.engine.EngineJob.handleResultOnMainThread(EngineJob.java:218)
    at com.bumptech.glide.load.engine.EngineJob$MainThreadCallback.handleMessage(EngineJob.java:324)
    at android.os.Handler.dispatchMessage(Handler.java:101)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6938)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

0 个答案:

没有答案