SDカードから音楽を再生していますが、ファイルは存在しますが、音楽ファイルを再生していますが、存在しない場合、アプリがクラッシュします。どうすればよいですか?
package com.example.downloadplay;
public class AudioPlayer extends Activity implements OnClickListener {
Button playButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
playButton = (Button) this.findViewById(R.id.ButtonPlayStop);
playButton.setOnClickListener(this);
}
public void onClick(View v) {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
File sdcard = Environment.getExternalStorageDirectory();
File audioFile = new File(sdcard.getPath() + "/bluetooth/یه سوال دارم مگه.mp3");
intent.setDataAndType(Uri.fromFile(audioFile), "audio/mp3");
startActivity(intent);
}
}
ファイルが存在するかどうかを確認します
if(audioFile.exists())
{
intent.setDataAndType(Uri.fromFile(audioFile), "audio/mp3");
startActivity(intent);
}
else
{
// show error
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加