first of all reach to the gallery of the phone using the intent.
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;
}
}
}
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);
}
Using this bytes you'll get the image uploaded to the web service and store the image on to the server.
Search This Blog
Showing posts with label GalleryImage. Show all posts
Showing posts with label GalleryImage. Show all posts
Tuesday, February 28, 2012
Uploading Image to the server using webservice
Labels:
Activity,
Bitmap,
cursor,
FileInputStream,
GalleryImage,
ImageBytes,
Intent,
Uri
Friday, February 10, 2012
Selecting The Image From The Gallery And Set The Path Into The EditText
First of all we have to reach to the android default gallery and select the file from that and set that path to the edit text.
Make an .xml file named main.xml file and copy this into it.
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class BrowseImagefromGallery extends Activity {
private static final int GET_PICTURE = 1;
private String selectedImagePath;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnBrowse = (Button)findViewById(R.id.btnBrowse);
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"), GET_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == GET_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
EditText imageBrowse = (EditText)findViewById(R.id.absolutepathofimage);
imageBrowse.setText(selectedImagePath);
}
}
}
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);
}
}
Make an .xml file named main.xml file and copy this into it.
<?xml version="1.0" encoding="utf-8"
?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText android:id="@+id/absolutepathofimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:text="Browse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnBrowse"
/>
</LinearLayout>
Now make the java file named BrowseImagefromGallery and copy this code into it
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class BrowseImagefromGallery extends Activity {
private static final int GET_PICTURE = 1;
private String selectedImagePath;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnBrowse = (Button)findViewById(R.id.btnBrowse);
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"), GET_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == GET_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
EditText imageBrowse = (EditText)findViewById(R.id.absolutepathofimage);
imageBrowse.setText(selectedImagePath);
}
}
}
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);
}
}
Subscribe to:
Posts (Atom)