如何创建一个应用程序图像文件夹中的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.
现在我不想要的图片保存在同一文件夹从相机拍摄的照片,我希望他们能够保存一个专用的应用程序文件夹中,就像从例如WhatsApp或Facebook应用程序的图像。
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.
我试着拯救他们在这两个位置中:
I've tried saving them in this two locations:
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);
如果我通过电话我看到我成功地保存图像,但他们没有在画廊的应用程序显示浏览。很明显,我失去了一些东西,但我不知道它是什么。也许添加一些元数据的文件或文件夹,以便画廊认识他们?
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).
这是我做过什么:
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);
这篇关于如何创建一个应用程序图像文件夹中的Android画廊展示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!