I can successfully download image in my phone storage but not showing in gallery before phone reboot

别来无恙 提交于 2020-07-13 15:55:02

问题


I am trying to download an image using a URL through a service I've created and when I inspect my device's file directory through Android Studio debugger, I can see the file in the device:

The red file is the downloaded file in question. The green image file is a file I manually dragged into the directory through Windows Explorer. my code is here please place saved file using MediaScannerConnection:

package com.romantic.coupleposts;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import com.romantic.coupleposts.R;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import com.theartofdev.edmodo.cropper.CropImage;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;

import static android.app.WallpaperManager.FLAG_LOCK;
import static android.app.WallpaperManager.FLAG_SYSTEM;

public class MyUtils {

    public static int counter = 0;

    public static void setBgOrDownload(String urlString, final Activity activity, final boolean download) {

        Picasso.get().load(urlString).into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                if (download) {
                   saveImageBitmap(activity, urlString,bitmap);

                } else {
                    new Thread(() -> {
                        WallpaperManager wallpaperManager = WallpaperManager.getInstance(activity);
                        try {
                            wallpaperManager.setBitmap(bitmap);
                            ((PreviewActivity) activity).wallpaperSet(true);
                        } catch (IOException e) {
                            e.printStackTrace();
                            ((PreviewActivity) activity).wallpaperSet(true);

                        }
                    }).start();

                }


            }

            @Override
            public void onBitmapFailed(Exception e, Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        });
    }
    public static void setHomeOrLock(String urlString, final Activity activity, boolean lockScreen) {

        Picasso.get().load(urlString).into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {

                    new Thread(() -> {
                        WallpaperManager wallpaperManager = WallpaperManager.getInstance(activity);
                        try {
                            if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.N)
                            {
                                if(lockScreen)
                                {
                                    wallpaperManager.setBitmap(bitmap,null,true,FLAG_LOCK);

                                }else{
                                    wallpaperManager.setBitmap(bitmap,null,true,FLAG_SYSTEM);


                                }
                                ((PreviewActivity) activity).homeOrLockSet(lockScreen);

                            }else{
                                wallpaperManager.setBitmap(bitmap);
                                ((PreviewActivity) activity).wallpaperSet(true);

                            }




                        } catch (IOException e) {
                            e.printStackTrace();
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(activity, "Something went wrong", Toast.LENGTH_SHORT).show();
                                }
                            });

                        }
                    }).start();

            }

            @Override
            public void onBitmapFailed(Exception e, Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        });
    }

    public static void saveImageBitmap(Activity context, String url, Bitmap bitmap) {
        AtomicBoolean isFilePresent = new AtomicBoolean(false);
        String myDir = Environment.getExternalStorageDirectory().toString() + "/" + context.getResources().getString(R.string.app_name);
        File fileDir = new File(myDir);

        if (!fileDir.exists()) {
            fileDir.mkdir();
        }
        url = url.substring(url.lastIndexOf("/") + 1);
        File newFile = new File(myDir, counter + "Wallpaper.jpg" );
        File[] filesList = fileDir.listFiles();
        if (filesList != null) {

            ArrayList<File> files = new ArrayList<>(Arrays.asList(filesList));
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
                Optional<File> result = files.stream().filter(file -> file.getAbsolutePath().equals(newFile.getAbsolutePath())).findFirst();
                result.ifPresent(file -> isFilePresent.set(true));
            } else {
                for (File file : files) {
                    if (file.getAbsolutePath().equals(newFile.getAbsolutePath())) {
                        isFilePresent.set(true);
                        break;
                    }
                }
            }

            if (!isFilePresent.get()) {
                saveImageIntoMemory(context,bitmap, newFile,true);
            } else {
                Toast.makeText(context, "Alread Downloaded", Toast.LENGTH_SHORT).show();
            }
        }

    }


    private static void saveImageIntoMemory(Activity context, Bitmap bitmap, File newFile, boolean download) {
        FileOutputStream ostream = null;
        try {
            newFile.createNewFile();
            ostream = new FileOutputStream(newFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
            ostream.flush();
            ostream.close();
            if(download)
            {
                ((PreviewActivity) context).downloadComplete(true);

            }else{
                Toast.makeText(context, "Image is saved", Toast.LENGTH_SHORT).show();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            ((PreviewActivity) context).downloadComplete(false);


        } catch (IOException e) {
            e.printStackTrace();
            ((PreviewActivity) context).downloadComplete(false);

        }
    }

    public static void getInternalFile(Activity context, String url) throws IOException {
        File myDir = context.getFilesDir();
        File file = new File(myDir, "tempFile.jpg");
        file.createNewFile();

        Picasso.get().load(url).into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                FileOutputStream ostream = null;
                try {
                    file.createNewFile();
                    ostream = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
                    ostream.flush();
                    ostream.close();
                    CropImage.activity(Uri.fromFile(file)).start(context);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    Log.d("lksjdf",e.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.d("lksjdf",e.toString());

                }
            }

            @Override
            public void onBitmapFailed(Exception e, Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        });
    }

    public static Bitmap getContactBitmapFromURI(Context context, Uri uri) {
        try {

            InputStream input = context.getContentResolver().openInputStream(uri);
            if (input == null) {
                return null;
            }
            return BitmapFactory.decodeStream(input);
        }
        catch (FileNotFoundException e)
        {

        }
        return null;

    }

    public static void saveCropedImage(Activity activity, String url, Bitmap bitmap)
    {
        String myDir = Environment.getExternalStorageDirectory().toString() + "/" + activity.getResources().getString(R.string.app_name);
        File fileDir = new File(myDir);

        if (!fileDir.exists()) {
            fileDir.mkdir();
        }
        url = url.substring(url.lastIndexOf("/") + 1);
        File newFile = new File(myDir, counter + "Crop.jpg");
        saveImageIntoMemory(activity,bitmap,newFile,false);
    }

    public static void shareApp(String content, Context context) {
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject Here");
        sharingIntent.putExtra(Intent.EXTRA_TEXT, content);
        context.startActivity(Intent.createChooser(sharingIntent, "Share Text"));
    }

    public static boolean isOnline(Context context) {

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        //should check null because in airplane mode it will be null
        return (netInfo != null && netInfo.isConnected());
    }
}

来源:https://stackoverflow.com/questions/62807993/i-can-successfully-download-image-in-my-phone-storage-but-not-showing-in-gallery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!