I have code like this:
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT,null);
galleryIntent.setType("image/*");
galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INTENT, galleryIntent);
chooser.putExtra(Intent.EXTRA_TITLE, "Continue with...");
Intent[] intentArray = {cameraIntent};
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
chooser.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,mImageCaptureUri);
startActivityForResult(chooser,UPLOAD_IMAGE);
This allows me to choose with what app I want to upload a picture, then
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == UPLOAD_IMAGE && resultCode == Activity.RESULT_OK){
if(data.getData()!=null){
try {
Uri u= data.getData();
InputStream stream = getContentResolver().openInputStream(u);
Bitmap tmp = BitmapFactory.decodeStream(stream);
ByteArrayOutputStream out = new ByteArrayOutputStream();
tmp.compress(Bitmap.CompressFormat.JPEG, 60, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
stream.close();
//Toast.makeText(this,"Uploaded "+(decoded.getByteCount())/1024/1024+"kb", Toast.LENGTH_LONG).show();
bitmap = decoded;
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = this.getContentResolver().query(u, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
imageName = cursor.getString(column_index);
Toast.makeText(this, "Name: "+imageName, Toast.LENGTH_LONG).show();
} finally {
if (cursor != null) {
cursor.close();
}
}
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
}
else
{
Bitmap tmp = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream out = new ByteArrayOutputStream();
tmp.compress(Bitmap.CompressFormat.JPEG, 60, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
String imageName = mImageCaptureUri.getPath();
Toast.makeText(this, "Name: "+imageName, Toast.LENGTH_LONG).show();
bitmap = decoded;
}
super.onActivityResult(requestCode, resultCode, data);
}
updateUploadedPictures();
}
And finally, I'm trying to upload it to the server by this code:
public void uploadPhoto(Bitmap bitmap,String fileName, String country, String city) throws Exception {
try {
// HttpClient httpClient = new DefaultHttpClient();
// HttpContext localContext = new BasicHttpContext();
// here, change it to your php;
HttpPost httpPost = new HttpPost("http://www.one_f*****g_website.com");
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
bitmap = BitmapFactory.decodeFile(fileName);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 80, bos);
byte[] data = bos.toByteArray();
// sending a String param;
entity.addPart("user", new StringBody(USER.id));
entity.addPart("country", new StringBody(country));
entity.addPart("city", new StringBody(city));
// sending a Image;
// note here, that you can send more than one image, just add another param, same rule to the String;
entity.addPart("image", new ByteArrayBody(data, USER.id));
httpPost.setEntity(entity);
//HttpResponse response = httpClient.execute(httpPost, localContext);
//BufferedReader reader = new BufferedReader(new InputStreamReader( response.getEntity().getContent(), "UTF-8"));
//String sResponse = reader.readLine();
} catch (Exception e) {
Log.v("myApp", "Some error came up");
}
}
My problem is, that the App crashes, but it isn't the worst... Sometimes, when I want to choose picture from gallery it says: No photos or videos available... But when I open file browser, everythings is there, and after that it shows up also in galery (SOMETIMES, sometimes the app just crashed).
Because of I can't have it in debbug mode, I can't see any LOG etc...
And even if I pass through uploading images into App, It crashes everytime I try to upload it on web
Can anyone give me a help?