java - Android application sends a file to a remote server -


i need in first android application, developed eclipse, upload file in android remote server.

to use web service in asp net.

the code android developed below, have alert "please select image" when have selected image gallery in smartphone, why?

i appreciate can give me in working problem.

thank in advance.

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 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 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());                 (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 gallery......******************.........",                         picturepath + "");                 viewimage.setimagebitmap(thumbnail);             }         }     } } 

inside onactivityresult creating bitmap object instead of using declaring @ class level remove second declaration of bitmap:

try {       //bitmap bitmap;  << remove         bitmapfactory.options bitmapoptions = new bitmapfactory.options();        bitmap = bitmapfactory.decodefile(f.getabsolutepath(),                             bitmapoptions);    .... code 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -