Android:应用程序在一台设备上正常工作,而不在另一台设备上工作
问题描述:
我遇到了一个问题,我的应用在我的Galaxy Tab 7.7 GT-P6800上工作正常,我用它进行调试,并在我的Galaxy Pocket上运行并创建一个文件夹该文件,甚至采取输入和所有的东西,但它不是创建该文件。我在eclipse中开发,构建应用程序的SDK是Android 2.3.3(API 10)。Android:应用程序在一台设备上正常工作,而不在另一台设备上工作
的应用程序的代码如下,无法找到问题的是什么...
package com.android.app1;
public class MainActivity extends Activity {
//Timestamp for file name e.g. Data_7-19-2012
SimpleDateFormat s1 = new SimpleDateFormat("MM-dd-yyyy_hh:mm:ss");
final String format1 = s1.format(new Date());
final public String FileName = "Data_"+format1+".csv";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText addbarcode = (EditText) findViewById(R.id.editText1);
addbarcode.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_ENTER:
//to get Timestamp
SimpleDateFormat s = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
String format = s.format(new Date());
//get barcode in a variable
Editable barcode = addbarcode.getText();
final String DATASTRING = new String(""+barcode+","+format+"");
try
{
File root = new File(Environment.getExternalStorageDirectory(), "BarcodeData");
if (!root.exists()) {
root.mkdirs();
}
//Showing file directory
TextView etDir= (TextView)findViewById(R.id.textView1);
etDir.setText(root.toString());
File gpxfile = new File(root, FileName);
BufferedWriter bW;
bW = new BufferedWriter(new FileWriter(gpxfile, true));
bW.write(DATASTRING);
bW.newLine();
bW.flush();
bW.close();
}
catch(IOException e)
{
e.printStackTrace();
}
String readString = new String(DATASTRING);
TextView etData= (TextView)findViewById(R.id.textView2);
etData.setText(readString);
//Clear the editview for new entries
addbarcode.setText("");
return true;
default:
break;
}
}
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.send:
final File dir = getFilesDir();
final File shareFile = new File(dir, FileName);
Uri u1 = null;
u1 = Uri.fromFile(shareFile);
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details");
sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/csv");
startActivity(sendIntent);
break;
}
return true;
}
}
我什至增加所需的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
,甚至设置Java构建路径财产项目属性。
答
请参阅getExternalStorageDirectory()文档。
获取Android外部存储目录。如果用户在其计算机上挂载了该目录,则该目录可能不会被 访问,或者已从设备中删除,或发生了其他一些问题 。您可以使用 getExternalStorageState()确定其当前状态。
在访问它之前检查它是否为null
。
我用这个检查,但仍然没有运气我在上面的一个布尔isSDPresent =添加以下代码:equals(android.os.Environment.MEDIA_MOUNTED); 如果(isSDPresent) {// 是SD卡存在 } 其他 {// 对不起 } '而其真正的,但仍然没有文件。 – 2012-07-23 09:15:31
有没有人知道我错过了什么?我真的想让它结束。谢谢您的帮助。 – 2012-07-24 07:42:57
你可以发布你的logcat吗? – biegleux 2012-07-24 08:00:30