Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将图像从android上载到php服务器时出错_Php_Android - Fatal编程技术网

将图像从android上载到php服务器时出错

将图像从android上载到php服务器时出错,php,android,Php,Android,我正在尝试将图片desert.png从android上传到php服务器,但它对我不起作用,它给了我以下错误日志: E/错误:(1450):/desert.png:打开失败:EROFS(只读文件系统) 这是我的代码: /** * Background Async Task to upload file * */ class UploadFileToURL extends AsyncTask<String, String, String> { /** * Befo

我正在尝试将图片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));
    }

}
/**
*要上载文件的后台异步任务
* */
类UploadFileToURL扩展异步任务{
/**
*在启动后台线程之前
*显示进度条对话框
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
显示对话框(进度条类型);
}
/**
*在后台线程中上载文件
* */
@凌驾
受保护的字符串doInBackground(字符串…f_url){
整数计数;
试一试{
URL=新URL(f_URL[0]);
URLConnection conconnection=url.openConnection();
conconnect.connect();
//这将非常有用,以便您可以显示典型的0-100%进度条
int lenghtOfFile=conconnect.getContentLength();
Log.i(“lenghtOfFile”,lenghtOfFile+”);
//下载该文件
InputStream输入=新的BufferedInputStream(url.openStream(),8192);
Log.i(“InputStream”、“InputStream”);
//输出流
OutputStream output=新文件OutputStream(“desert.png”);
Log.i(“OutputStream”、“OutputStream”);
位图Bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.desert);
ByteArrayOutputStream=新建ByteArrayOutputStream();
compress(bitmap.CompressFormat.PNG,90,stream);//压缩为所需格式。
byte[]byte_arr=stream.toByteArray();
字节数据[]=新字节[1024];
长字节=0;
while((count=input.read(byte_arr))!=-1){
输出.写入(字节\u arr,0,计数);
sentByte+=计数;
//已通过=已通过时间-前置时间;
//previosTime=已通过;
//发布进度。。。。
//在此之后,将调用onProgressUpdate
出版进度(“+(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+);
//将数据写入文件
输出.写入(数据,0,计数);
}
//冲洗输出
output.flush();
//合流
output.close();
input.close();
}捕获(例外e){
Log.e(“错误:,e.getMessage());
}
返回null;
}
/**
*更新进度条
* */
受保护的void onProgressUpdate(字符串…进度){
//设置进度百分比
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
*完成后台任务后
*关闭进度对话框
* **/
@凌驾
受保护的void onPostExecute(字符串文件\u url){
//下载文件后关闭对话框
解雇对话框(进度条类型);
//在图像视图中显示下载的图像
//从SD卡读取图像路径
//字符串imagePath=Environment.getExternalStorageDirectory().toString()+“/downloadedfile.jpg”;
//设置已下载到图像视图中
//my_image.setImageDrawable(Drawable.createFromPath(imagePath));
}
}

也许你可以从另一个不错的教程中找到这个链接。不要被代码吓到,这些是上传图像需要遵循的一些惯例

检查你在
清单.xml中添加的
读取权限
,我添加了它,但还是同样的问题,你觉得我的代码怎么样,有什么问题吗?发布你的UploadFileToUrl()代码为什么从doInBackground()过程返回null?我完全迷路了,我甚至不知道为什么要放它:(Sorry我尝试了第一个链接的代码,但它总是显示一个错误源文件不存在:/mnt/sdcard/desert.png,我将图片推到DDMS中的/mnt/sdcard/下,然后重新启动模拟器,但始终显示相同的消息。现在,您可以从我的Git存储库下载完整的源代码-只需从gallery和uploa中选择图片即可将它发送到您的服务器-@VictorianRoidGeek:)问题已经解决,多亏了您,AndroidGeek:)但我在这个链接中还有另一个问题:您是否使用g+或skype或任何其他messenger?我将完全按照您想要的方式解决您的问题-@VictorianRoidGeek您是一个android精灵!!谢谢它工作:))这是我的g+:ghabriamina@gmail.com@AndroidGeek