Android应用程序的虚拟设备上运行,但不是真实设备
问题描述:
我想开发一个Android应用程序,但我真的没有与它太多的经验。 目前在App读取所有的联系人信息,如姓名和电话号码,并将数据写入到存储在内部存储的XML文件。 从Android 4.1到Android 2.2的所有虚拟设备都可以正常工作。我正在使用eclipse。 但现在我想在真实设备上测试它。首先,我将它安装在带有Android 4.0的智能手机上。我设法安装了应用程序并启动它。该应用程序也写了文件,但它是空的。之后,我将它安装在Android 2.3的智能手机上。它也开始了,但我无法找到该文件。我使用AndroXplorer访问内部存储。Android应用程序的虚拟设备上运行,但不是真实设备
正如我从来没有与Android应用工作过,有谁能够给我一些想法,我怎么能弄清楚为什么应用程序是对所有的虚拟设备,但没有对以假乱真运行?
在此先感谢!
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create new file
File newxmlfile = new File(this.getFilesDir(), "newcontacts3.xml");
try {
newxmlfile.createNewFile();
} catch (IOException e) {
Log.e("IOException", "Exception in create new File(");
}
FileOutputStream fileos = null;
try {
fileos = new FileOutputStream(newxmlfile);
} catch (FileNotFoundException e) {
Log.e("FileNotFoundException", e.toString());
}
XmlSerializer serializer = Xml.newSerializer();
try {
serializer.setOutput(fileos, "UTF-8");
serializer.startDocument(null, Boolean.valueOf(true));
serializer.setFeature(
"http://xmlpull.org/v1/doc/features.html#indent-output",
true);
serializer.startTag(null, "root");
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String id = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
serializer.startTag(null, "ContactID");
serializer.attribute(null, "ID", id);
// GET ALL THE CONTACT DATA AND WRITE IT IN THE FILE
serializer.endTag(null, "ContactID");
}
}
cursor.close();
serializer.endTag(null, "root");
serializer.endDocument();
serializer.flush();
fileos.close();
} catch (Exception e) {
Log.e("Exception", "Exception occured in wroting");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
我的minSdkVersion是8,目标版本是15 我也加入了对互联网和读取联系人权限的权限。
当我在虚拟设备上运行它时,应用程序启动,我的启动屏幕出现,并在data/data/com.examples.anotherproject/files下创建文件“newcontacs3.xml”。
答
你可以,如果你的设备是植根在真实设备上访问内部存储。在模拟器上,您拥有完整的root权限,因此您可以在/data/data/com.your.package/files/
上找到它。但是在无根的真实设备上,你没有完全的特权。
替换:
File newxmlfile = new File(this.getFilesDir(), "newcontacts3.xml");
有了:
File newxmlfile = new File(Environment.getExternalStorageDirectory().getpath(), "newcontacts3.xml");
并添加权限您的清单:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
你会发现在你安装的存储根目录文件。
给我们一些细节。你的'min-sdk'和'target'是什么? – NewUser
张贴您的清单 – Sathish
@ user1778772张贴更多信息或张贴一些代码,以便我们可以帮助您更多。 –