分享

how to pick a image from gallery (SD Card) for my app in android?

 xue_dong5437 2011-09-11
http:///questions/2507898/how-to-pick-a-image-from-gallery-sd-card-for-my-app-in-android
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
   
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

   
switch(requestCode) {
   
case REQ_CODE_PICK_IMAGE:
       
if(resultCode == RESULT_OK){  
           
Uri selectedImage = imageReturnedIntent.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);
            cursor
.close();


           
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
       
}
   
}
}

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);
}

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多