import android.app.Activity;
main.xml :
Manifast:
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class Home extends Activity
{
Button btnGal;
ImageView ivGalImg;
Bitmap bmp;
final int RQS_GALARRY = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnGal = (Button)findViewById(R.id.btnGallary);
ivGalImg = (ImageView)findViewById(R.id.ivImage);
btnGal.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
openGallery();
}
});
}
private void openGallery()
{
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent,RQS_GALARRY );
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
switch(requestCode)
{
case RQS_GALARRY:
try
{
System.gc();
imageUri = data.getData();
String selectedImagePath = getPath(imageUri);
if(bmp != null && !bmp.isRecycled())
{
bmp = null;
((BitmapDrawable)YD_image.getDrawable()).getBitmap().recycle();
}
bmp = BitmapFactory.decodeFile(selectedImagePath);
ivGalImg .setBackgroundResource(0);
ivGalImg .setImageBitmap(bmp);
}
catch (Exception e)
{
e.printStackTrace();
}
break;
}
}
}
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);
}
}
main.xml :
<?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" >
<ImageView
android:id="@+id/ivImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/icon" >
</ImageView>
<Button
android:id="@+id/btnGallary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click To Open Gallary" >
</Button>
</LinearLayout>
Manifast:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pickimagefromgal.activities"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name=".Home"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment