Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.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
使用AsyncTask从android将摄像头拍摄的图像通过php上传到网页_Php_Android_Image_Android Asynctask_Camera - Fatal编程技术网

使用AsyncTask从android将摄像头拍摄的图像通过php上传到网页

使用AsyncTask从android将摄像头拍摄的图像通过php上传到网页,php,android,image,android-asynctask,camera,Php,Android,Image,Android Asynctask,Camera,我正试图通过PHP将图像上传到weabpage,但这是第一次,所以我遇到了一些困难。 我想上传的图像应该是一个用相机拍摄的图像。 所以,我有一个按钮来捕获图像,还有一个按钮用来上传(Post),请参见代码 我现在有两个问题。 1) 图像未上载到页面。 2) 相机正常工作,但在我使用它之后,post按钮或拍摄新照片都不起作用 有关MainActivity.java、activity_main.xml和php,请参见下文。 (我的清单中有互联网和摄像头权限) 如果有人能帮助我,我将非常感激 Main

我正试图通过PHP将图像上传到weabpage,但这是第一次,所以我遇到了一些困难。 我想上传的图像应该是一个用相机拍摄的图像。 所以,我有一个按钮来捕获图像,还有一个按钮用来上传(Post),请参见代码

我现在有两个问题。 1) 图像未上载到页面。 2) 相机正常工作,但在我使用它之后,post按钮或拍摄新照片都不起作用

有关MainActivity.java、activity_main.xml和php,请参见下文。 (我的清单中有互联网和摄像头权限)

如果有人能帮助我,我将非常感激

MainActivity.java:

public class MainActivity extends ActionBarActivity implements LocationListener {

Button Post, TakePic;

private static int RESULT_LOAD_IMAGE = 1;
ImageView imgView;

@Override
protected void onCreate(Bundle savedInstanceStatea) {
    super.onCreate(savedInstanceStatea);
    setContentView(R.layout.activity_main); 

    final ImageView imgView = (ImageView) findViewById(R.id.imageViewPic);
    Drawable  drawable  = getResources().getDrawable(R.drawable.ic_launcher);
    imgView.setImageDrawable(drawable);

    if (savedInstanceStatea == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new Fragment()).commit();  

        TakePic = (Button) findViewById(R.id.buttonTakePic);
        Post = (Button) findViewById(R.id.buttonPost);

        TakePic.setOnClickListener(new View.OnClickListener() {             
            @Override
            public void onClick(View arg0) {            
                Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(takePicture, RESULT_LOAD_IMAGE);
            }
        });             

        Post.setOnClickListener(new View.OnClickListener() {                
            @Override
            public void onClick(View arg0) {
                new UploadImageTask(imgView).execute();  
            }
        });                     
    }
}       

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);         

    if (resultCode == RESULT_OK && null != data) {
        Bitmap picture = (Bitmap) data.getExtras().get("data");                        
        ImageView imageView = (ImageView) findViewById(R.id.imageViewPic);
        imageView.setImageBitmap(picture);          
    }       
}

private class UploadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView imageView = (ImageView) findViewById(R.id.imageViewPic);

    public UploadImageTask(ImageView imgView) {     
    }

    protected Bitmap doInBackground(String... urls) {

        //Fetch the image in the image view
        Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

        try {  
        //Create a file to put the bitmap on:
        File f = new File("test.jpg");
        f.createNewFile();          

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        byte[] byteArray = bos.toByteArray();

        //write the bytes in file:
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(byteArray);           

          //String fileName = sourceFileUri;          
          HttpURLConnection conns = null;
          DataOutputStream dos = null;
          String lineEnd = "\r\n";
          String twoHyphens = "--";
          String boundary = "*****";

          //open a URL connection to the Server
          URL url = new URL("http:////ADRESS TO MY PAGE.php");

          //Open a HTTP  connection to  the URL
          conns = (HttpURLConnection) url.openConnection(); 
          conns.setDoInput(true);
          conns.setDoOutput(true);
          conns.setUseCaches(false);
          conns.setRequestMethod("POST");
          conns.setRequestProperty("Connection", "Keep-Alive");
          conns.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); 

          dos = new DataOutputStream(conns.getOutputStream());

          dos.writeBytes(twoHyphens + boundary + lineEnd);
          dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + fos + "\"" + lineEnd);                            
          dos.writeBytes(lineEnd);     

          dos.flush();
          dos.close();
          fos.close();
        } catch (Exception e) {

        }
        return bitmap;          
    }

    protected void onPostExecute(Bitmap result) {
        ImageView imgView = (ImageView) findViewById(R.id.imageViewPic1);
        imgView.setImageBitmap(result);
    }
}

@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}   
}

由于PHP服务器中的执行时间和文件大小限制,PHP上载可能无法工作。所以试着在android中使用FTP上传

示例链接:

注意:您也可以在后台使用AsyncTask执行此过程

您也可以在PHP服务器中增加文件上传的执行时间和大小


感谢快乐编码:)

您必须告诉自己的代码哪里出错了,哪里有错误。现在从点击post按钮开始。应该发生什么,而不是发生什么?你要上传哪张图片?当我点击post按钮时,我想上传第一张图片视图中显示的图片。如果我用这个相机,这个就是刚才拍到的。若我先点击post,那个么它将只是我添加的默认图像,只是为了简单起见。第二个图像视图将显示相同的图像,同时,我使用这个函数只是暂时尝试一下。问题是,我不知道这些错误,对不起,我是新来的…当点击post按钮时,不要谈论相机和其他事情,因为这一切在那一刻都是不相关的。“单击post按钮,我启动一个异步任务,从imageview中获取图像,然后…”(现在告诉所有您接下来要做的事情。解释代码流)。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusableInTouchMode="true" >

           <Button
                android:id="@+id/buttonTakePic"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:textColor="#ffffff" 
                android:text="Take picture" />  

           <Button
                android:id="@+id/buttonPost"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:textColor="#ffffff" 
                android:text="POST!" />       

        <ImageView
            android:id="@+id/imageViewPic"
            android:layout_width="match_parent"
            android:layout_margin="5dp"
            android:layout_height="300dp"            
            android:src="@drawable/abc_ab_bottom_transparent_light_holo" />        

        <ImageView
            android:id="@+id/imageViewPic1"
            android:layout_width="match_parent"
            android:layout_margin="5dp"
            android:layout_height="300dp"            
            android:src="@drawable/abc_ab_bottom_transparent_light_holo" />   

    </LinearLayout>

    </ScrollView>
$uploaddir = 'upload/';
$file = basename($_FILES['uploadedfile']['name']);
$uploadfile = $uploaddir . $file;

echo "file=".$file; //is empty, but shouldn't

if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $uploadfile)) {
echo $file;
}
else {
echo "error";