从Android上传图像到php服务器得到错误
我想从android上传一张图片desert.png到php服务器,但它不适合我,它给了我这个错误日志: E /错误:(1450):/desert.png:打开失败:EROFS(只读文件系统) 这是我的代码:从Android上传图像到php服务器得到错误
/**
* Background Async Task to upload file
* */
class UploadFileToURL extends AsyncTask<String, String, String> {
/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
/**
* Uploading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conection.getContentLength();
Log.i("lenghtOfFile",lenghtOfFile+"");
// download the file
InputStream input = new BufferedInputStream(url.openStream(), 8192);
Log.i("InputStream","InputStream");
// Output stream
OutputStream output = new FileOutputStream("desert.png");
Log.i("OutputStream","OutputStream");
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.desert);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
byte [] byte_arr = stream.toByteArray();
byte data[] = new byte[1024];
long sentByte = 0;
while ((count = input.read(byte_arr)) != -1) {
output.write(byte_arr, 0, count);
sentByte += count;
// passed=passedTime -previosTime ;
// previosTime = passed ;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((sentByte*100)/lenghtOfFile));
Log.i("log_lenghtOfFile",lenghtOfFile+"");
Log.i("log_total",sentByte+"");
Log.i("log_ourcentage",(int)((sentByte*100)/lenghtOfFile)+"");
// Log.i("log_Debit",passedTime +"");
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
// Displaying downloaded image into image view
// Reading image path from sdcard
// String imagePath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.jpg";
// setting downloaded into image view
// my_image.setImageDrawable(Drawable.createFromPath(imagePath));
}
}
可能是你可以按照从AndroidExample。另外一个很好的教程here此链接。不要害怕代码,这些是你需要遵循的一些约定上传图像
我尝试了第一个链接的代码,但它总是显示一个错误源文件不存在:/mnt/sdcard/desert.png,我将图片放在DDMS中的/ mnt/sdcard /下,并重新启动模拟器,但始终是相同的消息。 – victoria
现在,您可以从我的Git存储库下载完整的源代码-http://github.com/pankajgangwar/android只需从图库中选择图片并将其上传到您的服务器 - @ victoria – Pankaj
感谢您解决了这个问题AndroidGeek: )但我有这个链接的另一个问题:[链接](http://stackoverflow.com/questions/22377470/android-4-3-httpurlconnection-no-authentication-challenges-found) – victoria
检查你在'manifest.xml'中添加了'READ_PERMISSION' –
我加了它,但仍然是同样的问题,你对我的代码有什么看法,是不是有问题? – victoria
发布UploadFileToUrl()代码 – Pankaj