2014年02月

how to convert an image to byte array in android

I want to send an image as byte[] with my webservice. While sending request I am getting an error java.io.BufferedInputStream.streamClosed(BufferedInputStream.java:125) but the Image is Uploaded successfully here is my function to convert InputStream to byte[],



     public static byte[] streamToBytes(InputStream is) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len=0;
try {
while ((len = is.read(buffer)) >= 0) {
os.write(buffer, 0, len);
}
os.flush();
os.close();
is.close();
} catch (java.io.IOException e) {
}
return os.toByteArray();
}


Answers

You can use this -



ByteArrayOutputStream baos=new ByteArrayOutputStream(1000);
BufferedImage img=ImageIO.read(file);
ImageIO.write(img, "jpg", baos);
baos.flush();

byte[] byteArray = baos.toByteArray();




Text before php header not giving errors

I'm new to development and I'm trying to learn php.



Why is it that when I use the code below, the redirection, (through the header syntax), is still taking place even though content has been output before it ?



<html>
<body>
<p>My Page</p>
</body>
</html>

<?php

$redirect_page = 'http://localhost';
$redirect = true;
echo "Some Text";
if($redirect==true){
header('Location: '.$redirect_page);
}
?>


Many thanks in advance



Answers

HTTP requires response headers to arrive strictly before the body content, like so:



HTTP/1.1 200 OK
Powered-By: PHP/7.0.0-20150527
Content-Type: text/html; charset=utf-8

<html><body>Hello world!</body></html>


For PHP to comply with that requirement, PHP must send all accumulated headers at the instant it's ready to send any body content. The answer to your question hinges on the definition of when PHP is "ready to send".



To figure that out, look at your output_buffering setting:



var_dump(ini_get('output_buffering'));


On my machine with default settings, that returns 4096, which according to the manual means that PHP will be "ready to send" when 4096 body bytes have been accumulated.



To get the page to behave as you expect (ie, not sending the headers), you need to output enough bytes to exceed the setting of output_buffering. You can either do that by reducing the output buffering setting or by emitting more body text, eg:



echo str_repeat('<div/>', 4096);



See also this SO primer.





Get value of an input c# mvc

I have input boxes which I need to get the value from so that later they can be redirected to a specific controller, but I don't know how to access the values. Can someone please give me direction on how to get the values?



 <form action="@Url.Action("AddSpecific", "Comment", new { AuthorT ="1", WebSiteT = "1", postIdT = 1, TextT = "1",TitleT = "1" })" method="post">

<h1>
<label for="Title">Title</label>
<input name="Title" id="Title" type="text" required />
</h1>
<h1>
<label for="Text">Text</label>
<input name="Text" id="Text" type="text" required />
</h1>
<h1>
<label for="AuthorSite">AuthorSite</label>
<input name="AuthorSite" id="AuthorSite" required />
</h1>
<h1>
<label for="Author">Author</label>
<input name="Author" id="Author" type="text" required />
</h1>
<h1>a
<label for="IdOfPost">IdOfPost</label>
<input name="IdOfPost" id="IdOfPost" type="number" required />
</h1>

<input type="submit" value="Post comment" />




Answers

I would go with using a model to pass through and utilize as a parameter for your action.



You need to change your form html so you are not sending default values. Change:



<form action="@Url.Action("AddSpecific", "Comment", new { AuthorT ="1", WebSiteT = "1", postIdT = 1, TextT = "1",TitleT = "1" })" method="post">


To:



<form action="@Url.Action("AddSpecific", "Comment")" method="post">


Your MySpecificObject (the param object for your action). Note that the property names match the name attribute of your inputs, this is important:



public class MySpecificObject
{
public string AuthorSite {get;set;}
public string Title {get;set;}
public string Text {get;set;}
public string Author {get;set;{
public string IdOfPost {get;set;}
}


The Update Your CommentController:



public ActionResult AddSpecific(MySpecificObject mySpecificObject)
{
//mySpecificObject.IdOfPost
//do work
}


Additionally, if you decide you would like to do this via JavaScript and Ajax, check this StackOverflow post: Post form data to Controller"s action with Ajax



Answers

When you build your Form with the declared attributes, your in essence indicating where it should attempt to Post. Once you've outlined the said attribute the Submit Button will execute to that location. Model View Controller will perform some of your request automatically, so the Controller your pointing to:



public ActionResult Submit(int id, string name, string email)
{
// Utilize parameters.
}


So if you outline your form fields with a name that matches a parameter in the Controller, MVC will attempt to correlate said field directly to the Controller. So your current approach may not even need to occur.



Answers

Your input elements should have the same name value as the parameter on the controller



For Instance, if your controller looks like this:



public ActionResult AddSpecific(String AuthorT)
{
//Do Some Actions
return View();
}


You should have a form that has an element with the name "AuthorT":



<form action="@Url.Action("AddSpecific")" method="post">

<h1>
<label for="Title">Title</label>
</h1>
<input name="AuthorT" id="AuthorT" type="text" required />
<input type="submit" value="Post Comment"/>
</form>


See the name value of the input element has the same name as the controller parameter, the controller will pick up this value





Push ViewController with coder aDecoder: NSCoder

I'm trying to push a view controller using:



var vc2 = ViewController2()



self.navigationController?.pushViewController(vc2, animated: false)



but in my second view controller, I'm have:



required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}


But I get the error Missing argument for parameter 'coder' in call in the first ViewController. What goes in the parenthesis in the first view controller?



Answers

There are two ways you can fix this issue:



The easy way, just call the function passing nil to the parameters:



var vc2 = ViewController2(nibName: nil, bundle: nil)


The best way, create convenience initializers in your class:



class ViewController2: UIViewController {
required init(coder aDecoder: NSCoder) {
fatalError("This class does not support NSCoding")
}
override init (frame : CGRect) {
super.init(frame : frame)
}
convenience override init () {
self.init(frame:CGRectZero)
}
}


and now you can call:



var vc2 = ViewController2()


Answers

class ViewController2: UIViewController {
convenience init () {
self.init(nibName: nil, bundle: nil)
}
}


Now you can call ViewController2()





Nordic nrf51822 and S130 never gets to my application code

I am trying to run a NRF51822 chip using Emblocks and OpenOCD, the debugger interface is a ST-Link from a Discovery board, which supports SWD.



When I program the Blank device everything works fine, the program flow reaches the main function. However, when I flash S130, the program flow never reaches my main function (I don't have any other application code).



I have checked the assembly code and S130 is stuck on a (arm asm incoming) WFE and b.n instruction, to my knowledge, it seems like it is waiting for an interrupt, event or for a wake up to happen before doing anything... is this the expected behaviour or I am doing something wrong?



The only pins I have connected are the SWD lines (2 pins) GND and VDD (3 volts).





↑このページのトップヘ