从Android的上传图片到PHP服务器得到一个错误 [英] Upload image from android to php server getting an error

查看:289
本文介绍了从Android的上传图片到PHP服务器得到一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想上传图片从Android的desert.png到PHP服务器,但它不工作对我来说,它给我这个错误日志:
E /错误:(1450):/desert.png:打开失败:EROFS(只读文件系统)
这是我的code:

  / **
 *背景异步任务来上传文件
 * * /
类UploadFileToURL扩展的AsyncTask<字符串,字符串,字符串> {    / **
     *启动后台线程之前
     *显示进度条对话框
     * * /
    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        的ShowDialog(progress_bar_type);
    }    / **
     在后台线程*上传文件
     * * /
    @覆盖
    保护字符串doInBackground(字符串... f_url){
        诠释计数;
        尝试{
            网址URL =新的URL(f_url [0]);
            URLConnection的连接如= url.openConnection();
            conection.connect();
            //这将是有益的,这样就可以显示tipical 0-100%的进度条
            INT lenghtOfFile = conection.getContentLength();
            Log.i(lenghtOfFile,lenghtOfFile +);
            //下载文件
           InputStream的输入=新的BufferedInputStream(url.openStream(),8192);
            Log.i(InputStream的,InputStream的);
            //输出流
            OutputStream的输出=新的FileOutputStream(desert.png);
            Log.i(OutputStream的,的OutputStream);
            位图位图= BitmapFactory.de codeResource(getResources(),R.drawable.desert);
            ByteArrayOutputStream流=新ByteArrayOutputStream();
            bitmap.com preSS(Bitmap.Com pressFormat.PNG,90,流); // COM preSS你想要的格式。
            字节[] = byte_arr stream.toByteArray();            字节的数据[] =新的字节[1024];            长sentByte = 0;
            而((计数= input.read(byte_arr))!= -1){
                 output.write(byte_arr,0,计数);
                 sentByte + =计数;
              //通过= passedTime - previosTime;
               // previosTime =通过;                //发布进度....
                //在此之后onProgressUpdate将被称为
                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 +);
                //将数据写入到文件
               output.write(数据0,计);
            }
            //冲洗输出
            output.flush();            //关闭流
           output.close();
            input.close();        }赶上(例外五){
            Log.e(错误,e.getMessage());
        }        返回null;
    }    / **
     *更新进度条
     * * /
    保护无效onProgressUpdate(字符串...进度){
        //设置进度百分比
        pDialog.setProgress(的Integer.parseInt(进展[0]));
   }    / **
     *在完成后台任务之后
     *辞退进度对话框
     * ** /
    @覆盖
    保护无效onPostExecute(字符串FILE_URL){
        //关闭该对话框的文件下载后
        dismissDialog(progress_bar_type);        //显示下载的图像到图像视图
        //从SD卡读取图像路径
     //字符串的ImagePath = Environment.getExternalStorageDirectory()的toString()+/downloadedfile.jpg。
        //设置下载到图像视图
     // my_image.setImageDrawable(Drawable.createFromPath(的ImagePath));
    }}


解决方案

可能是你可以按照从<一此链接href=\"http://androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106\"相对=nofollow> AndroidExample 。另外一个不错的教程中的这里 .Don't得到了code发毛,这些都是一些你需要遵循上传图片约定

I'm trying to upload a picture desert.png from android to a php server ,but it doesn't work for me ,and it gives me this error log : E/Error:(1450): /desert.png: open failed: EROFS (Read-only file system) this is my code :

/**
 * 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));
    }

}

解决方案

may be you can follow this link from AndroidExample.Another nice tutorial here.Don't get scared from the code,those are some conventions that you need to follow to upload image

这篇关于从Android的上传图片到PHP服务器得到一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆