Skip to content

Instantly share code, notes, and snippets.

@arianimartins
Created May 29, 2014 22:29
Show Gist options
  • Save arianimartins/97accdd740c13cae18d4 to your computer and use it in GitHub Desktop.
Save arianimartins/97accdd740c13cae18d4 to your computer and use it in GitHub Desktop.
Pegar o path de fotos que estão em gerenciadores de arquivos e não apenas na galeria.
public class BrowsePicture extends Activity {
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private String filemanagerstring;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button) findViewById(R.id.Button01)).setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}
});
}
//UPDATED
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
filemanagerstring = selectedImageUri.getPath(); //OI FILE Manager
selectedImagePath = getPath(selectedImageUri); //MEDIA GALLERY
//NOW WE HAVE OUR WANTED STRING
if(selectedImagePath!=null){
//"selectedImagePath is the right one for you!"
}else{
//"filemanagerstring is the right one for you!"
}
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if(cursor!=null)
{
//HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL. THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}else{
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment