简体   繁体   English

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

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

I have an app where the user creates an image and then I want to save it so it's visible form the default gallery application.我有一个应用程序,用户可以在其中创建一个图像,然后我想保存它,这样它就可以从默认的图库应用程序中看到。

Now I don't want the pictures to be saved in the same folder as the pictures taken from the camera, I want them to be saved in a folder dedicated to the app, just like images from apps like whatsapp or facebook.现在我不希望图片与从相机拍摄的图片保存在同一文件夹中,我希望它们保存在专用于应用程序的文件夹中,就像来自 whatsapp 或 facebook 等应用程序的图像一样。

I've tried saving them in this two locations:我试过将它们保存在这两个位置:

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

and here和这里

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

If I browse through the phone I see that I successfully save the images but they don't show in the gallery app.如果我浏览手机,我会看到我成功保存了图像,但它们没有显示在图库应用程序中。 It is obvious that I'm missing something but I don't know what it is.很明显我错过了一些东西,但我不知道它是什么。 Maybe adding some kind of metadata to the files or folders so the gallery recognizes them?也许向文件或文件夹添加某种元数据,以便画廊识别它们?

Well I found the answer in the end.好吧,我最终找到了答案。

It turned out to be what I suspected.结果是我怀疑的。 The saved image needs to have some metadata added in order to be seen in the gallery (at least in my device).保存的图像需要添加一些元数据才能在图库中看到(至少在我的设备中)。

This is what I did:这就是我所做的:

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

I have had the same problem.我曾经也有过一样的问题。 The second way is the correct way, but you don't see the images in the Gallery because the gallery needs to be refreshed.第二种方式是正确的方式,但是您在图库中看不到图像,因为图库需要刷新。 So, you can wait a while until it refreshes itself, or you can use the MediaScanner - look here因此,您可以等待一段时间直到它自行刷新,或者您可以使用 MediaScanner - 看这里

Hope this helped!希望这有帮助!

Try this尝试这个

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

I did the following to get it to work:我做了以下工作来让它工作:

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

I tried this it works perfectly.我试过这个它工作得很好。

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

Try this尝试这个

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