Save Image view - android

Can someone help me understand how to use OnPause() and OnResume() in this code? I'm trying to save last Selected or Captured image in imageView so when the user closes the program and comes back again he doesn't need to set or take the image again.
package com.example.thang.sdcardimagesactivity;
public class MainActivity extends Activity {
private static final int REQUEST_CODE = 1;
private Bitmap bitmap;
Button button;
ImageView imageView;
String selectedImagePath;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button) findViewById(R.id.click);
imageView=(ImageView) findViewById(R.id.image);
imageView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Switch();
return true;
}
});
}
public void Switch(){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==REQUEST_CODE&&resultCode== Activity.RESULT_OK){
try{
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
Log.v("roni", filePath);
cursor.close();
if(bitmap != null && !bitmap.isRecycled())
{
bitmap = null;
}
bitmap = BitmapFactory.decodeFile(filePath);
//imageView.setBackgroundResource(0);
imageView.setImageBitmap(bitmap);
}catch (Exception e){
e.printStackTrace();
}
}
}
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);};
#Override
protected void onPause() {
SharedPreferences sp = getSharedPreferences("AppSharedPref", 1); // Open SharedPreferences with name AppSharedPref
SharedPreferences.Editor editor = sp.edit();
editor.putString("ImagePath", selectedImagePath); // Store selectedImagePath with key "ImagePath". This key will be then used to retrieve data.
editor.commit();
super.onPause();
}
#Override
protected void onResume() {
SharedPreferences sp = getSharedPreferences("AppSharedPref", 1);
selectedImagePath = sp.getString("ImagePath", "");
Bitmap myBitmap = BitmapFactory.decodeFile(selectedImagePath);
imageView.setImageBitmap(myBitmap);
super.onResume();
}
}
View Activity
public class ViewActivity extends Activity {
ImageButton imageViews;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
imageViews = (ImageButton) findViewById(R.id.image);
// textView=(TextView) findViewById(R.id.textview);
Intent intent = getIntent();
Uri data = intent.getData();
if (intent.getType().indexOf("image/") != -1)
{
imageViews.setImageURI(data);
}
setResult(RESULT_OK, intent);
Bundle bundle=new Bundle();
bundle.putInt("image",R.id.image);
intent.putExtras(bundle);
finish();
}
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);
}
}

You can save an image to the SD card so it can be permanently accessed. It's best you do this immediately in the onActivityResult() method.
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "ImagesFolder");
File file = new File(imagesFolder, "image.jpg");
String fileName = file.getAbsolutePath();
try {
FileOutputStream out = new FileOutputStream(file);
squareBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
Log.e("Image", "Convert");
}
And to load it back when you restart the application (in onCreate or preferably in onResume):
try {
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "ImagesFolder");
if (!imagesFolder.exists())
imagesFolder.mkdirs();
if (new File(imagesFolder, "image.jpg").exists()) {
String fileName = new File(imagesFolder, "image.jpg").getAbsolutePath();
Bitmap bitmap = BitmapFactory.decodeFile(fileName);
ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setImageBitmap(bitmap);
}
} catch (NullPointerException e) {
Log.e("Exception", "null");
}
Of course you can change the folder and image name.

Related

how to select a picture form local album in android

public static final int TAKE_PHOTO=1;
public static final int CROP_PHOTO=2;
private Button choosePhoto;
private ImageView picture;
private Uri imageUri;
choosePhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File outputImage = new File(Environment.getExternalStorageDirectory(),"output_image.jpg");
try {
if (outputImage.exists()){
outputImage.delete();
}
outputImage.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImage);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent,CROP_PHOTO);
}
});
}
protected void onActivityResult(int requestCode,int resultCode,Intent data){
switch (requestCode){
case CROP_PHOTO:
if (resultCode==RESULT_OK){
try{ //使用decodeStream()函数 解析成Bitmap对象,
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
picture.setImageBitmap(bitmap);
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
break;
default:break;
}
}
Taking photo is OK but choose photo is wrong, when I click the button it shows album successfully, but when selecting a picture, it return whitout choosing that picture.
Use below code in onActivityResult();
Uri filePath = intent1.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), filePath );
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
picture.setImageBitmap(bitmap);
Paste below code after getting data from intent in onactivityresult.
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
profilePic.setImageBitmap(BitmapFactory.decodeFile(picturePath));

How to set selected image from SD card on imageview in Android

I am a beginner of android development. I am trying to develop my own gallery. But I am facing a problem. When I go to SD card on my memory and choose a photo then I am trying to open image using my Image Viewer app. But when I select Image Viewer then another intent is called for choosing my image. But I directly want show the selected image on Imageview. Please anyone help me.
My code:
public class MainActivity extends Activity {
private static final int REQUEST_CODE = 1;
private Bitmap bitmap;
private ImageView imageView;
private String selectedImagePath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.result);
pickImage();
}
public void pickImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
InputStream stream = null;
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
try {
// recyle unused bitmaps
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
Log.v("roni", filePath);
cursor.close();
if(bitmap != null && !bitmap.isRecycled())
{
bitmap = null;
}
bitmap = BitmapFactory.decodeFile(filePath);
imageView.setBackgroundResource(0);
imageView.setImageBitmap(bitmap);
// imageView.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null)
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private 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);
}
}
/////After changing the code
public class MainActivity extends Activity {
private static final int REQUEST_CODE = 1;
private Bitmap bitmap;
private ImageView imageView;
private String selectedImagePath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.result);
Intent i = new Intent(
Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
InputStream stream = null;
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
try {
// recyle unused bitmaps
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
Log.v("roni", filePath);
cursor.close();
if(bitmap != null && !bitmap.isRecycled())
{
bitmap = null;
}
bitmap = BitmapFactory.decodeFile(filePath);
//imageView.setBackgroundResource(0);
imageView.setImageBitmap(bitmap);
// imageView.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null)
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private 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); } }
Let's assume you have an activity called ViewActivity that shows selected Image.
in your AndroidManifest.xml set your activity as a Image Viewer Activity by adding this :
<activity android:name=".ViewActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/*"/>
</intent-filter>
</activity>
in onCreate() method of ViewActivity catch passed Image by doing this :
Intent intent = getIntent();
Uri data = intent.getData();
//Check If data type is Image
if (intent.getType().indexOf("image/") != -1)
{
myImageView.setImageURI(data);
}
try this Code replace whole class plz if success then i will explain to you
public class ImageGalleryDemoActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}

How to convert an Image to Bits array in android

I am new to android development, Now currently am working on an App for Image Steganography. So in my app , I need to convert an Image that i selected from gallery to Bits array(Each pixel will have a 8 bit value, thats what i mean), How can i do it ? Can anybody help me ?
public class ImageActivity extends Activity {
private Button btnSelectImage;
private Button btnEncode;
String Pathfile=new String();
public String selectedImagePath;
private ImageView myImage;
Bitmap result;
public static final int ICON_SELECT_GALLERY = 1;
private static final Object IMAGE_TAKER_REQUEST = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
btnSelectImage = (Button) findViewById(R.id.button1);
btnEncode = (Button) findViewById(R.id.button2);
btnSelectImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
selectImage();
}
});
myImage = (ImageView) findViewById(R.id.imageView1);
}
public void selectImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, ICON_SELECT_GALLERY);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == 1)
{
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
Log.v("IMAGE PATH====>>>> ",selectedImagePath);
TextView imgPath=(TextView)findViewById(R.id.textView2);
imgPath.setText(selectedImagePath);
Pathfile=new String(selectedImagePath);
result = BitmapFactory.decodeFile(Pathfile);
myImage.setImageBitmap(result);
}
}
}
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);
}
}
Pass Bitmap and method will return byte[]
public static byte[] getBytesFromBitmap(Bitmap bitmap){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 70, stream);
return stream.toByteArray();
}

Application crashes when I choose a picture from gallery and integrate it into Gmail

I'm trying to create an application that lets the user choose an image from the gallery or take a new one, and then send it via email. The function that takes the picture works perfectly (I can take it and then email it). But when I try to choose a picture, my application crashes. Here is my Java file:
public class Request extends Activity {
Button send;
Bitmap thumbnail;
File pic;
protected static final int CAMERA_PIC_REQUEST = 0;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.requestscreen);
send=(Button) findViewById(R.id.sendBtn);
Button camera = (Button) findViewById(R.id.camBtn);
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
Button galbtn = (Button) findViewById(R.id.galBtn);
galbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
}
});
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0){
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"test#test.com"});
i.putExtra(Intent.EXTRA_SUBJECT,"On The Job");
//Log.d("URI#!##!#!###!", Uri.fromFile(pic).toString() + " " + pic.exists());
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));
i.setType("image/png");
startActivity(Intent.createChooser(i,"Share you on the jobing"));
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(thumbnail);
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
pic = new File(root, "pic.png");
FileOutputStream out = new FileOutputStream(pic);
thumbnail.compress(CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
} catch (IOException e) {
Log.e("BROKEN", "Could not write file " + e.getMessage());
}
}
if (requestCode == SELECT_PICTURE){
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
TextView uritv = null;
uritv.setText(selectedImagePath.toString());
}
}
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);
}
}
I don't know what I need to change in my code, can anyone help me?
Seems clear to me
TextView uritv = null;
uritv.setText(selectedImagePath.toString());
In other words, you're trying to use a method on a null object.
My guess is you're not instancing the real TextView which would be something like
TextView uritv = (TextView) findViewById(R.id.uritv);
which, of course, depends on the real nomenclature of your textview

Browse a image from SD card to show it in image view in android?

am use activity as a dialog.In that i have button to browse and select the image to show it in image view.
My Dialog activity code and browse image
public class Update_profile extends Activity{
private Uri mImageCaptureUri;
private static final int PICK_FROM_FILE = 1;
private View rootView;
Button save,browse_image;
ImageView profile_pic;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(new ColorDrawable(0));
setContentView(R.layout.update_profile);
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
#Override
public void uncaughtException(Thread t, Throwable e) {
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
});
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
this.rootView=findViewById(R.id.update_profile_details);
profile_pic = (ImageView)findViewById(R.id.update_profile_picture);
save = (Button)findViewById(R.id.save);
browse_image = (Button)findViewById(R.id.browse_image);
browse_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) return;
Bitmap bitmap = null;
String path = "";
if (requestCode == PICK_FROM_FILE) {
mImageCaptureUri = data.getData();
path = getRealPathFromURI(mImageCaptureUri); //from Gallery
Log.v("Path", ""+path);
if (path == null)
path = mImageCaptureUri.getPath(); //from File Manager
Log.v("Path", ""+path);
if (path != null)
bitmap = BitmapFactory.decodeFile(path);
} else {
path = mImageCaptureUri.getPath();
Log.v("Path", ""+path);
bitmap = BitmapFactory.decodeFile(path);
}
profile_pic.setImageBitmap(bitmap);
}
public String getRealPathFromURI(Uri contentUri) {
String [] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery( contentUri, proj, null, null,null);
if (cursor == null) return null;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
#SuppressLint("NewApi")
#Override
public boolean onTouchEvent(MotionEvent event) {
Rect rect = new Rect();
rootView.getHitRect(rect);
if (!rect.contains((int)event.getX(), (int)event.getY())){
setFinishOnTouchOutside(false);
return true;
}
return false;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
finish();
}
return true;
}
}
My manifest code to change the activity as a dialog
<activity
android:name="test.Update_profile"
android:excludeFromRecents="true"
android:launchMode="singleInstance"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Dialog"
android:windowSoftInputMode="stateHidden" >
</activity>
When am using above browse images code in ordinary activity means it fetch the images and show it in image view but in this dialog activity.its now able to pick the image.I want to pic the image from dialog activity.am not able to solve this issue.Can any one know please help me to solve this issue.
if u want to retrive direct from gallary then use this code
http://viralpatel.net/blogs/pick-image-from-galary-android-app/
this is my method for getting all file from sdcard and u just save this in one datacontroller class ohk dude
private void getallimages()
{
String[] STAR = { "*" };
final String[] columns = { MediaStore.Images.Thumbnails._ID , MediaStore.Images.Media.DATA};
final String orderBy = MediaStore.Images.Media.DEFAULT_SORT_ORDER;
Cursor imagecursor = ((Activity) cntx).managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, STAR, null, null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
int imgNameIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME);
int count = imagecursor.getCount();
for (int i = 0; i < count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
ImageItem imageItem = new ImageItem();
imageItem.id = id;
imageItem.filePath = imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA));
lastId = id;
imageItem.img = MediaStore.Images.Thumbnails.getThumbnail(cntx.getContentResolver(), id,MediaStore.Images.Thumbnails.MICRO_KIND, null);
imageItem.selection = false; //newly added item will be selected by default
controller.images.add(imageItem);
}
}
this is ImageItem wrapper class
public class ImageItem {
public boolean selection=true;
public int id;
public Bitmap img;
public String filePath;
public String fileType;
}
You can try this :
public class EMView extends Activity {
ImageView img,img1;
int column_index;
Intent intent=null;
// Declare our Views, so we can access them later
String logo,imagePath,Logo;
Cursor cursor;
//YOU CAN EDIT THIS TO WHATEVER YOU WANT
private static final int SELECT_PICTURE = 1;
String selectedImagePath;
//ADDED
String filemanagerstring;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img= (ImageView)findViewById(R.id.gimg1);
((Button) findViewById(R.id.Button01))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// in onCreate or any event where your want the user to
// select a file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
}
});
}
//UPDATED
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
//OI FILE Manager
filemanagerstring = selectedImageUri.getPath();
//MEDIA GALLERY
selectedImagePath = getPath(selectedImageUri);
img.setImageURI(selectedImageUri);
imagePath.getBytes();
TextView txt = (TextView)findViewById(R.id.title);
txt.setText(imagePath.toString());
Bitmap bm = BitmapFactory.decodeFile(imagePath);
img1.setImageBitmap(bm);
}
}
}
//UPDATED!
public String getPath(Uri uri) {
String[] projection = { MediaColumns.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
column_index = cursor
.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
imagePath = cursor.getString(column_index);
return cursor.getString(column_index);
}
}

Categories

Resources