android studio cannot show the image in the image view - java

//global variable
static final int CAMERA_REQUEST = 1;
ImageView imageview;
Bitmap bitmap;
//on create:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//take reference
imageview = findViewById(R.id.image_view);
//the onclick listener and the intent:
Button take_photo_btn = findViewById(R.id.photo_btn);
take_photo_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST);
}
});
//this is the activity for result:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && requestCode == RESULT_OK) {
Bundle extras = data.getExtras();
bitmap = (Bitmap) extras.get("image");
imageview.setImageBitmap(bitmap);
}
}
after i take a photo i didnt see it in the image view whats is the problem?

If you are using Marshmallow then you have to implement Runtime Permissions for Camera using in Android.
More details go this link https://developer.android.com/training/permissions/requesting

Assuming that you have included the camera permission in your Manifest:
<uses-permission android:name="android.permission.CAMERA" />
Try to check if the user has already granted permission to use the camera:
if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED)
Then this may solve your problem in Marshmallow:
requestPermissions(activity, new String[] {Manifest.permission.CAMERA}, requestCode);

Ok i found the problem here:
if (requestCode == CAMERA_REQUEST && requestCode == RESULT_OK)
I need to do this:
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK)

Related

Issue swapping image

I have a ProfileActivity:
public class ProfileActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
final ImageView exampleImage = (ImageView) this.findViewById(R.id.exampleImageView);
exampleImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// [TODO] Implement application behavior when the user clicks the profile picture
//Toast.makeText(ProfileActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
startActivity(new Intent(ProfileActivity.this, GalleryActivity.class));
}
});
}
and I have a second activity called 'GalleryActivity'. the concept is to let the user choose a image from the gallery and then use it to replace the original Profile image.
public class GalleryActivity extends AppCompatActivity {
ImageView imageView;
private static final int PICK_IMAGE = 100;
Uri imageUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
imageUri = data.getData();
imageView.setImageURI(imageUri);
}
}
}
however, when I click on a image of the gallery, instead of taking it as new profile image, it take me back to the original image.
what is happening and how I can solve it?
You can use Github Library it will help you.
Here is the Link.
ImagePicker.Companion.with(this)
.crop() //Crop image(Optional), Check Customization for more option
.compress(1024) //Final image size will be less than 1 MB(Optional)
.maxResultSize(1080, 1080)
.start();//Final image resolution will be less than 1080 x 1080(Optional)
On Activity Result.
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
selectedFileURI = data.getData();
if (selectedFileURI != null && !Objects.requireNonNull(selectedFileURI.getPath()).isEmpty()) {
btnUpdate.setVisibility(View.GONE);
civUser.setImageURI(selectedFileURI);
Glide.with(context).load(selectedFileURI).into(civUser);
btnUploadDp.setVisibility(View.VISIBLE);
} else {
Functions.showSnackBar(context, "Cannot Get this Image");
}
}
}
}
Refernce Link.

ImageView and VideoView on same activity

I'm trying to display a captured image on my MainActivity. The problem is it says that onActivityResult is already defined. My first onActivityResult is for the VideoView. Now I have to put a new onActivityResult for my image. How do I make this possible? Do I only have to have one onActivityResult? Do I have to put the second onActivityResult to the first onActivityResult?
Here's my code:
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
Button buttonPlay;
Button buttonFullScreen;
static final int REQUEST_VIDEO_CAPTURE = 1;
VideoView resultvideo;
MediaController mediacontroller;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
resultvideo = (VideoView)findViewById(R.id.videoView);
mediacontroller = new MediaController(MainActivity.this);
mediacontroller.setAnchorView(resultvideo);
resultvideo.setMediaController(mediacontroller);
Button click = (Button)findViewById(R.id.buttonRecord);
resultvideo = (VideoView)findViewById(R.id.videoView);
this.imageView = (ImageView)this.findViewById(R.id.imageView);
Button photoButton = (Button) this.findViewById(R.id.buttonCapture);
photoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
public void dispatchTakeVideoIntent(View v) {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final Uri videoUri = data.getData();
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
resultvideo.setVideoURI(videoUri);
mediacontroller.setAnchorView(resultvideo);
resultvideo.pause();
}
buttonPlay = (Button) findViewById(R.id.buttonPlay);
{
buttonPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediacontroller.show();
mediacontroller.setAnchorView(resultvideo);
resultvideo.start();
}
});
}
buttonFullScreen = (Button) findViewById(R.id.buttonFullScreen);
{
buttonFullScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("VIDEO_URI", videoUri.toString());
startActivity(intent);
}
});
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
create two view ImageView for Image and VideoView for Video and use this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case CAMERA_REQUEST:
if (resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
//Hide video view
videoview.setVisibility(View.GONE);
}
break;
case REQUEST_VIDEO_CAPTURE:
//bring your video stuff here
videoview.setVisibility(View.VISIBLE);
imageView.setVisibility(View.GONE);
break;
}
}
You need only a single onActivityResult(int requestCode, int resultCode, Intent data) for your Activity class
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case CAMERA_REQUEST:
if (resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
break;
case REQUEST_VIDEO_CAPTURE:
//bring your video stuff here
break;
}
}
Your onActivityResult should look like the above code. Every function can be declared with a the same types and order of parameters only once in a class.
You can not have two function with the same signature.
You have to have a single onActivityResult, and in the if statement, you have to handle the resoult.
Example
if(resultCode == Activity.RESULT_OK){
if (requestCode == CAMERA_REQUEST ) {
...
}
if (requestCode == REQUEST_VIDEO_CAPTURE ) {
...
}
}

Android ViewPager issue

I am working on android Launcher, Where it contains three activities, The first activity is HomeActivity which contain four buttons which will be perform intent operations and the rest of the activities are blank but, At HomeActivity when I click on contact button it will display contact list, but when I choose one of the contact it shows nothing and the rest of the buttons also behave like contact button it will not jump to the OnActivityResult() method so how can I fix this problem?
Below is my code:-
MainHomeActivity.java
public class MainHomeActivity extends AppCompatActivity {
DevicePolicyManager devicePolicyManager;
ComponentName adminComponent;
Button apps;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Button lock = (Button) findViewById(R.id.button_lock);
lock.setOnClickListener(btnListener);
Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT,15000);
apps = (Button) findViewById(R.id.main_menu);
}
Button.OnClickListener btnListener = new Button.OnClickListener() {
#Override
public void onClick(View v) {
adminComponent = new ComponentName(MainHomeActivity.this, AdminReceiver.class);
devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
if (!devicePolicyManager.isAdminActive(adminComponent)) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminComponent);
startActivityForResult(intent, 50);
} else {
devicePolicyManager.lockNow();
}
apps.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainHomeActivity.this, AppsListActivity.class);
startActivity(i);
}
});
}
};
}
HomeActivity.java
public class HomeActivity extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_home_activity);
ViewPager viewPage = (ViewPager) findViewById(R.id.viewpager);
viewPage.setAdapter(new CustomPageAdapter(this));
}
public void contact(View view){
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
if(intent.resolveActivity(getPackageManager()) != null){
startActivityForResult(intent,10);
}
}
public void audioIntent(View view){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getParent()+"/ext_card");
intent.setDataAndType(uri,"audio/*");
startActivityForResult(intent,20);
}
public void videoIntent(View v){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getParent()+"/ext_card");
intent.setDataAndType(uri,"video/*");
startActivityForResult(intent,30);
}
public void pdfIntent(View view){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getParent()+"/ext.card");
intent.setDataAndType(uri,"pdf/*");
startActivityForResult(intent,40);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == RESULT_OK && resultCode == 10){
Uri contactUri = data.getData();
Intent contactIntent = new Intent(Intent.ACTION_CALL);
contactIntent.setData(contactUri);
startActivity(contactIntent);
}
if(resultCode == 20 && requestCode == RESULT_OK){
Uri audioUri = data.getData();
Intent audioIntent = new Intent(Intent.ACTION_VIEW);
audioIntent.setDataAndType(audioUri,"audio/*");
startActivity(audioIntent);
}
if(requestCode == RESULT_OK && resultCode == 30){
Uri videoUri = data.getData();
Intent videoIntent = new Intent(Intent.ACTION_VIEW);
videoIntent.setDataAndType(videoUri,"video/*");
startActivity(videoIntent);
}
if(requestCode == RESULT_OK && resultCode == 40){
Uri pdfUri = data.getData();
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(pdfUri,"pdf/*");
startActivity(pdfIntent);
}
}
}
CustomPagerAdapter.java
public class CustomPageAdapter extends PagerAdapter {
private Context mContext;
public CustomPageAdapter(Context context){
mContext = context;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ModelObject modelObject = ModelObject.values()[position];
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(),container,false);
container.addView(layout);
return layout;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public int getCount() {
return ModelObject.values().length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public CharSequence getPageTitle(int position) {
ModelObject customPagerEnum = ModelObject.values()[position];
return mContext.getString(customPagerEnum.getTitleResId());
}
}
This is the screenshot of the application:-
You are doing wrong in your if conditions. You are trying to compare requestCode with RESULT_OK and resultCode with request codes (10,20,30 and 40) which is totally wrong.
Use:
if(requestCode == 10 && resultCode == RESULT_OK)
Instead of:
if(requestCode == RESULT_OK && resultCode == 10)
Update onActivityResult() as below:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 10 && resultCode == RESULT_OK){
Uri contactUri = data.getData();
Intent contactIntent = new Intent(Intent.ACTION_CALL);
contactIntent.setData(contactUri);
startActivity(contactIntent);
}
if(requestCode == 20 && resultCode == RESULT_OK){
Uri audioUri = data.getData();
Intent audioIntent = new Intent(Intent.ACTION_VIEW);
audioIntent.setDataAndType(audioUri,"audio/*");
startActivity(audioIntent);
}
if(requestCode == 30 && resultCode == RESULT_OK){
Uri videoUri = data.getData();
Intent videoIntent = new Intent(Intent.ACTION_VIEW);
videoIntent.setDataAndType(videoUri,"video/*");
startActivity(videoIntent);
}
if(requestCode == 40 && resultCode == RESULT_OK){
Uri pdfUri = data.getData();
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(pdfUri,"pdf/*");
startActivity(pdfIntent);
}
}
Hope this will help~

Access camera for multiple buttons

How do I set up multiple buttons to access the camera and save the photo?
I have figured out how to have one button do this task, but cannot figure out how to get multiple buttons to do this.
My Java Code so far:
package com.example.android.phototaker;
public class MainActivity extends AppCompatActivity {
ImageView result;
static final int REQUEST_IMAGE_CAPTURE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button click = (Button)findViewById(R.id.signbutton);
result = (ImageView)findViewById(R.id.imageView);
}
public void dispatchTakePictureIntent(View view) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
result.setImageBitmap(imageBitmap);
}
}
}
By multiple buttons do you mean, different buttons opening up the camera?
If so, you will need to add an onclick listener for those buttons.

Display image gallery with ImageView

I want to do so: Press the button opens gallery, choose a picture and it appears in the program ImageView. Picture selected all works only displayed nothing, just an empty area.
//upload photo
uploadPhoto.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
final int SELECT_PHOTO = 1234;
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 1234:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
ImageView photoRegistration = (ImageView) findViewById(R.id.testSet);
photoRegistration.setImageURI(selectedImage);
}
}
}
xml
<ImageView
android:id="#+id/testSet"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:src="#drawable/common_signin_btn_icon_pressed_light" />
try with:
Bitmap bitmap_photo = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);

Categories

Resources