【问题标题】:update android image gallery with newly created bitmap使用新创建的位图更新 android 图片库
【发布时间】:2011-10-07 13:09:04
【问题描述】:

我正在尝试将图像文件保存到外部存储。我可以将图片保存到 sdcard,但它没有显示在 Androids 图库应用程序中。我试过这种方法:

File path = Environment.getExternalStorageDirectory();
            File f = new File(path + "/mydirectory/" + imageName + "_" +     System.currentTimeMillis() + ".jpg");
            FileOutputStream fos = new FileOutputStream(f);
            f.mkdirs();
            b.compress(CompressFormat.JPEG, 100, fos);
            fos.close();

            Uri contentUri = Uri.fromFile(f);
            Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
            mediaScanIntent.setData(contentUri);
            getApplicationContext().sendBroadcast(mediaScanIntent);

但它没有出现在图库中。谁能指出我解决这个问题的正确方向?

【问题讨论】:

    标签: android android-camera android-mediascanner


    【解决方案1】:

    使用此代码将图像位图保存在 android 设备库中

    public void savePhoto(Bitmap bmp)
    {
    imageFileFolder = new File(Environment.getExternalStorageDirectory(),"Rotate");
    imageFileFolder.mkdir();
    FileOutputStream out = null;
    Calendar c = Calendar.getInstance();
    String date = fromInt(c.get(Calendar.MONTH))
                + fromInt(c.get(Calendar.DAY_OF_MONTH))
                + fromInt(c.get(Calendar.YEAR))
                + fromInt(c.get(Calendar.HOUR_OF_DAY))
                + fromInt(c.get(Calendar.MINUTE))
                + fromInt(c.get(Calendar.SECOND));
    imageFileName = new File(imageFileFolder, date.toString() + ".jpg");
    try
    {
     out = new FileOutputStream(imageFileName);
     bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
     out.flush();
     out.close();
     scanPhoto(imageFileName.toString());
     out = null;
    } catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    
    
    public String fromInt(int val)
    {
    return String.valueOf(val);
    }
    
    
    public void scanPhoto(final String imageFileName)
    {
    msConn = new MediaScannerConnection(PreviewDemo1.this,new MediaScannerConnectionClient()
    {
    public void onMediaScannerConnected()
    {
    msConn.scanFile(imageFileName, null);
    Log.i("msClient obj  in Photo Utility","connection established");
    }
    public void onScanCompleted(String path, Uri uri)
    {
    msConn.disconnect();
    Log.i("msClient obj in Photo Utility","scan completed");
    }
    });
    msConn.connect();
    } 
    

    这里我将图像保存在“旋转”文件夹中,如果您不想这样,您可以在 savePhoto 方法中轻松更改它。

    【讨论】:

    • 不要忘记在 AndroidManifest.xml 中使用<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 设置 WRITE_EXTERNAL_STORAGE 权限。
    【解决方案2】:

    我知道我回答这个问题有点晚了,但我想对于其他阅读本文的人来说,surendra 给出的答案是正确的,并且使用 MediaScannerConnection 是更新画廊的一种方式。至于 nevva 建议对他的代码进行更改的方式是:

        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        getApplicationContext().sendBroadcast(mediaScanIntent);
    

    【讨论】:

      【解决方案3】:

      我写了一篇关于在 Android 中扫描媒体文件的详细文章。 http://androidyue.github.io/blog/2014/01/19/scan-media-files-in-android/ 我希望这可以帮助你。

      【讨论】:

        【解决方案4】:

        将位图保存到 sd 后,只需使用 MediaScannerConnection:

        MediaScannerConnection.scanFile(this,
                        new String[] { Bitmapfile.getAbsolutePath() }, null,
                        new MediaScannerConnection.OnScanCompletedListener() {
                            public void onScanCompleted(String path, Uri uri) {
                                //now visible in gallery
                            }
                        }
                    );
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多