简体   繁体   中英

Android App is running on virtual device but not on real device

I am trying to develop an android app but I don't really have much experience with it. At the moment the App reads all the contact information like names and phone numbers and writes the data into an XML file which is stored in the internal storage. Everything is working fine on the virtual devices from Android 4.1 to Android 2.2. I am working with eclipse. But now I wanted to test it on real devices. First I installed it on a Smartphone with Android 4.0. I managed to install the App and to start it. The App also wrote the file but it was empty. Afterwards I installed it on a smartphone with Android 2.3. It also started but I was not able to find the file. I am using AndroXplorer to access the internal storage.

As I've never worked with Android Apps before, can anybody give me some idea how I can figure out why the App is running on ALL the virtual devices but not on the real ones?

Thanks in advance!

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;
}

}

My minSDKVersion is 8, the target Version is 15. And I've added the permission for the internet and the permission to read contacts.

When I run it on the virtual devices, the application starts, my start screen appears and it creates the file "newcontacs3.xml" under data/data/com.examples.anotherproject/files.

You can access the internal storage on a real device if your device is rooted. On emulator you have the full root access thus you can find it on /data/data/com.your.package/files/ . But on a unrooted real device you dont have the full privilege.

Replace:

File newxmlfile = new File(this.getFilesDir(), "newcontacts3.xml");

With:

File newxmlfile = new File(Environment.getExternalStorageDirectory().getpath(), "newcontacts3.xml");

And add permission to your manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">

You'll find your file on your mounted storage root directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM