【发布时间】:2012-02-29 21:19:46
【问题描述】:
我完全迷路了,我正在尝试通过 android 应用程序将照片发送到 php 网页。
理论上一切都应该是正确的,但目标数据已损坏或我不知道......
我可以获取帖子数据,我尝试使用一个简单的字符串它工作正常,但对于一个沉重的文件,数据似乎已损坏。
public class EncodingAndSending extends Thread{
ShareOnMyWebSiteActivity mycontext;
ContentResolver cr;
Uri uri;
public Handler mainHandler,sHandler;
public EncodingAndSending(ShareOnMyWebSiteActivity myctxt,Handler mainone,Uri URI,ContentResolver crr){
mycontext=myctxt;
cr=crr;
uri=URI;
mainHandler=mainone;
this.start();
}
return buffer.toString().toUpperCase();
}
public void run(){
InputStream is=null;
byte[] data=null;
try {
is = cr.openInputStream(uri);
// Get binary bytes for encode
data = getFileBytes(is);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String data_string=Base64.encodeToString(data,Base64.URL_SAFE);
if(data_string!=""){
SendRequest(data_string);
}
else{
}
}
public byte[] getFileBytes(InputStream ios) throws IOException {
ByteArrayOutputStream ous = null;
//InputStream ios = null;
try {
byte[] buffer = new byte[4096];
ous = new ByteArrayOutputStream();
//ios = new FileInputStream(file);
int read = 0;
while ((read = ios.read(buffer)) != -1)
ous.write(buffer, 0, read);
} finally {
try {
if (ous != null)
ous.close();
} catch (IOException e) {
// swallow, since not that important
}
try {
if (ios != null)
ios.close();
} catch (IOException e) {
// swallow, since not that important
}
}
return ous.toByteArray();
}
private void SendRequest(String data_string){
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("xxxxx.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("image", data_string));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
ResponseHandler<String> responseHandler=new BasicResponseHandler();
String responseBody = httpclient.execute(httppost, responseHandler);
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
}
编辑:
这行得通。我可以对图像进行编码、解码和预览。我没有使用getBytes()函数不知道问题是不是从那里来的。
我会告诉你的。
super.onCreate(savedInstanceState);
setContentView(R.layout.test_image);
ImageView image = (ImageView) findViewById(R.id.imageView1);
FileInputStream in;
BufferedInputStream buf;
Intent intent = getIntent();
Bundle extras = intent.getExtras();
Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
ContentResolver cr = getContentResolver();
Bitmap bMap=null;
try {
InputStream is = cr.openInputStream(uri);
bMap = BitmapFactory.decodeStream(is);
if (is != null) {
is.close();
}
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bMap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String data_string=Base64.encodeToString(b,Base64.DEFAULT);
b=null;bMap=null;
b=Base64.decode(data_string, Base64.DEFAULT);
bMap=BitmapFactory.decodeByteArray(b, 0, b.length);
image.setImageBitmap(bMap);
【问题讨论】:
-
你最好把你没有在你的例子中使用的代码去掉,比如
byteArrayToHexString,它只会让读者感到困惑。 -
对,我不知道是否相关。
-
能否将方法
getFileBytes的代码包含在内?也许这就是罪魁祸首? -
当然可以,就是这样。
-
抱歉找不到任何明显的错误...您可以通过打印
data_string和Base64解码并检查结果进行调试。通过这种方式,您可以查看问题出在阅读/编码还是 http 帖子中。
标签: android image post base64 http-post