Hi I am new to java and android studio and have followed some tutorials to get images from the gallery and display it in a circular image view.
here is my code:
private void openImageFrom() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, IMAGE_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_CODE && resultCode == RESULT_OK && data != null && data.getData() != null) {
imageUri = data.getData();
avatar.setImageURI(imageUri);
}
}
The problem is that the circleImageView moves based on the image. I need it to stay in its fixed position. Does anyone know what the problem could be?
I am using this dependency in module:app:
implementation 'de.hdodenhof:circleimageview:3.0.1'
And this is my XML
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/circleImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0px"
android:src="#drawable/profile_avatar"
app:civ_border_color="#color/DarkGreen"
app:civ_border_overlay="true"
app:civ_border_width="12dp" />
woops fixed my own issue nevermind.
changed XML:
android:layout_width="350dp"
android:layout_height="350dp"
Related
I want to croping image from other activity to another activity with Canhub Android Image Cropper library. This is my code :
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
mCropImageUri = result.getOriginalUri();
createImage();
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
AppLogger.e(result.getError().getMessage());
}
}
if (requestCode == PermissionCheckUtils.LOCATION_PERMISSION_REQUEST_CODE && resultCode == RESULT_OK) {
getLocation();
}
}
and this when i access the camera :
private void openCropImage() {
Intent intent = CropImage.activity().setGuidelines(CropImageView.Guidelines.ON).getIntent(this);
startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
}
with these code, i open camera but source include galery. My question is how to open the croper with source just from camera. I already read the documentation but I'm confused : https://github.com/CanHub/Android-Image-Cropper
It is very simple !!
In Your openCropImage() Function set following code :
Intent intent = CropImage
.activity()
.setImageSource(includeGallery = false, includeCamera = true)
.setGuidelines(CropImageView.Guidelines.ON)
.getIntent(this);
startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
That's it !! Happy Coding :-)
I have a project organized as follows:
AdminAddNewMerchantActivity
->TimeFragment
---->FindFoodAdminFragment
---->MapMerchantFagment
---->PriceFragment
---->StartFragment
---->EndFragment
->OrdersFragment
---->IncompleteOrders
---->CompleteOrders
PickImageActivity
where:
-> - a new subdirectory
----> - a subsubdirectory
I'm trying to pick an image and assign it. I successfully got it to work in PickImageActivity, but have problems getting the same code to work from FindFoodAdminFragment. The code for PickImageActivity that I use is as follows:
public class PickImageActivity extends AppCompatActivity {
ImageView image;
CircleImageView imageBanner;
private Uri imageUri, imageUri2;
private static final int GalleryPick = 1, GalleryPickBanner = 2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pick_image);
.
.
.
image = findViewById(R.id.pi_input_merchant_image);
imageBanner = findViewById(R.id.pi_input_banner_image);
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GalleryPick);
.
.
.
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if((requestCode == GalleryPick) && (resultCode == RESULT_OK) && (data!=null))
{
imageUri = data.getData();
image.setImageURI(imageUri);
// Prevalent.currentMerchant.setImage();
}
else if((requestCode == GalleryPickBanner) && (resultCode == RESULT_OK) && (data!=null))
{
imageUri2 = data.getData();
imageBanner.setImageURI(imageUri2);
}
}
}
}
The problem is in startActivityForResult(galleryIntent, GalleryPick); I tried super.startActivityForResult(galleryIntent, GalleryPick); and getParentFragment().startActivityForResult(galleryIntent, GalleryPick); but it doesn't work. How do I get it to work from FindFoodAdminFragment?
I solved it by starting a new activity from FindFoodAdminFragment and using getIntent().getExtras() and intent.putExtra("myKey", myString) to transmit data between the activity and fragment. In an activity startActivityForResult works fine so I used it in the activity then onActivityResult activity is triggered. In onActivityResult i putExtra the imageUri, then I started another activity which contained FindFoodAminFragment and getExtras. Thanks for help all.
I'm developing an Android app in which the user will be asked to upload 2 images from his phone gallery.
My activity has one "Upload" button + 2 ImageViews to show the selected images before proceeding to the next activity.
Everything seems to work fine but both ImageViews are filled with just one of the images I select and I don't know why.
I searched on Google and on this website finding a lot of similar questions, but none of them helped me. Since I'm not an expert I could be missing something stupid and easy, but I'm quite lost at this point and decided to create a post.
Here is my Java code from the activity:
public class UploadActivity extends AppCompatActivity {
//Button and ImageViews have the same names for layout IDs
Button uploadBtn;
ImageView imgOne;
ImageView imgTwo;
public static final int PICK_IMAGE = 100;
public static Uri imgUri1;
public static Uri imgUri2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
uploadBtn = (Button)findViewById(R.id.uploadBtn);
imgOne = (ImageView)findViewById(R.id.imgOne);
imgTwo = (ImageView)findViewById(R.id.imgTwo);
uploadBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openGallery();
}
});
}
private void openGallery() {
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
gallery.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
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) {
imgUri1 = data.getData();
imgOne.setImageURI(imgUri1);
imgUri2 = data.getData();
imgTwo.setImageURI(imgUri2);
}
}
}
This code works but shows just one selected image for BOTH ImageViews.
Looks like it's skipping the second image and assigns its ImageView to the first one more time.
Your Problem is this piece of code:
imgUri1 = data.getData();
imgOne.setImageURI(imgUri1);
imgUri2 = data.getData();
imgTwo.setImageURI(imgUri2);
You set imgUri1 AND imgUri2 to data.getData(). So imgUri1 and imgUri2 are exact the same. So you set the same Uri to both imageViews.
Maybe should set data to array data[0]=data1fromimg1 and data[1]=data2fromimg1
in your code like this
<br>
imgUri1 = data[0].getData();<br>
imgOne.setImageURI(imgUri1);<br>
imgUri2 = data[1].getData();<br>
imgTwo.setImageURI(imgUri2);
make sure that receive data
Solved by myself after many tries.
The solution was to use a for like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE && resultCode == RESULT_OK && data.getClipData() != null) {
int count = data.getClipData().getItemCount();
for(int i = 0; i < count; i++) {
Uri screen = data.getClipData().getItemAt(i).getUri();
imgUri1 = data.getClipData().getItemAt(0).getUri();
imgOne.setImageURI(imgUri1);
imgUri2 = data.getClipData().getItemAt(1).getUri();
imgTwo.setImageURI(imgUri2);
}
}
}
I'm trying to access the image gallery and I have these code. Content Resolver turned red in the fragment. I've been trying tweaks, but still it isn't solved.
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK && data!= null && data.getData() != null){
Uri filepath = data.getData();
bitmap = (Bitmap) MediaStore.Images.Media.getBitmap(getContentResolver, filepath);
}
}
Try these variations:
getActivity().getApplicationContext().getContentResolver()
or simply
getActivity().getContentResolver()
I have a button in Fragment when I press it I open a new activity for result but When I return back to my fragment I found all UI element = null
Please find the code
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), MyActivity.class);
getActivity().startActivityForResult(intent, "3030");
}
});
when choose a value from activity I should back to fragment and set data to textview in the activity.
Intent intent = Activity.this.getIntent();
intent.putExtra("categoryId", id);
intent.putExtra("categoryName", name);
setResult(RESULT_OK, intent);
finish();
and I have put that in the activity that contains the fragment
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 3030 && resultCode == RESULT_OK) {
Fragment fragment = mTabFragments.get(MyFragment.class.getName());
if (fragment != null) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
}
and in fragment
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 3030 && resultCode == Activity.RESULT_OK) {
int categoryId = data.getIntExtra("categoryId", 0);
String categoryName = data.getStringExtra("categoryName");
mChooseCategoryTextView.setText(categoryName);
}
}
the problem now that mChooseCategoryTextView is null
Can anyone tell me what is the problem?
To get result in fragment
startActivityForResult(intent,REQ_CODE);
not
getActivity().startActivityForResult(intent,REQ_CODE);
I think the question how you initialise this view?
Since your lunching another activity so your whole fragment my be reconstruct or only onViewCreated recalled again.
So my guess is that you don't reinitialize or override mChooseCategoryTextView reference in one of fragment callbacks.
Try to add more logs and check what's happening for this reference
I believe the error is in the line
Fragment fragment = mTabFragments.get(MyFragment.class.getName());
I assume mTabFragments in an adapter of some sort? I'd have to look at it's code to be sure, but it sounds like it's not returning the right fragment. Make sure that the reference it's returning is the same as the fragment that is being shown on screen.