[英]Why Android Studio again and again show ERROR: Failed to open zip file. even when i am crating a new project?
[英]Android: Reading a file: Does not open the first time I select the file. When I try it again it opens the file
這就是我的代碼。 發生的情況是,當我第一次啟動該應用程序時,我嘗試選擇一個文件,並且resultCode為0。第二次單擊按鈕以打開資源管理器時,我選擇了文件,並且resultCode為-1,因此成功打開文件。 知道第一次發生什么嗎? 我似乎無法理解為什么它會給我代碼0? 多謝你們。 PS這是可怕的編碼,但我只是想了解Android中的文件打開過程。 謝謝。
public class MainActivity extends Activity implements OnClickListener {
final int ACTIVITY_CHOOSE_FILE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button my_button = (Button) findViewById(R.id.activity);
my_button.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/");
startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.v("onActivityResult","requestCode is: " + requestCode + " resultCode" + resultCode);
if ((requestCode == ACTIVITY_CHOOSE_FILE) && (resultCode == RESULT_OK)) {
Log.v("onActivityResult","Passes through if statement");
//String fileSelected = data.getStringExtra("fileSelected");
String FilePath = data.getData().getPath();
TextView my_text = (TextView) findViewById(R.id.textView1);
my_text.setText(FilePath);
StringBuilder text = null;
try{
Scanner input = new Scanner(FilePath);
//File dir = Environment.getExternalStorageDirectory();
//File yourFile = new File(dir, FilePath);
//Read text from file
text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(FilePath));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
}
catch (IOException e) {
//You'll need to add proper error handling here
}
}catch(Exception e){
Log.v("file not opened","THE FILE WAS NOT OPENED");
}
my_text.setText(text);
}
}
}
這些錯誤是第三方錯誤,請嘗試在其他設備上使用該應用程序或使用模擬器,然后查看其運行情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.