在我的应用程序中,我使用以下意图获得SD卡写入权限.如果用户从系统文件资源管理器中选择sd卡文件夹,那么我有SD卡写访问权限.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
startActivityForResult(intent, 42);
Run Code Online (Sandbox Code Playgroud)
之后,我可以使用DocumentFile类修改SD卡中的文件.但我在获取随机文件路径的DocumentFile时遇到问题.
Document.fromFile(new File(path));
Document.fromSingleUri(Uri.fromFile(new File(path)));
Run Code Online (Sandbox Code Playgroud)
两者都返回一个DocumentFile对象,该对象在.canWrite()上返回false.即使我已经获得SD卡许可.
所以我编写了在我的问题末尾发布的方法,以获得一个在.canWrite()上返回true的DocumentFile.但这很慢......而且感觉非常错误!必须有更好的方法来做到这一点.我还写了一个返回相同字符串的方法
String docFileUriString = docFile.getUri().toString();
Run Code Online (Sandbox Code Playgroud)
对于任何文件,其中docFile是由下面的方法返回的DocumentFile.但
DocumentFile.fromTreeUri(Uri.parse(docFileUriString ));
Run Code Online (Sandbox Code Playgroud)
返回一个DocumentFile,它指向sd卡的根,而不是DocumentFile路径.这很奇怪.有人可以提出更优雅的解决方案吗?
public static DocumentFile getDocumentFileIfAllowedToWrite(File file, Context con){
List<UriPermission> permissionUris = con.getContentResolver().getPersistedUriPermissions();
for(UriPermission permissionUri:permissionUris){
Uri treeUri = permissionUri.getUri();
DocumentFile rootDocFile = DocumentFile.fromTreeUri(con, treeUri);
String rootDocFilePath = FileUtil.getFullPathFromTreeUri(treeUri, con);
if(file.getAbsolutePath().startsWith(rootDocFilePath)){
ArrayList<String> pathInRootDocParts = new ArrayList<String>();
while(!rootDocFilePath.equals(file.getAbsolutePath())){
pathInRootDocParts.add(file.getName());
file = file.getParentFile();
}
DocumentFile docFile = null;
if(pathInRootDocParts.size()==0){
docFile = DocumentFile.fromTreeUri(con, rootDocFile.getUri());
}
else{
for(int i=pathInRootDocParts.size()-1;i>=0;i--){
if(docFile==null){docFile = rootDocFile.findFile(pathInRootDocParts.get(i));} …Run Code Online (Sandbox Code Playgroud) 这很奇怪,但是我从未见过关于如何在正确的时间执行动画的详细记录,以便在Activity的生命周期进展期间可以流畅地观看动画。
例如,如果您开始制作动画,onCreate那么您甚至都不会看到动画的发生。
同样,如果“活动A”以“活动B”开始,startActivityForResult并且您要启动动画onActivityResult(以显示用户的选择),那么您再也看不到任何内容,因为从“活动B”到“活动A”的过渡需要一些时间。
在这些情况下,我们如何知道何时开始动画?我现在对onActivityResult此案更感兴趣。
另外,我们一般如何知道视图是否可以真正执行用户实际看到的动画?
animation android android-animation android-lifecycle android-view
我的应用程序有一个非常奇怪的问题。我有一项始终显示 ImageView 的服务。当用户与图像交互以使某些事情发生时,使用 onTouchListener。当服务关闭时,ImageView 将从窗口中删除(至少它应该如此)。
当我进行设置活动(这是一个片段)时,每次更改首选项时,我都会重新启动服务,以便在服务中进行更改。在重新启动期间,出于某种奇怪的原因,有时不会删除 ImageView。服务再次启动后,会在其顶部显示一个新的 ImageView。如果我完全关闭服务,“孤立” ImageView 只会停留在屏幕上,如果触摸它,应用程序就会崩溃。
这个错误是随机的。作为用户,我无法将它连接到特定的设置或操作;即使单击启用/禁用切换按钮几次,也可能发生错误。
设置片段
ToggleButton enableToggle = (ToggleButton)v.findViewById(R.id.enable_toggle);
enableToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
prefs.edit().putBoolean("HotSpotEnabled", isChecked).commit();
if(isChecked){
getActivity().stopService(new Intent(getActivity(),mysService.class));
getActivity().startService(new Intent(getActivity(),myService.class));
}
else{
getActivity().stopService(new Intent(getActivity(),myService.class));
}
}
});
Run Code Online (Sandbox Code Playgroud)
每当用户的首选项发生更改时,我都会使用此功能重新启动服务
public void restartService (){
if(myService.isRunning){
enabledToggle.setChecked(false);
final Handler handler = new Handler();
handler.postDelayed(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
enabledToggle.setChecked(true);
}
}, 1000);
}
}
Run Code Online (Sandbox Code Playgroud)
(我使用切换来重新启动服务而不是 stopService();-startService(); 因为由于某种原因,这种问题不太常见) …
我正在我的应用程序中实现文件浏览器功能.我知道如何使用ACTION_OPEN_DOCUMENT_TREE意图获得外部SD卡的持久权限,以及如何使用DocumentFile类创建文件夹和删除文件/文件夹.
但是,我找不到将文件复制/移动到外部SD卡文件夹的方法.你能指出我正确的方向吗?
我认为Exception有助于捕获所有可能的异常,因为每个异常都将Exception作为基类.然后在开发Android应用程序时,我使用了以下方法,这些方法在一些自定义ROM中已被删除.
boolean result = false;
try{
result = Settings.canDrawOverlays(context);
}
catch(Exception e){
Log.e("error","error");
}
Run Code Online (Sandbox Code Playgroud)
然而,这并没有抓住异常抛出.后来我用NoSuchMethodError而不是Exception然后抓住了异常.
有人可以解释为什么会这样吗?
我正在构建的应用程序使用接近传感器.在不同的手机中尝试它我发现接近传感器在不同的手机中有不同的(最小,最大)值(我的htc感觉有0覆盖,9有未覆盖,而xperia有0覆盖,1有未覆盖).所以问题是:我如何获得接近传感器的最小值?
构建一个 Android 应用程序 我遇到了需要进行一些文件复制的问题。我想要一种通过在文件名中添加“(增量)”字符串来在文件名已被使用的情况下获取新文件名的方法。例如
text.txt ---> text(1).txt
Run Code Online (Sandbox Code Playgroud)
该算法应考虑以下因素
1) 如果text.txt存在,新文件名应该NEVER是text.txt(1)
2)如果text(1).txt存在,那么新的文件名应该是text(2).txt不text(1)(1).txt
3) 如果text(1)foo.txt存在,新文件名应该是text(1)foo(1).txt
我已经完成了第一个,但第二个我遇到了困难。正则表达式不是我的强项!(使用正则表达式不是强制性的。欢迎使用每种方法)一些帮助?
回答:
结合我的原始代码和这里的一个答案,我最终得到了这个,无论文件是否有扩展名,它在所有情况下都非常适合我:
public static File getFinalNewDestinationFile(File destinationFolder, File fileToCopy){
String destFolderPath = destinationFolder.getAbsolutePath()+File.separator;
File newFile = new File(destFolderPath + fileToCopy.getName());
String filename=fileToCopy.getName();
String nameWithoutExtentionOrIncrement;
String extension = getFileExtension(filename);
if(extension!=null){
extension="."+extension;
int extInd = filename.lastIndexOf(extension);
nameWithoutExtentionOrIncrement = new StringBuilder(filename).replace(extInd, extInd+extension.length(),"").toString();
}
else{
extension="";
nameWithoutExtentionOrIncrement = filename;
}
int c=0;
int indexOfClose = nameWithoutExtentionOrIncrement.lastIndexOf(")");
int …Run Code Online (Sandbox Code Playgroud) 使用vqmod我在header.tpl中注入此代码.
<?php if ($this->customer->isLogged()){ if($this->customer->getCustomerGroupId()!=1) { ?>
//show something
<?php } ?>
Run Code Online (Sandbox Code Playgroud)
它在opencart 1.5.6.1中运行良好但是现在我更新到2.0.3.1并且我收到此错误:
Notice: Undefined property: Loader::$customer in C:\UniServerZ\www\opencart-2.0.3.1\upload\vqmod\vqcache\vq2-catalog_view_theme_default_template_common_header.tpl
Run Code Online (Sandbox Code Playgroud)
有没有办法从2.0.3.1中的.tpl文件中引用$ this?