Reputation: 36404
This is my code to take a photo from gallery or camera. I've implemented a background thread to get the task done and then using SmartImageView to set the image using the url. My error is Out of Memory Allocation.:
upload.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity.this);
builder.setMessage("Select") .setCancelable(false).setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent gallIntent=new Intent(Intent.ACTION_GET_CONTENT);
gallIntent.setType("image/*");
startActivityForResult(gallIntent, 10);
}
})
.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 0);
}
});
AlertDialog alert = builder.create();
alert.show();
if (bitmap == null) {
Toast.makeText(getApplicationContext(),
"Please select image", Toast.LENGTH_SHORT).show();
} else {
dialog = ProgressDialog.show(activity.this, "Uploading",
"Please wait...", true);
//new ImageUploadTask().execute();
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, final Intent data){
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 10:
if (resultCode == Activity.RESULT_OK) {
Thread t = new Thread()
{
public void run(){
Uri imageUri = data.getData();
Bitmap b;
try {
b = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
String timestamp = Long.toString(System.currentTimeMillis());
MediaStore.Images.Media.insertImage(getContentResolver(), b, timestamp, timestamp);
HttpResponse httpResponse;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] ba = bao.toByteArray();
int f = 0;
String ba1=Base64.encodeToString(ba, f);
try {
OAuth oAuth = new OAuth(activity.this);
HttpPost httpPost = new HttpPost("url");
httpPost.setEntity(new ByteArrayEntity(ba));
HttpClient httpClient= new DefaultHttpClient();
httpResponse = httpClient.execute(httpPost);
int responseCode = httpResponse.getStatusLine().getStatusCode();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}};
t.start();
imgView.setImageUrl(obj.ImageUrl);
}
Upvotes: 2
Views: 1100
Reputation: 39406
this
String ba1=Base64.encodeToString(ba, f);
is very heavy. I recommend using a http://developer.android.com/reference/android/util/Base64OutputStream.html instead, write to a file, then use an InputStream in the HttpEntity.
Upvotes: 1