i'm trying to do this code in android-studio, but i don't understand why the selectedImageUri and selectedImagePath are both null. This is the code:
In Android-manifest i have included :
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
this is my layout:
<Button android:text="Browse gallery"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick">
</Button>
and this is my java code:
public class MainActivity extends ActionBarActivity {
private static final int SELECTED_PICTURE = 1;
ImageView iv;
String filePath;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
// 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"), SELECTED_PICTURE);
// System.out.println("sonoqui");
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
// System.out.println("sonoqui1");
if (requestCode == SELECTED_PICTURE) {
// System.out.println("sonoqui2");
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
System.out.println("sonoqui25" + selectedImagePath);
// Log.v("IMAGE PATH====>>>> ", 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);
}
}
Aucun commentaire:
Enregistrer un commentaire