我写的代码应该覆盖所选文本文件的内容,但它会附加它.我究竟做错了什么?
File fnew=new File("../playlist/"+existingPlaylist.getText()+".txt");
String source = textArea.getText();
System.out.println(source);
FileWriter f2;
try {
f2 = new FileWriter(fnew,false);
f2.write(source);
/*for (int i=0; i<source.length();i++)
{
if(source.charAt(i)=='\n')
f2.append(System.getProperty("line.separator"));
f2.append(source.charAt(i));
}*/
f2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
编辑
我尝试制作一个新的temp.txt文件并将新内容写入其中,删除此文本文件并将temp.txt重命名为此文件.事实是,删除总是不成功.我不认为我必须为此更改用户权限吗?
此外,我的程序的一部分列出了该目录中的所有文件,因此我猜测它们正在被程序使用,因此无法删除.但为什么不覆盖?
解决了
我最大的"D'哦"时刻!我一直在Eclipse上编译它而不是cmd,这是我执行它的地方.所以我新编译的类转到bin文件夹,通过命令提示符编译的类文件在我的src文件夹中保持不变.我用我的新代码重新编译,它就像一个魅力.
File fold=new File("../playlist/"+existingPlaylist.getText()+".txt");
fold.delete();
File fnew=new File("../playlist/"+existingPlaylist.getText()+".txt");
String source = textArea.getText();
System.out.println(source);
try {
FileWriter f2 = new FileWriter(fnew, false);
f2.write(source);
f2.close();
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud) [编辑] 我设计了一些图像比较代码.匹配的部分仍然有点缺陷,我希望得到一些帮助.该项目可在 - GitHub找到.
我有这两个图像Img1和Img2:

当我在openCV中使用以下命令时
Mat img1 = Highgui.imread("mnt/sdcard/IMG-20121228.jpg");
Mat img2 = Highgui.imread("mnt/sdcard/IMG-20121228-1.jpg");
try{
double l2_norm = Core.norm( img1, img2 );
tv.setText(l2_norm+"");
} catch(Exception e) {
//image is not a duplicate
}
Run Code Online (Sandbox Code Playgroud)
我得到了l2_norm的double值.对于重复的图像对,此双值会有所不同.但是如果图像不同,则抛出异常.这是我识别重复图像的方式吗?还是有更好的方法?我用Google搜索广泛,无法找到真正有说服力的答案.我希望代码和解释如何比较两个图像并获得布尔值true或false取决于图像.
编辑
Scalar blah= Core.sumElems(img2);
Scalar blah1=Core.sumElems(img1);
if(blah.equals(blah1))
{
tv.setText("same image");
}
}
Run Code Online (Sandbox Code Playgroud)
我试过这个,但if条件永远不会满足.我假设有一些差异,但没有任何compare功能Scalar.我该怎么办?
编辑
try{
Scalar blah= Core.sumElems(img2);
Scalar blah1=Core.sumElems(img1);
String b=blah.toString();
String b1=blah1.toString();
System.out.println(b+" "+b1);
double comp=b.compareTo(b1);
tv.setText(""+comp);
}
Run Code Online (Sandbox Code Playgroud)
这种方法又有缺陷.虽然它可以用于比较具有相当精确度的图像,但是当图像具有不同的尺寸时它会失败. …
这是我的代码:
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
wm = (WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mTopView = (ViewGroup) inflater.inflate(R.layout.activity_invisible, null);
params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_DIM_BEHIND
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_FULLSCREEN;
if (keep==true) {
int value = brightnessIntent.getExtras().getInt("value");
float v=value/255.0f;
params.dimAmount=0;
params.alpha=v;
rl = (RelativeLayout) mTopView.findViewById(R.id.window);
getWindow().setAttributes(params);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
wm.addView(mTopView, params);
}
Run Code Online (Sandbox Code Playgroud)
视图仍显示状态栏,mTopView是一个叠加窗口.如何让覆盖窗口覆盖整个屏幕?我不想"隐藏"状态栏,我希望我的活动叠加到它上面.
[编辑] 强调文字
我已经在我的onCreate()方法中有这个:
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Run Code Online (Sandbox Code Playgroud)
我的清单定义了一个全屏的风格.
SCREENSHOTS
这是目前的情况,我希望叠加层也扩展到状态栏.
假设我有字符串"Torcellite"和另一个字符串"Tor" - 这两个字符串的相似长度是3,因为它们都以"Tor"开头.现在另一个字符串"christmas"和"mas"的相似度为0,因为它们不是以相同的字符集开头的.
在这两种情况下,第二个字符串都是第一个字符串的后缀.
一个更清晰的例子:
字符串长度:1到10 ^ 5
串: abaabc
后缀:abaabc,baabc,aabc,abc,bc,c
相似度:abaabc,无,a,ab,无,无
相似度长度:6,0,1,2,0,0
答案:6 + 0 + 1 + 2 + 0 + 0 = 9
我有一个低效的逻辑来使用正则表达式找到这些部分后缀匹配.
算法:
从后缀的子串创建一个模式.
for(int i=1; i<substrings[i].length; i++) {
Pattern p = Pattern.compile("^"+substrings[i].substring(0, i));
Matcher m = p.find(string); //the given string for which similarities need to be calculated
if(m.find())
similaryLengths += i;
}
Run Code Online (Sandbox Code Playgroud)这种复杂性大致为O(n ^ 2),因为我需要通过字符串为后缀,然后是模式的子串.
我曾想过在模式中使用分组来查找组,但我不确定正则表达式会是什么样子.我想到的是第一个子串是: …
现在我注意到应用程序的安装大小非常奇怪.
当我使用从XML文件初始化视图的常规活动时,大小是715 kb.
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main_layout);
executeEvents = new ExecuteEvents(this);
display = (TextView) findViewById(R.id.display);
powermenu = (Button) findViewById(R.id.powermenu);
turnoffscreen = (Button) findViewById(R.id.turnscreenoff);
mapButton = (ImageButton) findViewById(R.id.mapButton);
SP = getSharedPreferences(PBConstants.Power_Button_SP, MODE_MULTI_PROCESS);
if(SP.contains(PBConstants.INPUT_DEVICE_TAG))
display.setText(getResources().getString(R.string.configured));
mapButton.setOnClickListener(this);
powermenu.setOnClickListener(this);
turnoffscreen.setOnClickListener(this);
}
Run Code Online (Sandbox Code Playgroud)
切换到创建的对话框后,设置包含XML文件中的小部件的视图.应用程序大小现在200kb.
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
//setContentView(R.layout.main_layout);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.main_layout, null);
executeEvents = new ExecuteEvents(this);
display = (TextView) view.findViewById(R.id.display);
powermenu = (Button) …Run Code Online (Sandbox Code Playgroud) 好吧,我有一个活动类,有两个后台任务(Async-Task),它们已经在两个独立的类中定义了
public class GettingBeaconsList extends AsyncTask<String, String, String>
public class GettingAirports extends AsyncTask<String, String, String>
Run Code Online (Sandbox Code Playgroud)
在MainClass中初始化和执行
public class MainClass extends Activity implements DelegateTaskCompleted
{
int ServiceBoolean = 0;
public OnClickListener LoadingBeaconList = new OnClickListener()
{
public void onClick(View v)
{
ServiceBoolean =1;
new GettingBeaconsList (context,MainClass.this).execute();
}
}
public OnClickListener LoadingAirportList= new OnClickListener()
{
public void onClick(View v)
{
ServiceBoolean =2;
new GettingAirports(context,MainClass.this).execute();
}
}
@Override
public void JsonArrayLoaded(JSONArray result)
{
// bla bla or whatever here i write, …Run Code Online (Sandbox Code Playgroud) 我写了这段代码,显然有缺陷.我如何创建一个不断检查数量变化的服务?关键监听器不能在服务中使用,请不要使用卷密钥监听器发布答案.
我的代码错了,因为我为while循环提供了虚拟条件.我的服务检查音量变化而不是崩溃的条件是什么?它不能是isScreenOn()因为在听音乐时可以改变音量并且屏幕关闭.
码
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
int newVolume=0;
while(1==1)
{
newVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
if(currentVolume!=newVolume)
{
Toast.makeText(this, "Volume change detected!", Toast.LENGTH_LONG).show();
}
}
Run Code Online (Sandbox Code Playgroud) 我有两个活动A和B. B是透明的传递活动,可以看到A. 我想按下按钮A杀死B.
这是我到目前为止所尝试的:
B obj=new B();
obj.finish();
Run Code Online (Sandbox Code Playgroud)
我创建了一个B的对象并试图杀死它.那没用.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("keep", true);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
这段代码应该做的是清除最顶层的活动,即B并再次调用B,除非这次我传递的值使得B在几秒钟后自杀.
由于某种原因,这只会增加活动的更多实例.或者至少我认为发生了什么,因为屏幕由于许多透明的活动而变得像素化.
这是我的清单:
<activity
android:name="com.xxx.xxx.B"
android:excludeFromRecents="true"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:clearTaskOnLaunch="true" >
</activity>
Run Code Online (Sandbox Code Playgroud)
当我在显示活动时按下按钮并且第二次杀死按钮时,我该怎么办呢?创作部分显然得到了照顾.我的活动B弹出,我想杀死它,因为B在顶部.
编辑
我用一个checkBox尝试了这个,这是代码:
enable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finishActivity(0);
Intent intent = new Intent(A.this, B.class);
if (enable.isChecked()) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("keep", true);
intent.putExtra("value", 10);
startActivityForResult(intent, 0);
}
else
{
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("keep", false);
startActivityForResult(intent, 0);
}
}
});
Run Code Online (Sandbox Code Playgroud)
选中启用后,将调用该活动,该工作正常.但他们一直在努力.当我检查并取消选中复选框时,它不像这个A-> B-> A-> B. 它是A-> B-> BB-> BBB
我正在尝试创建类似于Paranoid Android上的Halo Notification的应用程序.到现在为止还挺好.
但是,当我的"光环"出现在屏幕上时,我可以将它移动得很好,大多数触摸事件都能正常工作.但是,当我尝试通过点击EditText弹出键盘时,没有任何反应.窗口似乎消耗了焦点.后退按钮也不起作用,但主页和最近的应用程序按钮工作.
我正在运行PA的AOSP 4.4的根管理Nexus 4上测试应用程序
我用来创建光环窗口的代码(布局参数)是:
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
Run Code Online (Sandbox Code Playgroud)
有什么想法导致这种选择性的触摸事件消耗?
我想知道服务是否从特定活动中终止,所以我在调用时从该活动传递一个字符串stopServivce(service).
这是代码:
Intent service = new Intent(Activity.this,
service.class);
service.putExtra("terminate", "activity terminated service");
stopService(service);
Run Code Online (Sandbox Code Playgroud)
但我似乎可以getIntent().getExtras().getString("terminate);在onDestroy()方法中访问此变量.
[编辑]
我找到了绕过这个障碍的方法,但我仍然希望我的问题得到解答.我只是onDestroy()在活动中的方法中做了我必须做的事情然后调用stopService(service).我很幸运,我的情况不需要更复杂的事情.
我目前有一个AsyncTask目前bubble sort使用OpenCV技术比较图像.说,我必须相互比较400图像.这意味着400*401/2=80,200比较.我们假设一次比较需要1秒钟.所以,这80,200 sec是围绕22.27 hours这是可笑长.所以,我开发了这种类型的算法:
它将400图像分成几组5.所以80每组都有图像.
算法的第一部分是在组成员中比较自己的图像.
所以,image1会比较自己image2-80,这意味着有79比较.image2会有78比较等等.这使得3,160比较.或者3,160 sec.同样,image81将与自己进行比较image82-160等等.因此,所有"组比较"都已完成,3,160 sec因为它们是并行运行的.
算法的第二部分将group 1元素与group 2元素,group 2with group 3,group 3with group 4等进行比较.这将意味着image1将与之进行比较image81-160,这是80比较,因此将进行比较,group 1并group 2进行80*80=6400比较.是否可以将每个图像与组比较并行比较?也就是说,如果 …
我已经构建了一个身份验证系统,并希望将其设为联合身份验证和授权系统 (SSO),例如 Google+ 或 Facebook。经过研究,我发现 OAuth 2.0 之上的 OpenID Connect 是最好的选择。
我认为使用现有的经过良好测试的库比自己实现整个堆栈更好,因此我计划使用MITREID Connect。任何意见?
然而,还有一些事情我不确定:
如果我遗漏了任何关键点,请告诉我。如果我在错误的论坛上发帖,请随时将帖子移至适当的论坛。
java federated-identity openid-provider single-sign-on openid-connect
假设我正在将一个位图加载到我的Android设备上.可以抛出许多可能的异常.为了简单起见,我们采取NullPointerException和OutOfMemoryError.
现在我有两段代码.
代码1
try{
//load the bitmap
}
catch(Exception e)
{
//do something
}
Run Code Online (Sandbox Code Playgroud)
代码2
try{
//load the bitmap
catch (NullPointerException e)
{
//do something
}
catch(OutOfMemoryError e)
{
//do something else
}
Run Code Online (Sandbox Code Playgroud)
一段代码比其他性能更有效吗?如果是这样,为什么?