我需要從android項目上傳圖像到服務器文件夾和URL到MS SQL數據庫表。上傳圖像到文件夾和圖像路徑到MS SQL Server數據庫的Android代碼
我有一些代碼來捕捉圖像或從圖庫中選擇,現在我需要上傳圖像到服務器文件夾及其URL表
我需要從android項目上傳圖像到服務器文件夾和URL到MS SQL數據庫表。上傳圖像到文件夾和圖像路徑到MS SQL Server數據庫的Android代碼
我有一些代碼來捕捉圖像或從圖庫中選擇,現在我需要上傳圖像到服務器文件夾及其URL表
下面的代碼登錄MS SQL,把它作爲一個參考在表格中,你想使用應該是你進入 即「DB」默認數據庫用戶數據庫更新URL應該是「取消」
public class MainActivity extends ActionBarActivity
{
// Declaring layout button, edit texts
Button login;
EditText username,password;
ProgressBar progressBar;
// End Declaring layout button, edit texts
// Declaring connection variables
Connection con;
String un,pass,db,ip;
//End Declaring connectaaion variables
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting values from button, texts and progress bar
login = (Button) findViewById(R.id.button);
username = (EditText) findViewById(R.id.editText);
password = (EditText) findViewById(R.id.editText2);
//progressBar = (ProgressBar) findViewById(R.id.progressBar);
// End Getting values from button, texts and progress bar
// Declaring Server ip, username, database name and password
ip = "yourIP";
db = "DatabaseName";
un = "userName";
pass = "password";
// Declaring Server ip, username, database name and password
// Setting up the function when button login is clicked
login.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
CheckLogin checkLogin = new CheckLogin();// this is the Asynctask, which is used to process in background to reduce load on app process
checkLogin.execute(username.getText().toString(),password.getText().toString());
}
});
//End Setting up the function when button login is clicked
}
public class CheckLogin extends AsyncTask<String,String,String>
{
String z = "";
Boolean isSuccess = false;
@Override
protected void onPreExecute()
{
//progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(String r)
{
//progressBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, r, Toast.LENGTH_SHORT).show();
if(isSuccess)
{
Toast.makeText(MainActivity.this , "Login Successfull" , Toast.LENGTH_LONG).show();
//finish();
Intent intent = new Intent(MainActivity.this, HomePage.class);
startActivity(intent);
finish();
}
}
@Override
protected String doInBackground(String... params)
{
String usernam = params[0];
String passwordd = params[1];
if(usernam.trim().equals("")|| passwordd.trim().equals(""))
z = "Please enter Username and Password";
else
{
try
{
con = connectionclass(un, pass, db, ip); // Connect to database
if (con == null)
{
z = "Check Your Internet Access!";
}
else
{
// Change below query according to your own database.
//String query ="select * from dbo.AA_UserMaster where UserName= 'test' and Password = 'admin1*'";
String query = "select UserId from [AA_UserMaster] where UserName= '" + usernam.toString() + "' and Password = '"+ passwordd.toString() +"' ";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if(rs.next())
{
z = "Login successful";
isSuccess=true;
con.close();
}
else
{
z = "Invalid Credentials!";
isSuccess = false;
}
}
}
catch (Exception ex)
{
isSuccess = false;
z = ex.getMessage();
}
}
return z;
}
}
@SuppressLint("NewApi")
public Connection connectionclass(String user, String password, String database, String server)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection connection = null;
String ConnectionURL = null;
try
{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";" + database + ";user=" + user+ ";password=" + password + ";";
connection = DriverManager.getConnection(ConnectionURL);
}
catch (SQLException se)
{
Log.e("error here 1 : ", se.getMessage());
}
catch (ClassNotFoundException e)
{
Log.e("error here 2 : ", e.getMessage());
}
catch (Exception e)
{
Log.e("error here 3 : ", e.getMessage());
}
return connection;
}
}
使用此功能的默認數據庫:
private void setImage(String path, CircleImageView img) {
options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.showImageOnLoading(R.drawable.user)
.showImageForEmptyUri(R.drawable.user)
.showImageOnFail(R.drawable.user)
.build();
ImageLoader.getInstance().displayImage(path, img, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
super.onLoadingStarted(imageUri, view);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
super.onLoadingFailed(imageUri, view, failReason);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
super.onLoadingCancelled(imageUri, view);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String s, View view, int i, int i1) {
}
});
檢查上傳[此鏈接](http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/)將圖像上傳到服務器 – Jaydroid