我正在尝试将捕获/选择的图像从图库上传到我的服务器,但出现以下错误
I am trying to upload the captured/ selected image from gallery to my server but it is giving below error
class ImageGalleryTask extends AsyncTask<Void, Void, String> {
@SuppressWarnings("unused")
@Override
protected String doInBackground(Void... unsued) {
InputStream is;
BitmapFactory.Options bfo;
Bitmap bitmapOrg;
ByteArrayOutputStream bao ;
// ImageView imgshoot = null;
bfo = new BitmapFactory.Options();
bfo.inSampleSize = 2;
// bitmapOrg = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/" + customImage, bfo);
// bitmap = mark(bitmap, "Hallo");
bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, bao);
byte [] ba = bao.toByteArray();
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(bao.toByteArray()));
String ba1 = Base64.encodeBytes(ba);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));
nameValuePairs.add(new BasicNameValuePair("cmd","image_android"));
Log.v("log_tag", System.currentTimeMillis()+".png");
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
// Here you need to put your server file address
HttpPost("http://192.168.0.1/Upload/UploadImg.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.v("log_tag", "In the try Loop" );
}catch(Exception e){
Log.v("log_tag", "Error in http connection "+e.toString());
}
return "Success";
// (null);
}
@Override
protected void onProgressUpdate(Void... unsued) {
}
@Override
protected void onPostExecute(String sResponse) {
try {
if (dialog.isShowing())
dialog.dismiss();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
e.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
imgView.setImageBitmap(bitmap);
}
这是 Logcat 中显示的错误:
02-16 14:44:05.872: E/AndroidRuntime(9375):
java.lang.RuntimeException: An error occurred while executing
doInBackground() 02-16 14:44:05.872: E/AndroidRuntime(9375): at
android.os.AsyncTask.done(AsyncTask.java:309)
02-16 14:44:05.872: E/AndroidRuntime(9375): at
java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
02-16 14:44:05.872: E/AndroidRuntime(9375): at
java.util.concurrent.FutureTask.setException(FutureTask.java:223)
02-16 14:44:05.872: E/AndroidRuntime(9375): at
java.util.concurrent.FutureTask.run(FutureTask.java:242) 02-16
14:44:05.872: E/AndroidRuntime(9375): at
android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:234) 02-16
14:44:05.872: E/AndroidRuntime(9375): at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
02-16 14:44:05.872: E/AndroidRuntime(9375): at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
02-16 14:44:05.872: E/AndroidRuntime(9375): at
java.lang.Thread.run(Thread.java:818) 02-16 14:44:05.872:
E/AndroidRuntime(9375): Caused by:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the
original thread that created a view hierarchy can touch its views.
02-16 14:44:05.872: E/AndroidRuntime(9375): at
android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6603) 02-16
14:44:05.872: E/AndroidRuntime(9375): at
android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:954)
我什至添加了存储在 php 文件中的服务器代码:(check.php):
<?php
$target_dir = "Upload/";
$base=$_REQUEST['image'];
$name=$_REQUEST['cmd'];
$binary=base64_decode($base);
header('Content-Type: jpeg; charset=utf-8');
$file = fopen($target_path, 'wb');
$file = fopen($name, 'wb');
fwrite($file, $binary);
fclose($file);
//move_uploaded_file($file,$target_path.$name);
copy($name,$target_path.$name);
?>
在doInBackground()中,你写了imgView.setImageBitmap(bitmap)
这不是acceptable.You应该在那个方法中写后台操作。
有很多 tutorials.Hope 会有帮助的。
试试这个 example
替换imgView.setImageBitmap(位图);与
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
imgView.setImageBitmap(bitmap);
}
});
class ImageGalleryTask extends AsyncTask<Void, Void, String> {
@SuppressWarnings("unused")
@Override
protected String doInBackground(Void... unsued) {
InputStream is;
BitmapFactory.Options bfo;
Bitmap bitmapOrg;
ByteArrayOutputStream bao ;
// ImageView imgshoot = null;
bfo = new BitmapFactory.Options();
bfo.inSampleSize = 2;
// bitmapOrg = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/" + customImage, bfo);
// bitmap = mark(bitmap, "Hallo");
bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, bao);
byte [] ba = bao.toByteArray();
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(bao.toByteArray()));
String ba1 = Base64.encodeBytes(ba);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));
nameValuePairs.add(new BasicNameValuePair("cmd","image_android"));
Log.v("log_tag", System.currentTimeMillis()+".png");
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
// Here you need to put your server file address
HttpPost("http://192.168.0.1/Upload/UploadImg.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.v("log_tag", "In the try Loop" );
}catch(Exception e){
Log.v("log_tag", "Error in http connection "+e.toString());
}
return "Success";
// (null);
}
@Override
protected void onProgressUpdate(Void... unsued) {
}
@Override
protected void onPostExecute(String sResponse) {
try {
if (dialog.isShowing())
dialog.dismiss();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
e.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
imgView.setImageBitmap(bitmap);
}
这是 Logcat 中显示的错误:
02-16 14:44:05.872: E/AndroidRuntime(9375): java.lang.RuntimeException: An error occurred while executing doInBackground() 02-16 14:44:05.872: E/AndroidRuntime(9375): at
android.os.AsyncTask.done(AsyncTask.java:309)02-16 14:44:05.872: E/AndroidRuntime(9375): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) 02-16 14:44:05.872: E/AndroidRuntime(9375): at java.util.concurrent.FutureTask.setException(FutureTask.java:223) 02-16 14:44:05.872: E/AndroidRuntime(9375): at java.util.concurrent.FutureTask.run(FutureTask.java:242) 02-16 14:44:05.872: E/AndroidRuntime(9375): at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:234) 02-16 14:44:05.872: E/AndroidRuntime(9375): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 02-16 14:44:05.872: E/AndroidRuntime(9375): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 02-16 14:44:05.872: E/AndroidRuntime(9375): at java.lang.Thread.run(Thread.java:818) 02-16 14:44:05.872: E/AndroidRuntime(9375): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 02-16 14:44:05.872: E/AndroidRuntime(9375): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6603) 02-16 14:44:05.872: E/AndroidRuntime(9375): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:954)
我什至添加了存储在 php 文件中的服务器代码:(check.php):
<?php
$target_dir = "Upload/";
$base=$_REQUEST['image'];
$name=$_REQUEST['cmd'];
$binary=base64_decode($base);
header('Content-Type: jpeg; charset=utf-8');
$file = fopen($target_path, 'wb');
$file = fopen($name, 'wb');
fwrite($file, $binary);
fclose($file);
//move_uploaded_file($file,$target_path.$name);
copy($name,$target_path.$name);
?>
在doInBackground()中,你写了imgView.setImageBitmap(bitmap)
这不是acceptable.You应该在那个方法中写后台操作。
有很多 tutorials.Hope 会有帮助的。 试试这个 example
替换imgView.setImageBitmap(位图);与
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
imgView.setImageBitmap(bitmap);
}
});