如何将捕获的图像路径从imageview保存到mysql数据库

时间:2015-04-20 10:35:04

标签: java php android mysql database

我想知道是否有人可以帮助我我在Android上创建表单并且用户必须插入图像。应用程序打开相机,用户可以拍照并将图像设置为图像视图。我的问题是如何获取imageview的图像路径以使用php保存它的mysql数据库。

private void captureImage() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

        // start the image capture Intent
        startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
    }



    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // save file url in bundle as it will be null on screen orientation
        // changes
        outState.putParcelable("file_uri", fileUri);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        // get the file url
        fileUri = savedInstanceState.getParcelable("file_uri");
    }




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // if the result is capturing Image
        if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {


                filePath = getPath(fileUri);

                final Bitmap image = BitmapFactory.decodeFile(filePath);

                image1.setImageBitmap(image);

            } else if (resultCode == RESULT_CANCELED) {

                // user cancelled Image capture
                Toast.makeText(getApplicationContext(),
                        "No picture for your Schedule", Toast.LENGTH_SHORT)
                        .show();

            } else {
                // failed to capture image
                Toast.makeText(getApplicationContext(),
                        "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                        .show();
            }

        }
        }

2 个答案:

答案 0 :(得分:0)

我认为你已经拥有了你需要的东西。当你使用:

filePath = getPath(fileUri);

你有Uri和Path。现在有了这些信息,您应该为服务器中的savePath.php文件创建一个HttpPost。您必须在AsyncTask中发出请求。

http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient

答案 1 :(得分:0)

您可以使用java添加mysql数据库的路径......

将其插入您的代码中:

import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

Connection conn = null;
try {  
  Class.forName("com.mysql.jdbc.Driver").newInstance(); 
}catch (Exception exe) { 
  System.err.println("Unable to load driver."); 
  exe.printStackTrace();
  toReturn += "Unable to load driver."; 
} 
try {  
  String url = "jdbc:mysql://"+hostname+"/"+dbname; 
  conn = DriverManager.getConnection(url, username, password); 
  st = conn.createStatement();
}catch (SQLException sqle) { 
  System.out.println("SQLException: " + sqle.getMessage()); 
  System.out.println("SQLState: " + sqle.getSQLState()); 
  System.out.println("VendorError: " + sqle.getErrorCode()); 
  sqle.printStackTrace();
  toReturn += "Can't connect to the Server. Check Username/Password.";
}
try{
    st.execute("insert into \tablename\ values(\""+filePath+"\")");
}catch(Exception ex){
    ex.printStackTrace();
}
try {
  conn.close();
} catch (SQLException ex) {
  ex.printStackTrace();
}