OnScanCompletedListener由于Toast而崩溃

时间:2012-02-11 15:57:15

标签: android toast

我正在写我的课程中的SD卡,我可以成功地做到这一点,我也可以设置MediaScannerConnection和OnScanCompletedListener。这一切都有效,但是当我举起一个Toast来提醒用户扫描已经在OnScanCompleteListener中完成时,我只是从LogCat中获得了很多错误并且没有出现toast,但文件仍然保存到SD卡。

我该如何解决这个问题?

下面有更多细节..

写入SD卡(完美运行)

  InputStream is = getResources().openRawResource(R.drawable.greenball);
                OutputStream os = new FileOutputStream(file);
                byte[] data = new byte[is.available()];
                is.read(data);
                os.write(data);
                is.close();
                os.close();

OnScanCompletedListener,只是不能用toast里面?它仍然保存在SD卡上的文件,但是我只是从Logcat和Toast中出现了很多错误,OnscanCompleteListener没有出现,我已经尝试过没有Toast并且它可以工作,所以它一定是那样。

MediaScannerConnection.scanFile(ExternalData.this,
new String[] {file.toString()},
null, 
new MediaScannerConnection.OnScanCompletedListener() {

                            public void onScanCompleted(String path, Uri uri) {
                                // TODO Auto-generated method stub
                                Toast t = Toast.makeText(ExternalData.this, "scan complete", Toast.LENGTH_SHORT);
                                t.show();
                            }
                        });

一些Log Cat错误

02-11 15:34:39.896: E/JavaBinder(11134): java.lang.RuntimeException: Can't create
handler inside thread that has not called Looper.prepare()

02-11 15:34:39.896: E/JavaBinder(11134):    at android.os.Handler.<init>   
(Handler.java:121)

02-11 15:34:39.896: E/JavaBinder(11134):    at 
android.widget.Toast.makeText(Toast.java:231)

任何帮助都会很棒我已经四处寻找并且无法找到任何相关信息。如果我做了一个简单的错误,有人可以纠正我,谢谢。

以下是此类的完整代码,以防它有用:

 IMPORTS...

 public class ExternalData extends Activity implements OnClickListener, 
 OnItemSelectedListener  {

TextView canWrite, canRead;
String state;
boolean canW, canR;
Spinner spinner;
String[] paths = { "Music", "Pictures", "Download" };
File path = null;
File file = null;
EditText saveFile;
Button confirm, save;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.externaldata);


    canWrite = (TextView) findViewById(R.id.tvCanWrite);
    canRead = (TextView) findViewById(R.id.tvCanRead);
    confirm = (Button) findViewById(R.id.bConfirmSaveAs);
    save = (Button) findViewById(R.id.bSaveFile);
    saveFile = (EditText) findViewById(R.id.etSaveAs);
    confirm.setOnClickListener(this);
    save.setOnClickListener(this);

    checkState();

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(ExternalData.this, android.R.layout.simple_spinner_item, paths);

    spinner = (Spinner) findViewById(R.id.spinner1);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);


}

private void checkState() {
    // TODO Auto-generated method stub
    state = Environment.getExternalStorageState();
    if (state.equals(Environment.MEDIA_MOUNTED)) {
        // read and write
        canWrite.setText("true");
        canRead.setText("true");
        canW = canR = true;

    } else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
        // read but can't write
        canWrite.setText("false");
        canRead.setText("true");
        canW = false;
        canR = true;

    } else {
        canWrite.setText("false");
        canRead.setText("false");
        canW = canR = false;
    }

}



@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()){
    case R.id.bSaveFile:
        String f = saveFile.getText().toString();
        file = new File(path, f + "png");
        checkState();
        if (canW == canR == true){
            path.mkdir();
            try {
                InputStream is =    
      getResources().openRawResource(R.drawable.greenball);
                OutputStream os = new FileOutputStream(file);
                byte[] data = new byte[is.available()];
                is.read(data);
                os.write(data);
                is.close();
                os.close();

                Toast t = Toast.makeText(ExternalData.this, "File has been Saved", Toast.LENGTH_LONG);
                t.show();

                //Update files for the user to use
                MediaScannerConnection.scanFile(ExternalData.this,
                        new String[] {file.toString()},
                        null, 
                        new MediaScannerConnection.OnScanCompletedListener() {

                            public void onScanCompleted(String path, Uri uri) {
                                // TODO Auto-generated method stub
                                Toast t = Toast.makeText(ExternalData.this, "Scan Complete", Toast.LENGTH_SHORT);
                                t.show();
                            }
                        });


            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        break;
    case R.id.bConfirmSaveAs:
        save.setVisibility(View.VISIBLE);
        break;
    }

}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    int position = spinner.getSelectedItemPosition();
    switch (position){

    case 0:
        path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
        break;
    case 1:
        path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        break;
    case 2:
        path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        break;

    }
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

}

1 个答案:

答案 0 :(得分:1)

您正在错误的线程中调用makeToast。 尝试创建最终处理程序并发布makeToast。

final Handler handler = new Handler();
MediaScannerConnection.scanFile.....
    handler.post(new Runable()...