2014-10-31 36 views
0

我有這個代碼發送圖像從android到服務器,它的工作原理! 但有時它給我回一個錯誤的logcat:有時候給我跳過57幀!該應用程序可能在其主線程上做了太多的工作

Skipped 57 frames! The application may be doing too much work on its main thread 

但我不知道只是有時和例如,在安裝了這個應用程序不要電話機T工作得很好,當這種情況發生!

public class SendingImage extends Activity { 

     private static final int REQUEST_IMAGE = 100; 

     TextView tvPath; 
     TextView txtHaut; 
     ImageView preview; 
     File destination; 
     String imagePath; 
     ImageButton takePhoto; 
     Button btnCreate; 

     int serverResponseCode = 0; 
     ProgressDialog dialog = null; 

     String upLoadServerUri = null; 

     String name;  

     private static final int CAMERA_REQUEST = 1; 
     private static final int PICK_FROM_GALLERY = 2; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      // TODO Auto-generated method stub 
      super.onCreate(savedInstanceState); 

      setContentView(R.layout.activity_sending_image); 

      preview = (ImageView) findViewById(R.id.imageViewPreview); 
      btnCreate = (Button) findViewById(R.id.bInviaFoto) ; 
      txtHaut = (TextView) findViewById(R.id.textViewEsito); 
      takePhoto = (ImageButton) findViewById(R.id.bCamera); 
      preview.setVisibility(View.GONE); 
      txtHaut.setVisibility(View.GONE); 

      upLoadServerUri = "http://fiezzo.altervista.org/upload_image.php"; 

      name = dateToString(new Date(),"yyyyMMddhhmmss"); 
      destination = new File(Environment.getExternalStorageDirectory(), name + ".jpg"); 


      takePhoto.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination)); 
        startActivityForResult(intent, REQUEST_IMAGE); 
       } 
      }); 


      btnCreate.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        dialog = ProgressDialog.show(SendingImage.this, "", "Uploading file...", true); 

        new Thread(new Runnable() { 
         public void run() {    
          uploadFile(imagePath); 
         } 
        }).start();   
       } 

      }); 



     } 


     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      if(requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK){ 
       try { 
        preview.setVisibility(View.VISIBLE); 
        takePhoto.setVisibility(View.GONE); 
        txtHaut.setVisibility(View.VISIBLE); 
        txtHaut.setText("Immagine selezionata correttamente!"); 

        FileInputStream in = new FileInputStream(destination); 
        BitmapFactory.Options options = new BitmapFactory.Options(); 
        options.inSampleSize = 15; 
        imagePath = destination.getAbsolutePath(); 
        Log.d("INFO", "PATH === " +imagePath); 
        //tvPath.setText(imagePath); 
        Bitmap bmp = BitmapFactory.decodeStream(in, null, options); 
        preview.setImageBitmap(bmp); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } 

      } 
      else{ 
       tvPath.setText("Request cancelled"); 
      } 
     } 


     /** 
     * open camera method 
     */ 
     public void callCamera() { 
      Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      cameraIntent.putExtra("crop", "true"); 
      cameraIntent.putExtra("aspectX", 0); 
      cameraIntent.putExtra("aspectY", 0); 
      cameraIntent.putExtra("outputX", 200); 
      cameraIntent.putExtra("outputY", 150); 
      startActivityForResult(cameraIntent, CAMERA_REQUEST); 

     } 

     /** 
     * open gallery method 
     */ 

     public void callGallery() { 
      Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      intent.putExtra("crop", "true"); 
      intent.putExtra("aspectX", 0); 
      intent.putExtra("aspectY", 0); 
      intent.putExtra("outputX", 200); 
      intent.putExtra("outputY", 150); 
      intent.putExtra("return-data", true); 
      startActivityForResult(
        Intent.createChooser(intent, "Complete action using"), 
        PICK_FROM_GALLERY); 

     } 

     public String dateToString(Date date, String format) { 
      SimpleDateFormat df = new SimpleDateFormat(format); 
      return df.format(date); 
     } 

     public int uploadFile(String sourceFileUri) { 

      String fileName = sourceFileUri; 

      HttpURLConnection conn = null; 
      DataOutputStream dos = null; 
      String lineEnd = "\r\n"; 
      String twoHyphens = "--"; 
      String boundary = "*****"; 
      int bytesRead, bytesAvailable, bufferSize; 
      byte[] buffer; 
      int maxBufferSize = 1 * 1024 * 1024; 
      File sourceFile = new File(sourceFileUri); 

      if (!sourceFile.isFile()) { 
       dialog.dismiss(); 
       Log.e("uploadFile", "Source File not exist :" +imagePath); 
       return 0; 
      } else { 
       try { 

        // open a URL connection to the Servlet 
        FileInputStream fileInputStream = new FileInputStream(sourceFile); 
        URL url = new URL(upLoadServerUri); 

        // Open a HTTP connection to the URL 
        conn = (HttpURLConnection) url.openConnection(); 
        conn.setDoInput(true); // Allow Inputs 
        conn.setDoOutput(true); // Allow Outputs 
        conn.setUseCaches(false); // Don't use a Cached Copy 
        conn.setRequestMethod("POST"); 
        conn.setRequestProperty("Connection", "Keep-Alive"); 
        conn.setRequestProperty("ENCTYPE", "multipart/form-data"); 
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); 
        conn.setRequestProperty("uploaded_file", fileName); 

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

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

        // create a buffer of maximum size 
        bytesAvailable = fileInputStream.available(); 

        bufferSize = Math.min(bytesAvailable, maxBufferSize); 
        buffer = new byte[bufferSize]; 

        // read file and write it into form... 
        bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

        while (bytesRead > 0) { 

         dos.write(buffer, 0, bufferSize); 
         bytesAvailable = fileInputStream.available(); 
         bufferSize = Math.min(bytesAvailable, maxBufferSize); 
         bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

        } 

        // send multipart form data necesssary after file data... 
        dos.writeBytes(lineEnd); 
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

        // Responses from the server (code and message) 
        serverResponseCode = conn.getResponseCode(); 
        String serverResponseMessage = conn.getResponseMessage(); 

        Log.i("uploadFile", "HTTP Response is : "+ serverResponseMessage + ": " + serverResponseCode); 

        if(serverResponseCode == 200){ 

         runOnUiThread(new Runnable() { 
          public void run() { 

           Toast.makeText(SendingImage.this, "File Upload Complete.", Toast.LENGTH_SHORT).show(); 
          } 
         });     
        }  

        //close the streams // 
        fileInputStream.close(); 
        dos.flush(); 
        dos.close(); 

       } catch (MalformedURLException ex) { 

        dialog.dismiss(); 
        ex.printStackTrace(); 

        runOnUiThread(new Runnable() { 
         public void run() { 

          Toast.makeText(SendingImage.this, "MalformedURLException", Toast.LENGTH_SHORT).show(); 
         } 
        }); 

        Log.e("Upload file to server", "error: " + ex.getMessage(), ex); 
       } catch (Exception e) { 

        dialog.dismiss(); 
        e.printStackTrace(); 

        runOnUiThread(new Runnable() { 
         public void run() { 

          Toast.makeText(SendingImage.this, "Got Exception : see logcat ", Toast.LENGTH_SHORT).show(); 
         } 
        }); 
        Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e); 
       } 
       dialog.dismiss(); 
       return serverResponseCode; 

      } // End else block 
     } 

    } 

這裏也PHP代碼:

<?php 
    $file_path = "uploads/"; 

    $file_path = $file_path . basename($_FILES['uploaded_file']['name']); 
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) { 
     echo "success"; 
    } else{ 
     echo "fail"; 
    } 
?> 

I M使用爲例項目: http://androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106

非常感謝您的幫助或建議!

回答

0

看起來好像你開始爲自己上傳的,這是很好的一個新的線程。但是這位

Bitmap bmp = BitmapFactory.decodeStream(in, null, options); 

正在主線程上執行。你應該考慮將其轉移到背景中,以免在主線程中讀取圖像。這可能是你的問題。

更一般地,考慮使用AsyncTask這些東西:它更加靈活,不易出錯,比創建和管理自己的線程。

它也有地方允許在後臺工作完成後,你做在主線程上的東西什麼的。因此,使用您的Bitmap,您可以在後臺線程上執行doInBackground()方法中的加載和decodeStream()調用;然後下面一行

preview.setImageBitmap(bmp); 

是你在onPostExecute()做什麼,這恰好(自動)在主線程,因爲你必須與主線程上的GUI組件進行交互。

+0

我的AsyncTask tryed,但它不能正常工作DRO我的版本! 你有建議或幫助嗎? 非常感謝 – 2014-10-31 16:02:50

+0

@MarcC如果你的意思是你使用的是舊版本的Android不具有其提供,從Android支持利布斯嘗試'CompatAsyncTask'。 – 2014-10-31 16:04:01

+0

它可能幫助嗎?在過去的7個小時裏,我沒有意識到在asynctask中...... – 2014-10-31 23:13:36

相關問題