【问题标题】:Upload image from android to php server getting an error将图像从 android 上传到 php 服务器出现错误
【发布时间】:2014-03-12 08:52:33
【问题描述】:

我正在尝试将图片 Desert.png 从 android 上传到 php 服务器,但它对我不起作用,它给了我这个错误日志: E/错误:(1450):/desert.png:打开失败:EROFS(只读文件系统) 这是我的代码:

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

}

【问题讨论】:

  • 检查你在manifest.xml中添加了READ_PERMISSION
  • 我加了,还是一样的问题,你觉得我的代码有什么问题吗?
  • 发布您的 UploadFileToUrl() 代码
  • 你为什么从 doInBackground() 进程返回 null??
  • 我完全迷路了,我什至不知道我为什么要放它:(对不起

标签: php android


【解决方案1】:

也许你可以从AndroidExample点击这个链接。另一个很好的教程here。不要被代码吓到,这些是上传图片需要遵循的一些约定

【讨论】:

  • 我尝试了第一个链接的代码,但它总是显示错误源文件不存在:/mnt/sdcard/desert.png,我在DDMS中将图片推到/mnt/sdcard/下,然后重新启动模拟器,但仍然总是相同的消息。
  • 现在您可以从我的 Git 存储库下载完整的源代码-github.com/pankajgangwar/android 只需从图库中选择图片并将其上传到您的服务器-@victoria
  • 感谢 AndroidGeek 解决了这个问题 :) 但我在这个链接中有另一个问题:link
  • 您在使用 g+ 或 skype 或任何其他信使吗?我会按照你想要的方式解决你的问题-@victoria
  • AndroidGeek 你是一个 android 精灵!!谢谢它的工作 :))) 这是我的 g+ : ghabriamina@gmail.com @AndroidGeek
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-02
  • 2015-03-29
  • 2011-10-15
  • 2018-12-15
相关资源
最近更新 更多