如何获得imageview图像的完整大小?

时间:2014-09-30 21:55:51

标签: android imageview

我在imageview控件中显示了一个图像,但是当我尝试将图像发送到远程服务器,php服务器时,存储在服务器中的图像是最小化版本,没有在imageview控件中捕获的全尺寸图像

public void onClick(View v) {
            // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
            Uri tempUri = getImageUri(getApplicationContext(), imagen);
            imagepath = getRealPathFromURI(tempUri);
          Log.i("uploadFile", "File Path : "
                    + imagepath);
            // CALL THIS METHOD TO GET THE ACTUAL PATH
            //File finalFile = new File(getRealPathFromURI(tempUri));

            dialog = ProgressDialog.show(DisplayImgCamActivity.this, "", "Subiendo imagen...", true);
             //messageText.setText("uploading started.....");
             new Thread(new Runnable() {
                 public void run() {
                      uploadFile(imagepath);           
                 }
               }).start();              
        }

public int uploadFile(String sourceFileUri) {        
    String fileName = sourceFileUri;

    //Log.i("uploadFile", "File Uri : "
      //      + fileName);


    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("SubirImagen", "No existe la imagen :" + imagepath);

         runOnUiThread(new Runnable() {
             public void run() {
                 //messageText.setText("Source File not exist :"+ imagepath);
                 Toast.makeText(DisplayImgCamActivity.this, "No existe imagen a subir", Toast.LENGTH_LONG).show();
             }
         }); 

         return 0;

    }
    else
    {
         try { 

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

             // 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() {
                          String msg = "File Upload Completed.\n\n See uploaded file here : \n\n"
                                +" F:/wamp/wamp/www/uploads";
                          //messageText.setText(msg);
                          Toast.makeText(DisplayImgCamActivity.this, "Imagen subida satisfactoriamente", Toast.LENGTH_LONG).show();
                      }
                  });                
             }    

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

        } catch (MalformedURLException ex) {

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

            runOnUiThread(new Runnable() {
                public void run() {
                    //messageText.setText("MalformedURLException Exception : check script url.");
                    Toast.makeText(DisplayImgCamActivity.this, "MalformedURLException", Toast.LENGTH_LONG).show();
                }
            });

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

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

            runOnUiThread(new Runnable() {
                public void run() {
                    //messageText.setText("Got Exception : see logcat ");
                    Toast.makeText(DisplayImgCamActivity.this, "Error: ver logcat ", Toast.LENGTH_LONG).show();
                }
            });
            Log.e("Upload file to server Exception", "Exception : "  + e.getMessage(), e);  
        }
        dialog.dismiss();       
        return serverResponseCode; 

     } // End else block 
   }    

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null); 
    cursor.moveToFirst(); 
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
    return cursor.getString(idx); 
}

2 个答案:

答案 0 :(得分:0)

不知道我是否理解了这个问题,但是你发送的是一个在imageview中加载的图像,并且是一个php,它的大小比imageview中显示的要小,如果是这样的话,那么您在应用程序中拥有的图像很小,如果您可以帮助解决方案,那么会发生相同的缩放图像视图,对不起我的英文

编辑:我的意思是图像的外观如果相同则没有任何修改

答案 1 :(得分:0)

好吧,最后我可以结束剧本了。最后,我选择了智能手机中存储图像的完整路径,而不是图像视图中显示的图像。在这种情况下,我可以获得我想要的完整图像。图像视图中显示的图像是存储在手机中的图像的最小化版本。