Subir imagen de la galería incorporada

As I don't have the SD card. I want to upload image to a server from selecting image from built -in Gallery.

Can anybody help me with this on how to place image in in-built gallery so that I can select from there.

Any help will be appricated.

preguntado el 10 de marzo de 12 a las 14:03

1 Respuestas

You can go to your inbuilt gallery from the following code:

Button btnBrowse = (Button)findViewById(R.id.btn_browse);
    btnBrowse.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,
              "Select Picture"), SELECT_PICTURE);

}
});

Now onActivityResult should be like this.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (resultCode == RESULT_OK) {
     if (requestCode == SELECT_PICTURE) {
         Uri selectedImageUri = data.getData();
         selectedImagePath = getPath(selectedImageUri);
         EditText imageBrowse = (EditText)findViewById(R.id.thumb_url);
         imageBrowse.setText(selectedImagePath);
         byte[] strBytes = convertToBytes(selectedImagePath);

         imageBytes = strBytes;
     }
 }
}

ConvertToBytes method should be like this:

public byte[] convertToBytes(String selectedImagePath)
{
 try
 {
  FileInputStream fs = new FileInputStream(selectedImagePath);

  Bitmap bitmap = BitmapFactory.decodeStream(fs);

  ByteArrayOutputStream bOutput = new ByteArrayOutputStream();

  bitmap.compress(CompressFormat.JPEG,1, bOutput);

  byte[] dataImage = bOutput.toByteArray();

  return dataImage;
 }
 catch(NullPointerException ex)
 {
  ex.printStackTrace();
  return null;
 } 
 catch (FileNotFoundException e)
 {    
    e.printStackTrace();
    return null;
 }

}


public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
         .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

By this you can upload the image.

respondido 10 mar '12, 14:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.