繁体   English   中英

如何创建要在 Android 库中显示的应用程序图像文件夹

[英]How to create an app image folder to show in Android gallery

我有一个应用程序,用户可以在其中创建一个图像,然后我想保存它,这样它就可以从默认的图库应用程序中看到。

现在我不希望图片与从相机拍摄的图片保存在同一文件夹中,我希望它们保存在专用于应用程序的文件夹中,就像来自 whatsapp 或 facebook 等应用程序的图像一样。

我试过将它们保存在这两个位置:

File imagePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+ File.separator + appDirectoryName + File.separator);

和这里

File imagePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ File.separator + appDirectoryName + File.separator);

如果我浏览手机,我会看到我成功保存了图像,但它们没有显示在图库应用程序中。 很明显我错过了一些东西,但我不知道它是什么。 也许向文件或文件夹添加某种元数据,以便画廊识别它们?

好吧,我最终找到了答案。

结果是我怀疑的。 保存的图像需要添加一些元数据才能在图库中看到(至少在我的设备中)。

这就是我所做的:

OutputStream fOut = null;
    File file = new File(imagePath,"GE_"+ System.currentTimeMillis() +".jpg");

    try {
        fOut = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
    try {
        fOut.flush();
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    ContentValues values = new ContentValues();
    values.put(Images.Media.TITLE, this.getString(R.string.picture_title));
    values.put(Images.Media.DESCRIPTION, this.getString(R.string.picture_description));
    values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis ());
    values.put(Images.ImageColumns.BUCKET_ID, file.toString().toLowerCase(Locale.US).hashCode());
    values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, file.getName().toLowerCase(Locale.US));
    values.put("_data", file.getAbsolutePath());

    ContentResolver cr = getContentResolver();
    cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

我曾经也有过一样的问题。 第二种方式是正确的方式,但是您在图库中看不到图像,因为图库需要刷新。 因此,您可以等待一段时间直到它自行刷新,或者您可以使用 MediaScanner - 看这里

希望这有帮助!

尝试这个

private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) {

    File direct = new File(Environment.getExternalStorageDirectory() + "/DirName");

    if (!direct.exists()) {
        File wallpaperDirectory = new File("/sdcard/DirName/");
        wallpaperDirectory.mkdirs();
    }

        File file = new File(new File("/sdcard/DirName/"), fileName);
        if (file.exists())
            file.delete();
        try {
            FileOutputStream out = new FileOutputStream(file);
            imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
 }

我做了以下工作来让它工作:

public void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_MOUNTED);
    String mCurrentPhotoPath = "file:" + image.getAbsolutePath(); // image is the created file image
    File file = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(file);
    mediaScanIntent.setData(contentUri);
    sendBroadcast(mediaScanIntent);
}

我试过这个它工作得很好。

private Uri imageUri;
String getimgname, getimgpath;
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(),  "IMG"+System.currentTimeMillis()+".jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
getimgname = photo.getName();
getimgpath = photo.getAbsolutePath();

尝试这个

MediaScannerConnection.scanFile(context,
                new String[] { file.toString() }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        Log.i("ExternalStorage", "Scanned " + path + ":");
                        Log.i("ExternalStorage", "-> uri=" + uri);
                    }
                });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM