2014-09-22 153 views
-1

我需要在我的第一個由Eclipse開發的android應用程序中,將android文件上傳到遠程服務器。Android應用程序發送文件到遠程服務器

爲此,我將在asp網絡中使用一個web服務。

Android開發的代碼如下,但我總是提醒"Please select image"即使當我從智能手機中的圖庫中選擇圖像時,爲什麼?

我將不勝感激任何幫助,你可以給我解決這個問題。

預先感謝您。

MainActivity.java

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.ProgressDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.database.Cursor; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    ImageView viewImage; 
    Button b, upload; 
    private Bitmap bitmap; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     b = (Button) findViewById(R.id.btnSelectPhoto); 
     viewImage = (ImageView) findViewById(R.id.viewImage); 
     upload = (Button) findViewById(R.id.button1); 

     b.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       selectImage(); 
      } 
     }); 

     upload.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 

       if (bitmap == null) { 
        Toast.makeText(getApplicationContext(), 
          "Please select image", Toast.LENGTH_SHORT).show(); 
       } else { 
        ProgressDialog.show(MainActivity.this, "Uploading" 
          + bitmap, "Please wait...", true); 

       } 
      } 
     }); 
    } 

    private void selectImage() { 

     final CharSequence[] options = { "Take Photo", "Choose from Gallery", 
       "Cancel" }; 

     AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
     builder.setTitle("Add Photo!"); 
     builder.setItems(options, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int item) { 
       if (options[item].equals("Take Photo")) { 
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        File f = new File(android.os.Environment 
          .getExternalStorageDirectory(), "temp.jpg"); 
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 
        startActivityForResult(intent, 1); 
       } else if (options[item].equals("Choose from Gallery")) { 
        Intent intent = new Intent(
          Intent.ACTION_PICK, 
          android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
        startActivityForResult(intent, 2); 

       } else if (options[item].equals("Cancel")) { 
        dialog.dismiss(); 
       } 
      } 
     }); 
     builder.show(); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == RESULT_OK) { 
      if (requestCode == 1) { 
       File f = new File(Environment.getExternalStorageDirectory() 
         .toString()); 
       for (File temp : f.listFiles()) { 
        if (temp.getName().equals("temp.jpg")) { 
         f = temp; 
         break; 
        } 
       } 
       try { 
        Bitmap bitmap; 
        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 

        bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), 
          bitmapOptions); 

        viewImage.setImageBitmap(bitmap); 

        String path = android.os.Environment 
          .getExternalStorageDirectory() 
          + File.separator 
          + "Phoenix" + File.separator + "default"; 
        f.delete(); 
        OutputStream outFile = null; 
        File file = new File(path, String.valueOf(System 
          .currentTimeMillis()) + ".jpg"); 
        try { 
         outFile = new FileOutputStream(file); 
         bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile); 
         outFile.flush(); 
         outFile.close(); 
        } catch (FileNotFoundException e) { 
         e.printStackTrace(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } else if (requestCode == 2) { 

       Uri selectedImage = data.getData(); 
       String[] filePath = { MediaStore.Images.Media.DATA }; 
       Cursor c = getContentResolver().query(selectedImage, filePath, 
         null, null, null); 
       c.moveToFirst(); 
       int columnIndex = c.getColumnIndex(filePath[0]); 
       String picturePath = c.getString(columnIndex); 
       c.close(); 
       Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath)); 
       Log.w("path of image from gallery......******************.........", 
         picturePath + ""); 
       viewImage.setImageBitmap(thumbnail); 
      } 
     } 
    } 
} 

回答

1

onActivityResult你創建的,而不是用你是在類級別聲明,以便去除位圖的第二個聲明另一個位圖對象:

try { 
     //Bitmap bitmap; << remove 
     BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 

     bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), 
          bitmapOptions); 
    .... your code 
+0

謝謝,但錯誤不會改變...即使我在智能手機中從圖庫中選擇了圖像,我也總是提醒「請選擇圖像」 – 2014-09-23 07:40:54

相關問題