Android - 为什么我保存的图像没有出现在手机的默认图库中? [英] Android - Why my saved image is not appearing in the default gallery of my phone?

查看:25
本文介绍了Android - 为什么我保存的图像没有出现在手机的默认图库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将应用程序中的图像保存到手机的默认图库中.如果我的手机上有 SD 卡,下面的代码可以完美运行.正如预期的那样,保存的图像会出现在手机的图库和所有内容中:

I am trying to save an image from my application to the default gallery of my phone. The code below works perfectly if I have a SD card on the phone. The image saved appears in the phone's gallery and everything, as expected:

private Uri saveMediaEntry(File f, String title, String description, int orientation,      Location loc) {

    ContentValues v = new ContentValues();
    v.put(Images.Media.TITLE, title);
    v.put(Images.Media.DISPLAY_NAME, title);
    v.put(Images.Media.DESCRIPTION, description);

    v.put(Images.Media.ORIENTATION, orientation);

    String nameFile = f.getName();
    File parent = f.getParentFile() ;
    String path = parent.toString().toLowerCase() ;
    String nameParent = parent.getName().toLowerCase() ;
    v.put(Images.ImageColumns.BUCKET_ID, path.hashCode());
    v.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, nameParent);
    v.put(Images.Media.SIZE,f.length()) ;

    if( nameFile.toLowerCase().contains(".png") ){
        v.put(Images.Media.MIME_TYPE, "image/png");

    }else if( nameFile.toLowerCase().contains(".jpg") || 
              nameFile.toLowerCase().contains(".jpeg") ){
         v.put(Images.Media.MIME_TYPE, "image/jpeg");

    }else{
        v.put(Images.Media.MIME_TYPE, "image/jpeg");
    }

    String imagePath = f.getAbsolutePath();
    v.put("_data", imagePath) ;
    ContentResolver c = getContentResolver() ;

    Uri uriOfSucessfulySavedImage = null;
    uriOfSucessfulySavedImage = c.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, v);

    return uriOfSucessfulySavedImage;
  }

但是,如果我尝试将相同的图像保存到内部存储中(当手机没有 SD 卡时),图像不会出现在手机的图库中!为此,我只更改了上述代码中的一行:

However, if I try to save the same image into the internal storage (for when the phone does not have a SD card), the image does not appear in the phone's gallery! To try to do that, I only change one line from the above code:

uriOfSucessfulySavedImage = c.insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, v);

然而,有趣的是,变量 uriOfSucessfulySavedImage 不为空(它返回 content://media/internal/images/media/x,其中x"是一个数字).因此,图像被保存在手机内部存储的某个位置,但它没有像我使用 MediaStore.Images.Media.EXTERNAL_CONTENT_URI 时那样显示在手机图库中.

The interesting thing about this, however, is that the variable uriOfSucessfulySavedImage is not null (it returns content://media/internal/images/media/x, where 'x' is a number). So, the image is being saved somewhere in the internal storage of the phone, but it is not getting displayed in the phone gallery's as when I use MediaStore.Images.Media.EXTERNAL_CONTENT_URI.

有人知道发生了什么吗?如何将图像保存到手机的内部存储并将该图像保存在手机的图库中?

Does anybody have any clue what is going on? How can I save an image into the internal storage of the phone and have that image in the phone's gallery?

我忘记了一项重要信息."saveMediaEntry" 方法的参数中的文件f"来自另一种方法,用于挂载 SD 卡时(即第一个代码):

I forgot one important information. The File "f" in the parameters of the method "saveMediaEntry" is coming from this other method for when the SD card is mounted (that is, for the first code):

public static File getCacheDirectory(String desiredNameOfTheDirectory){

File fileCacheDir = null;

    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ){

        fileCacheDir = new File( Environment.getExternalStorageDirectory(), desiredNameOfTheDirectory );
   }


    if(!fileCacheDir.exists()){
        fileCacheDir.mkdirs();
    }

    return fileCacheDir;
}

以及从以下代码中了解 SD 卡何时创建:

and from the following code for when the SD card is not founded:

public static File getCacheDirectory(String desiredNameOfTheDirectory, Context   context){

    File fileCacheDir = null;

        fileCacheDir = context.getCacheDir();

    if(!fileCacheDir.exists()){
        fileCacheDir.mkdirs();
    }

    return fileCacheDir;
}

推荐答案

这个我没试过,但是我相信你需要运行Media Scanner来扫描内部存储目录,这样画廊才能看到你新保存的图像.在此处查看此帖子.

I haven't tried this, but I believe you need to run the Media Scanner to scan the internal storage directory so that the gallery can see your newly saved image. Check this post here.

这篇关于Android - 为什么我保存的图像没有出现在手机的默认图库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆