Android:应用程序在一台设备上运行良好,但在另一台设备上运行不正常

标签 android permissions compatibility

我遇到了一个问题,我的应用程序在我的 Galaxy Tab 7.7 GT-P6800 上运行良好,我将其用于调试目的,并在我的 Galaxy Pocket 上使用它运行并创建一个文件夹来放置文件,甚至接受输入和所有这些东西,但它没有创建文件。我正在 eclipse 中开发,应用程序的构建 SDK 是 Android 2.3.3 (API 10)。

应用代码如下,找不到问题所在...

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 Build Path 属性。

最佳答案

参见 getExternalStorageDirectory()文档。

Gets the Android external storage directory. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with getExternalStorageState().

在访问它之前检查它是否为 null

关于Android:应用程序在一台设备上运行良好,但在另一台设备上运行不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11608766/

相关文章:

javascript - 独立使用 AngularJS 指令和 $scope

java - 无法解析符号 'ImageReference'

linux - 使用 netstat 远程获取基于 unix 的操作系统上的所有进程连接需要什么权限

html - Doctype 搞乱了 CSS 字体样式?

visual-studio - 使用 VS 2012 和 VS 2015 处理 Visual Studio 项目

powershell - 如何使用 Powershell 在 IIS 中设置 "ConnectAs"用户

c# - 在 c# 中使用 unity 在简历上重新启动应用程序

android - 在 Android 中屏幕旋转时 View 是清除数据

java - 使用 Google map PlacePicker API 时,我得到了错误的地名(获取坐标)

c - "creat"函数给出了错误的权限