我在用这个
https://www.youtube.com/watch?v=EmOzhbGFEAk&t=1118s
这是我的主要活动课
public class MainActivity extends AppCompatActivity {
EditText editName, editfatherName, editCNIC, editReview, editRegistration;
Button btn_add, btn_retrieve;
ImageView takePhoto, openGallery, UploadImage, ivImage;
CameraPhoto cameraPhoto;
GalleryPhoto galleryPhoto;
final int CAMERA_REQUEST = 13323;
final int GALLERY_REQUEST=22131;
String SelectedPhoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cameraPhoto=new CameraPhoto(getApplicationContext());
galleryPhoto=new GalleryPhoto(getApplicationContext());
takePhoto=(ImageView)findViewById(R.id.Takephoto);
openGallery=(ImageView)findViewById(R.id.OpenGallery);
UploadImage=(ImageView)findViewById(R.id.Image_upload);
ivImage=(ImageView)findViewById(R.id.image);
takePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
startActivityForResult(cameraPhoto.takePhotoIntent(),CAMERA_REQUEST);
} catch (IOException e) {
Toast.makeText(MainActivity.this, "Something wrong while taking photos", Toast.LENGTH_SHORT).show();
}
}
});
openGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivityForResult(galleryPhoto.openGalleryIntent(),GALLERY_REQUEST);
}
});
UploadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
Bitmap bitmap= ImageLoader.init().from(SelectedPhoto).requestSize(1024,1024).getBitmap();
String encryptString=BitMapToString(bitmap);
Bitmap image=StringToBitmap(encryptString);
String encodedImage = ImageBase64.encode(bitmap);
Log.d("UploadImaes",encodedImage);
HashMap<String,String> postData = new HashMap<String, String>();
postData.put("image",encodedImage);
PostResponseAsyncTask task = new PostResponseAsyncTask(MainActivity.this, postData, new AsyncResponse() {
@Override
public void processFinish(String s) {
if(s.contains("uploaded_success"))
{
Toast.makeText(MainActivity.this, "Image Upload Successfully", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this, "Image Upload Failed", Toast.LENGTH_SHORT).show();
}
}
});
task.execute("http://localhost/news/upload.php");
task.setEachExceptionsHandler(new EachExceptionsHandler() {
@Override
public void handleIOException(IOException e) {
Toast.makeText(MainActivity.this, "Cannot Connect to Server 1", Toast.LENGTH_SHORT).show();
}
@Override
public void handleMalformedURLException(MalformedURLException e) {
Toast.makeText(MainActivity.this, "Cannot Connect to Server 2", Toast.LENGTH_SHORT).show();
}
@Override
public void handleProtocolException(ProtocolException e) {
Toast.makeText(MainActivity.this, "Cannot Connect to Server 3", Toast.LENGTH_SHORT).show();
}
@Override
public void handleUnsupportedEncodingException(UnsupportedEncodingException e) {
Toast.makeText(MainActivity.this, "Cannot Connect to Server 4", Toast.LENGTH_SHORT).show();
}
});
} catch (FileNotFoundException e) {
Toast.makeText(MainActivity.this, "Something wrong while encoding photos", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode==RESULT_OK)
{
if(requestCode==CAMERA_REQUEST)
{
String photoPath =cameraPhoto.getPhotoPath();
SelectedPhoto=photoPath;
try {
Bitmap bitmap= ImageLoader.init().from(photoPath).requestSize(1024,1024).getBitmap();
ivImage.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Toast.makeText(MainActivity.this, "Something wrong while loading photos", Toast.LENGTH_SHORT).show();
}
}
else if(requestCode==GALLERY_REQUEST)
{
Uri uri=data.getData();
galleryPhoto.setPhotoUri(uri);
String photoPath=galleryPhoto.getPath();
SelectedPhoto=photoPath;
try {
Bitmap bitmap= ImageLoader.init().from(photoPath).requestSize(1024,1024).getBitmap();
ivImage.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Toast.makeText(MainActivity.this, "Something wrong while choosing photos", Toast.LENGTH_SHORT).show();
}
}
}
}
Copy and paste two function here.
这是我的Php脚本
<?PHP
if(isset($_POST['image']))
{
$now = DateTime::createFromFormat('U.u', microtime(true));
$id = $now->format('YmdHisu');
$upload_folder = âupload";
$path = "$upload_folder/$id.jpeg";
$image = $_POST['image'];
if(file_put_contents($path, base64_decode($image)) !=false)
{
echo âuploaded_success";
exit;}
else
{
echo âuploaded_failed";
}
}
else {
echo "image not in";
exit;
}
?>