嘿,这似乎是一个愚蠢的问题,但我怎样才能在内部存储中制作多个文件而不必发送垃圾邮件
f = new File(context.getFilesDir(),filename);
当我想在用户每次触摸屏幕时创建一个文件时。我试图让它文件名末尾有一个数字,所以当我创建文件时它会有所不同,但程序总是求助于原始文件“name0 " 而不是 "name1" 即使日志说它应该说 1
String filename = "name"+num;
每次用户触摸屏幕时,我必须更改什么程序才能生成新的数据文件?
面板类的 Ontouch 方法
public boolean onTouchEvent(MotionEvent event) {
mainactivity.setvalue(newvalue);
mElements.add(new Element(getResources(),(int) event.getX(),(int) event.getY(),this.getContext()));
Log.v("Gesture", "is newvalue: "+newvalue);
newvalue++;
return super.onTouchEvent(event);
}
元素类
public Element(Resources res, int x, int y,Context context) {
location2 =item3;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap = BitmapFactory.decodeResource(res, location2);
mX = x - mBitmap.getWidth() / 2;
mY = y - mBitmap.getHeight() / 2;
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArray = stream.toByteArray();
writeBitmap(byteArray, context);
writemX(mX,context);
writemY(mY,context);
Log.v("Element", "num: "+num);
num++;
}
writeX 方法
public void writemX(int x,Context context){
value=getvalue();
Log.v("main", "made it to method writeX" );
Log.v("main", "value before= "+getvalue());
File f = new File(context.getFilesDir(),postionX);
try {
fos = new FileOutputStream(f);
objectwrite = new ObjectOutputStream(fos);
objectwrite.writeByte(x);
fos.close();
Log.v("main", "file was made File ");
}catch (FileNotFoundException e){
e.printStackTrace();
Log.v("main", "file was not made File not found ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.v("main", "file was not made File IOException ");
}
public int getvalue(){
Log.v("main", "getvale value = "+value);
return(value);
}
public void setvalue(int newvalue){
value=newvalue;
Log.v("main", "setvale value = "+value);
}