ファイルが見つからないというエラーが発生し続けます。コードをシミュレートするためにAndroidエミュレーターを使用しました
java.io.FileNotFoundException :/mnt/sdcard/test.txt: オープンに失敗しました: EACCES (許可が拒否されました) オープンに失敗しました
助けてください..ありがとう
public class TestUpload extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_upload);
final TextView tmp = (TextView) findViewById(R.id.textView1);
tmp.setText("Hi! Click the button!");
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
File f = new File("/mnt/sdcard/test.txt");
try {
f.createNewFile();
Date d = new Date();
PrintWriter writer = new PrintWriter(f);
writer.println(d.toString());
writer.close();
HttpClient client = new DefaultHttpClient();
httpPostFileUpload(client, "/mnt/sdcard/test.txt", "http://localhost/upload.php", "uploadedfile");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void httpPostFileUpload(HttpClient client,String filePath,String uploadUri,String inputNameAttr) throws ClientProtocolException,IOException {
HttpUriRequest request = new HttpPost(uploadUri);
MultipartEntity form = new MultipartEntity();
client.getParams().setBooleanParameter("http.protocol.expect-continue", false);
form.addPart(inputNameAttr, new FileBody(new File(filePath)));
((HttpEntityEnclosingRequestBase) request).setEntity(form);
try {
client.execute(request);
} catch (ClientProtocolException e) {
throw e;
} catch (IOException ee) {
throw ee;
}
}
}