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

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

问题描述

我想从我的应用程序到手机的默认图库保存图像。下面的code完美地工作,如果我有在手机上的SD卡。保存的图像显示在手机的画廊,一切都如预期:

 私人乌里saveMediaEntry(F文件,标题字符串,字符串描述,诠释方向,位置LOC){    ContentValues​​ V =新ContentValues​​();
    v.put(Images.Media.TITLE,职称);
    v.put(Images.Media.DISPLAY_NAME,职称);
    v.put(Images.Media.DESCRIPTION,说明);    v.put(Images.Media.ORIENTATION,方向);    串nameFile = f.getName();
    文件的父= f.getParentFile();
    字符串路径= parent.toString()与toLowerCase()。
    。字符串nameParent = parent.getName()与toLowerCase();
    v.put(Images.ImageColumns.BUCKET_ID,path.hash code());
    v.put(Images.ImageColumns.BUCKET_DISPLAY_NAME,nameParent);
    v.put(Images.Media.SIZE,f.length());    如果(nameFile.toLowerCase()。包含(PNG)){
        v.put(Images.Media.MIME_TYPE,图像/ PNG);    }否则如果(nameFile.toLowerCase()。包含(JPG)||
              nameFile.toLowerCase()。包含(JPEG)){
         v.put(Images.Media.MIME_TYPE,图像/ JPEG);    }其他{
        v.put(Images.Media.MIME_TYPE,图像/ JPEG);
    }    串的ImagePath = f.getAbsolutePath();
    v.put(_数据的ImagePath);
    ContentResolver的C = getContentResolver();    乌里uriOfSucessfulySavedImage = NULL;
    uriOfSucessfulySavedImage = c.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,ⅴ);    返回uriOfSucessfulySavedImage;
  }

不过,如果我尝试在同一图像保存到内部存储器(用于当手机没有SD卡),图像不会出现在手机的画廊!要尝试这样做,我只从上面的code改变一行:

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

这个有趣的事情,然而,就是变量 uriOfSucessfulySavedImage 不为空(它返回内容:// /内部/图片/媒体/ X ,其中x是一个数字)。因此,图像被某处在手机的内部存储保存,但它是没有得到显示在手机画廊,当我使用 MediaStore.Images.Media.EXTERNAL_CONTENT_URI

没有任何人有任何线索是怎么回事?我如何保存图片到手机的内部存储的并具有图像在手机的画廊的?

更新

我忘了一个重要的信息。文件F中的方法的参数的saveMediaEntry的从该其他方法来当SD卡被安装成(即,用于第一$ C $℃):

 公共静态文件getCacheDirectory(字符串desiredNameOfTheDirectory){文件fil​​eCacheDir = NULL;    如果(Environment.getExternalStorageState()。等于(Environment.MEDIA_MOUNTED)){        fileCacheDir =新的文件(Environment.getExternalStorageDirectory(),desiredNameOfTheDirectory);
   }
    如果(!fileCacheDir.exists()){
        fileCacheDir.mkdirs();
    }    返回fileCacheDir;
}

,然后从以下code时为SD卡的不是成立:

 公共静态文件getCacheDirectory(字符串desiredNameOfTheDirectory,上下文的背景下){    文件fil​​eCacheDir = NULL;        fileCacheDir = context.getCacheDir();    如果(!fileCacheDir.exists()){
        fileCacheDir.mkdirs();
    }    返回fileCacheDir;
}


解决方案

我没有试过,但我相信你需要运行媒体扫描仪扫描的内部存储目录,以便画廊可以看到新保存的图像。点击这里,查看此帖子

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;
  }

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);

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?

Update

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;
}

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;
}

解决方案

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天全站免登陆