我想將imageview android圖像發送到我的服務器url並將其存儲到我的服務器數據庫(phpmyadmin)中。 當I M試圖插入簡單的字符串,然後它工作,但是當它是圖像視圖,然後它not..please幫助..這是我的代碼使用json數據發送imageview base64編碼字符串
public class AddnewActivity extends Activity {
ImageView iv;
public static final int PHOTO_PICKER_ID = 0;
EditText name;
EditText comments;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addnew);
// Show the Up button in the action bar.
setupActionBar();
iv=(ImageView) findViewById(R.id.imageView1);
ImageButton imgb= (ImageButton) findViewById(R.id.imageButton1);
imgb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Complete action using"), PHOTO_PICKER_ID);
}
catch(Exception e){}
}
});
Button save=(Button) findViewById(R.id.button1);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] imageByteArray=stream.toByteArray();
String img_str = Base64.encodeToString(imageByteArray, 0);
name=(EditText) findViewById(R.id.editText1);
String nm=name.getText().toString();
comments=(EditText) findViewById(R.id.editText2);
String com=comments.getText().toString();
if("".equals(nm) || "".equals(com)){
Toast.makeText(getApplicationContext(), "Empty field detected", Toast.LENGTH_SHORT).show();
}
else{
try {
JSONObject json = new JSONObject();
json.put("name", nm);
json.put("comments", com);
json.put("img", img_str);
HttpClient client = new DefaultHttpClient();
String url = "http://myservername/parser/json_req.php";
HttpPost request = new HttpPost(url);
request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
request.setHeader("json", json.toString());
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
Toast.makeText(getApplicationContext(), "Inserted", Toast.LENGTH_SHORT).show();
}
name.setText(null);
comments.setText(null);
} catch (Throwable t) {
Toast.makeText(getApplicationContext(), "Request failed: " + t.toString(), Toast.LENGTH_SHORT).show();
}
}
}catch(Exception e){}
}
});
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri currImageURI = null;
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
currImageURI = data.getData();
}
if (currImageURI == null) {
String filePath = Environment.getExternalStorageDirectory()
+ "";
filePath = filePath + File.separator + "temp_img.jpg";
File f = new File(filePath);
currImageURI = Uri.fromFile(f);
}
ContentResolver cr = getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr,currImageURI);
iv.setImageBitmap(bitmap);
} catch (Exception e) {
Log.e("Camera", e.toString());
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.addnew, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
你可以發佈你所得到的錯誤。 – MAkS