这个问题之前已经被问过(不是特别像这样),但还没有一个 All Exclusive 的答案。所以我们试图在这里找到最好的解决方案。我正在开发一个应用程序,在我的应用程序中,我通过将其文件移动到一个名为 .myPic 的目录来隐藏一个名为 myPic 的目录。当我隐藏我的照片时,它的缩略图仍在画廊中。我找到了 3 个解决方案:
像这样使用 ACTION_MEDIA_MOUNTED 广播:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
此代码的问题在于它占用大量资源,最重要的是它自 android 4.4 起被阻止。所以使用这种方法在图库中添加 10 张图片是不合理的。所以它不是一个全排他的方法。也使用 ACTION_MEDIA_SCANNER_SCAN_FILE 在 android 4.4 上也不起作用
使用 MediaScannerConnection。所以我创建了一个 for 循环并传递我隐藏的每个文件的旧地址。这是我的 MediaScannerConnection 函数:
private void scanFile(File file) {
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,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);
}
});
}
关于 MediaScannerConnection 的事情是它仅在文件存在时才有效。所以假设我在 myPic 目录中有一张名为 1.jpg 的图片。使用这个类,我可以立即将 1.jpg 添加到我的画廊,但是当我将 1.jpg 移动到 .myPic 目录并扫描旧的1.jpg 的路径没有任何反应。 logcat 说这个文件不存在。所以 MediaScannerConnection 只将文件添加到画廊。如果我将 1.jpg 的新路径传递给 MediaScannerConnection 会怎样?好吧,它将 .myPic 目录中的 1.jpg 添加到画廊,这正是我想要的不是。所以再次不是完全排他的方法
使用 getContentResolver()。所以对于删除缩略图,这种方法可能是最终的解决方案。所以我写了打击代码。在每个循环中,我都会检索图像的路径并将其传递给 getContentResolver().delete(Uri.parse(path),null,null)。这是代码:
File myPic = new File(Environment.getExternalStorageDirectory()+"/myPic");
File myPicHide = new File(Environment.getExternalStorageDirectory()+"/.myPic");
if (!(myPicHide.exists()) & !(myPicHide.isDirectory())) {
myPicHide.mkdirs();
};
if (myPic.isDirectory()) {
String[] childeren = myPic.list();
if (childeren.length > 0) {
for (int i = 0; i < childeren.length; i++) {
String fileName = childeren[i];
File from = new File(Environment.getExternalStorageDirectory()+"/myPic"+fileName);
File to = new File(Environment.getExternalStorageDirectory()+"/.myPic"+fileName);
from.renameTo(to);
try {
String path = from.toString();
getContentResolver().delete(Uri.parse(path),null,null);
} catch(Exception e) {
Log.d("Rename", "Error happened");
}
}
}
} else {
Toast.makeText(getApplicationContext(), "myPic directory not found", Toast.LENGTH_LONG).show();
}
但它也不起作用,我的文件的缩略图仍然显示在厨房中。那么我是否以错误的方式使用 getContentResolver() ?对于已删除文件缩略图出现在图库中的情况,这可能是所有独占方法。我有我的文件路径,我只需要从媒体存储内容提供商中删除它。
更新:
事实证明,在第三个解决方案中使用 Uri.parse(path) 是错误的。 image Uri 以 content:// 开头,可以通过 MediaScannerConnection 检索。所以我创建了一个名为 imageInGalleryUri 的 Uri 并为其分配 null 值。使用我的 scanFile 函数,我不时更改它的值并将它的值传递给 getContentResolver()。这是代码:
boolean whereIsMediaState = true;
Uri imageInGalleryUri = null;
File myPic = new File(Environment.getExternalStorageDirectory()+"/myPic");
File myPicHide = new File(Environment.getExternalStorageDirectory()+"/.myPic");
if (!(myPicHide.exists()) & !(myPicHide.isDirectory())) {
myPicHide.mkdirs();
};
if (myPic.isDirectory()) {
String[] childeren = myPic.list();
if (childeren.length > 0) {
for (int i = 0; i < childeren.length; i++) {
String fileName = childeren[i];
File from = new File(Environment.getExternalStorageDirectory()+"/myPic"+fileName);
scanFile(from);
File to = new File(Environment.getExternalStorageDirectory()+"/.myPic"+fileName);
from.renameTo(to);
if (to.isFile()){
try {
getContentResolver().delete(imageInGalleryUri,null,null);}
catch(Exception e) {
Log.d("Rename", "Error happened");
}
}
}
} else {
Toast.makeText(getApplicationContext(), "myPic directory not found", Toast.LENGTH_LONG).show();
}
private void scanFile(File file) {
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,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);
imageInGalleryUri = uri;
}
});
}
我尝试了代码,但它只检测到第一张图像并将其从图库中删除,但不会影响其他图像。我不知道为什么。有什么想法吗?
提前感谢您的帮助
最佳答案
。之前的文件夹只是让它不可见。但是有办法说根本不要使用这个文件夹来画廊。 请尝试将名为“.nomedia”的空文件放入您的文件夹中。
关于java - 隐藏时从图库中删除图像缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29843846/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司
在Ruby中是否有Gem或安全删除文件的方法?我想避免系统上可能不存在的外部程序。“安全删除”指的是覆盖文件内容。 最佳答案 如果您使用的是*nix,一个很好的方法是使用exec/open3/open4调用shred:`shred-fxuz#{filename}`http://www.gnu.org/s/coreutils/manual/html_node/shred-invocation.html检查这个类似的帖子:Writingafileshredderinpythonorruby?
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht