从android手机上传图像到服务器
我是新来的Android试图上传图像到服务器上。 从互联网上得到了一些示例代码。但它显示出一些错误,无法处理它。在这种情况下,任何人都可以帮助我。并且其取代码的链路是http://blog.sptechnolab.com/2011/03/09/android/android-upload-image-to-server/从android手机上传图像到服务器
和误差越来越是
“的方法encodeBytes(字节[])是未定义的类型的Base64”
和相应
我甚至下载base64.java文件中的项目
Whaaaa!
您必须indent
您的代码!
每{
你打开你的空间应该在右边,所以你会看到更好的你的代码是这样做的:
这是一件好事:
try {
something();
} catch (Exception e) {
weMessedUp();
if (e == i)
{
lol();
}
}
这是不好:
try {
something();
} catch (Exception e) {
weMessedUp();
if (e == i)
{
lol();
}
}
它只适用于阅读,如果你想在一个星期内修改某些内容,你的程序将更快地理解。
在eclipse中缩进,做ctrl + a
来选择你的整个代码,然后ctrl + i
来缩进。
这并不回答你的问题,但会帮助他人回答,并提高你的技能。
您可以简单地将文件作为字节流打开并将其作为流发送到您的httpconnection?
打开文件作为流,像这样:
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();
这是逐行读取文件行的执行情况。不确定在没有换行符的情况下对图像进行编码是否会起作用,但是您应该可以重新设计以逐字节地进行流式传输,而不会有太多麻烦。
您可以使用这些方法,而不是
public static String encodeToString (byte[] input, int offset, int len, int flags)
自:API等级8
的Base64编码给定的数据,并用结果返回一个新分配的字符串。
参数
输入:数据编码
偏移:输入阵列内的位置处启动
LEN:输入的字节数来编码
标志:控制编码输出的某些功能。
传递默认输出结果符合RFC 2045
public static String encodeToString (byte[] input, int flags)
自:API等级8
的Base64编码给定的数据,并用结果返回一个新分配的字符串。
参数
输入:数据编码
国旗:控制所述编码输出的某些特征。
传递DEFAULT结果符合RFC 2045
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;
感谢张贴答案输出!虽然代码片段可以回答这个问题,但添加一些附加信息仍然很棒,比如解释等。 – j0k
感谢您的帮助 – radha