问题
I am new to android trying to work on uploading image in to server. Got some sample code from the Internet. But it is showing some error in the line which am not able to handle it. Can any one help me in this case. And the link which is fetch the code is http://blog.sptechnolab.com/2011/03/09/android/android-upload-image-to-server/
and the error is getting is
"The method encodeBytes(byte[]) is undefined for the type Base64"
and the corresponding

i have even downloaded base64.java file in the project
回答1:
There is no encodeBytes
in the API. Use encodeToString.
回答2:
You can use these methods instead
public static String encodeToString (byte[] input, int offset, int len, int flags)
Since: API Level 8
Base64-encode the given data and return a newly allocated String with the result.
Parameters
input : the data to encode
offset : the position within the input array at which to start
len : The number of bytes of input to encode
flags : controls certain features of the encoded output.
Passing DEFAULT results in output that adheres to RFC 2045.
public static String encodeToString (byte[] input, int flags)
Since: API Level 8
Base64-encode the given data and return a newly allocated String with the result.
Parameters
input : the data to encode
flags :controls certain features of the encoded output.
Passing DEFAULT results in output that adheres to RFC 2045.
回答3:
Whaaaa!
You MUST indent
your code!
For every {
you open you should space to the right so you'll see better what your code is doing:
This is good:
try {
something();
} catch (Exception e) {
weMessedUp();
if (e == i)
{
lol();
}
}
This is bad:
try {
something();
} catch (Exception e) {
weMessedUp();
if (e == i)
{
lol();
}
}
It's only for reading, your programs will be faster to understand if in one week you want to modify something.
In eclipse to indent, do ctrl + a
to select your whole code, then ctrl + i
to indent.
This doesn't answer you question but will help others to answer and you to improve your skills.
回答4:
You can simply open the file as a bytestream and send it as a stream to your httpconnection?
Opening a file as a stream like so:
File inFile = new File(fileName);
BufferedReader br = new BufferedReader(
new InputStreamReader(
new FileInputStream(inFile)
)
);
URL url = new URL("http://www.google.com");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
while ((decodedString = br.readLine()) != null) {
out.write(decodedString);
}
out.close();
This is the implementation for reading a file line by line. Not sure if it will work given the encoding of an image without line breaks, but you should be able to reengineer to stream byte-by-byte without much trouble.
回答5:
public class UploadImage extends Activity {
InputStream inputStream;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon); ByteArrayOutputStream <span id="IL_AD5" class="IL_AD">stream</span> = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",image_str));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/Upload_image_ANDROID/upload_image.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String the_string_response = convertResponseToString(response);
Toast.makeText(UploadImage.this, "Response " + the_string_response, Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(UploadImage.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();
System.out.println("Error in http connection "+e.toString());
}
}
public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException{
String res = "";
StringBuffer buffer = new StringBuffer();
inputStream = response.getEntity().getContent();
int contentLength = (int) response.getEntity().getContentLength(); //getting content length…..
Toast.makeText(UploadImage.this, "contentLength : " + contentLength, Toast.LENGTH_LONG).show();
if (contentLength < 0){
}
else{
byte[] data = new byte[512];
int len = 0;
try
{
while (-1 != (len = inputStream.read(data)) )
{
buffer.append(new String(data, 0, len)); //converting to string and appending to stringbuffer…..
}
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
inputStream.close(); // closing the stream…..
}
catch (IOException e)
{
e.printStackTrace();
}
res = buffer.toString(); // converting stringbuffer to string…..
Toast.makeText(UploadImage.this, "Result : " + res, Toast.LENGTH_LONG).show();
//System.out.println("Response => " + EntityUtils.toString(response.getEntity()));
}
return res;
来源:https://stackoverflow.com/questions/7412778/upload-image-from-android-phone-to-the-server