Reputation: 584
I want to display a captured image from a Camera Intent inside an ImageView. I am able to get the camera to open and make a directory for the image, but when I pass the image through the intent and attempt to call it, the image is not displayed. This is what I am doing:
private static final int REQUEST_CODE = 1;
private ImageView addPicture;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(//setting the view);
addPicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs(); // <----
File image = new File(imagesFolder, "image_001.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imageIntent, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
addPicture = (ImageView) findViewById(R.id.profilepicture_registerone);
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
try {
InputStream stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
addPicture.setImageBitmap(bitmap);
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
}
RoundImage image = new RoundImage(bitmap);
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
Toast.makeText(getApplicationContext(), "Bad Image Request. Please Try Again!", Toast.LENGTH_LONG).show();
}
}
}
My Permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
And My ImageView in XML:
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/profilepicture_registerone"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:clickable="true"
android:onClick="addPicture" />
EDIT: I had the incorrect IDs due to me attempting to change things for this post. I changed them back to the original ID to avoid any more confusion.
EDIT 2: After further testing, I've found that no Images are being set inside of the onActivityResult()
method. I tried loading a drawable image and that did not work either. It worked when I called it inside of onCreate()
.
Upvotes: 0
Views: 3084
Reputation: 1238
Try this ,
Declare the constant in Class area:
final int TAKE_PHOTO_REQ = 100;
Invoke Camera Intent:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PHOTO_REQ);
Add onActivityResult(..) to your Activity
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PHOTO_REQ: {
if (resultCode == TakePicture.RESULT_OK && data != null) {
Bitmap myBmp = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(myBmp);
break;
}
}
}
}
*Still having problem then check your ImageView ,if it is correct or not.
Hope this will be helpful ... thanks
Upvotes: 1
Reputation: 2825
You need to find your ImageView ID in onCreate()
with proper ID tag.
addPicture = (ImageView) findViewById(R.id.profilepicture_registerone);
And
Please check your bitmap, is it null or not?
Upvotes: 1
Reputation: 26
Are you sure, you are using the correct id for your imageview. In the code, the id in the xml is "picture" but you refer to profilepicture_registerone in java ?
Upvotes: 0