How to set ImageView URI with URI String? - android

I have an app where a user can find an image from the gallery and it will be shown on the screen through a button click. The user can exit the app and the image they chose can be saved, so when they click the button again, it will also be shown. The image is shown the first time when it is picked, but the saving functionality does not seem to be working since the image is not being shown after the user closes and reopens the app.
I have tried looking up what camera permissions I may need, but I have not had any luck. I don't know what I would need since the app is able to load the image the first time.
//This is the method that shows the image
public void showImage(View view)
{
LinearLayout scroll = findViewById(R.id.imageViewer);
for(String i: imgDataList){
ImageView image = new ImageView(this);
image.setImageURI(Uri.parse(i));
scroll.addView(image);
}
}
//This is the method that works with the image that is gotten from the gallery
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
String imgData = data.getDataString();
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
imgDataList.add(imgData);
}
break;
case 1:
if(resultCode == RESULT_OK){
imgDataList.add(imgData);
}
break;
}

you may use sharedPreferences to store you image uri
please read more here about sharedPreferences and Save key-value data

Related

Capturing and displaying the same image

I am working on a semester project and it requires that I send images from an android device to server without storing the image. I want to make sure my cameta application is working properly. I want to check this using an ImageView that displays the captured image on the screen. How can I do it? I thought I could load from drawable but I cannot add files to drawable during runtime.
Thanks for any support
You don't need to save it to the drawable. These are the two steps:
1.Convert the captured image to Bitmap
2.Set the bitmap image to an imageview
Ref: StackOverflow Answer
private void takePicture(){ //you can call this every 5 seconds using a timer or whenever you want
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap picture = (Bitmap) data.getExtras().get("data");//this is your bitmap image and now you can do whatever you want with this
imageView.setImageBitmap(picture); //for example I put bmp in an ImageView
}
}

How to save multiple selected photos separately to upload to a server in android studio?

I have had a problem for weeks and still can not solve, did a lot of research and tested many codes, but nothing solved. I will explain my problem in detail.
I am making an application where in a layout there would be three or more clickable photos, I am using the “ArthurHub / Android-Image-Cropper” image cropping library, the steps are:
Click on the image, which gives the option to open the image gallery to select a photo or take a new photo, after that I can crop
the selected image.
Select the second photo, do the same as the previous one, thereafter.
Upload the photos to the server.
Steps 1 is working correctly, the problem arises when I try to select from the second image.
What I'm using: I'm using two classes: the one that requests the images and the one that returns:
1. would be the main window with ImageButton.
2. and the other class that returns the selected images to the first class. (Contains the function that calls the gallery or camera, cuts the image, the other “onActivityResult” function that returns the image address, who asked for the picture).
So far everything works correctly.
Problem Description: The problem arises from the second image selected. when the second class returns everything to the first, it is as if the second image is stored in the same memory space as the first (erasing everything previously), and if I select the third image, it deletes the second and only remains the third and henceforth. What I want to do is select the images and have them all seen at the same time (usable) to send to a server.
Solutions I tried:
After days of searching, the suggestion was to make several returns on “onActivityResult” so that it returned the result of selecting
images separately, I couldn't find anything that worked, the
explanations I found were only halfway (including the official
documentation does not detail the steps to control the various returns
of the function, is very superficial), could not control the separate
pointing of the images.
switch (requestCode){
case (1000):
Intent intent1 = new Intent(TirarFoto1.this, RegistrarAutomovelDuasRodas.class);
intent1.putExtra("class", classname);
intent1.putExtra("imageview", imageview);
intent1.putExtra("pathimage", pathimage);
startActivity(intent1);
break;
case (2000):
Intent intent2 = new Intent(TirarFoto1.this, RegistrarAutomovelDuasRodas.class);
intent2.putExtra("class", classname);
intent2.putExtra("imageview", imageview);
intent2.putExtra("pathimage", pathimage);
startActivity(intent2);
case (3000):
Intent intent3 = new Intent(TirarFoto1.this, RegistrarAutomovelDuasRodas.class);
intent3.putExtra("class", classname);
intent3.putExtra("imageview", imageview);
intent3.putExtra("pathimage", pathimage);
startActivity(intent3);
break;
default: break;
}
I decided to save each image separately outside the “temporary cache” folder (where they are located), even with the “manifest”
permissions, nothing happens, permission is denied, so I couldn't even
create the new folder to save the images in it, as a result does not
save the selected images.
if (ContextCompat.checkSelfPermission(TirarFoto1.this,Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
File file = new File(Environment.getExternalStoragePublicDirectory(String.valueOf(imageUri))+"folderName");
if (!file.exists()){
Toast.makeText(this, "exists", Toast.LENGTH_SHORT).show();
}
if (success){
Toast.makeText(this, "creaty", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "Erro!!!", Toast.LENGTH_SHORT).show();
}
}else {
requestStoragePermisson();
}
Result: The first solution did not work and the second did not work.
What I want: a model, a concrete way to help me, can anyone please help me with this? I have been stuck in this problem for a long time.
me to use ArthurHub-Android-Image-Cropper for cropped image, you can learn from this link
https://github.com/ArthurHub/Android-Image-Cropper/wiki
hope this help
Edit:
i have a class using ArthurHub-Android-Image-Cropper but from kotlin and i try to convert to java
like this
//for identify image
private Int imageNo;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//......
//set button image 1
final Button button1 = findViewById(R.id.button_image1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// set image count to 1
imageNo=1
getImageClick()
}
});
//set button image 2
final Button button1 = findViewById(R.id.button_image1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// set image count to 2
imageNo=2
getImageClick()
}
});
//set button image 3
final Button button1 = findViewById(R.id.button_image1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// set image count to 3
imageNo=3
getImageClick()
}
});
}
//select image form camera or galery
public void getImageClick() {
CropImage.startPickImageActivity(this);
}
//this for Crope Image
private void startCropImageActivity(Uri imageUri) {
CropImage.activity(imageUri)
.start(this);
}
#Override
#SuppressLint("NewApi")
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//handle result from onGetimageClick(button for select image from camera or galery)
if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == AppCompatActivity.RESULT_OK) {
Uri imageUri = CropImage.getPickImageResultUri(this, data);
//start Crope image
startCropImageActivity(imageUri);
}
// handle result of CropImageActivity
else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
Uri resultUri = result.getUri();
switch (imageNo){
case (1):
//here you have resultUri for save image or preview as image1
break;
case (2):
//here you have resultUri for save image or preview as image1
case (3):
//here you have resultUri for save image or preview as image1
}
}
}
don't forget to add manifest
<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="#style/Theme.AppCompat"/>
Hope this help

I could select an image from gallery in android. But, i want to select next and previous images of the selected image. How do i do it?

I could select an image from gallery in android. But, i want to select next and previous images of the selected image. How do i do it?
This works fine.. for selecting and then displaying the image..now what do i do for selecting next and previous images?
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
// Log.d(TAG, String.valueOf(bitmap));
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I could select an image from gallery in android
Your code happens to bring up a gallery app on your device. You are requesting ACTION_GET_CONTENT for image/*. This means:
Other apps, besides a gallery, can respond to that Intent and can be chosen by the user
There will be devices that do not have an app that would be considered a "gallery"
How do i do it?
Write your own gallery. There is no concept of "next" or "previous" with ACTION_GET_CONTENT.
There are many image picker libraries for Android that might form the basis for your own gallery UI.
I forgot to write the most important thing, which is code for previous and next is not working.. till now i have written code only for previous, coz once it works; next ll be just few changes ahead.
moveToprevious is coming false n cursor is declared as global.. so that when a query for cusor s fired in getcontentresolver() is fired, it gets the needed value..
Please look into this!

Android SDK - Reference the phone's gallery app?

As of right now, in my app I have created a rudimentary gallery app using the provided widget, I need this to select a picture from the phone. This is working fine and everything, but lacking very much in presentation.
I've got a couple apps on my phone that do the same thing, but they somehow use the gallery that's already in the phone to let the user select an image. FourSquare, for example, when you select an image to use as your picture, it loads the gallery and asks you to select an image.
How is this possible? I've scoured the internet for the last couple and have come up empty handed.
To get an image from the standard gallery you can do:
private static final int MEDIA_IMAGE_REQUEST_CODE = 203948; // This can be any unique number you like
Intent getImageFromGalleryIntent =
new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(getImageFromGalleryIntent, MEDIA_IMAGE_REQUEST_CODE);
Then to receive the image once the user has chosen one:
protected final void onActivityResult(final int requestCode, final int resultCode, final Intent i) {
super.onActivityResult(requestCode, resultCode, i);
if(resultCode == RESULT_OK) {
switch(requestCode) {
case MEDIA_IMAGE_REQUEST_CODE:
// Get the chosen images Uri
Uri imageUri = i.getData();
// Load the bitmap data from the Uri
// It is probably best to do this in an AsyncTask to avoid clogging up the Main Thread
break;
}
}

setting picture captured by built-in camera into an ImageView

in my app i m calling built in camera for capturing picture but i want to set that picture into an image view following is the code . so what code should i add to it to set the picture into imageview.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 0) {
String result = data.toURI();
// ...
}
}
Thanks in advance
Add a button and an ImageView to your main layout. When you click the button, first intent.putExtra to specify where the image will be stored, then start the camera intent. In your onActivityResult, if the resultCode comes back 0 that means the user accepted the picture. In this case, go grab the image from the path you specified as an extra of the intent. Create a bitmap from the file specified by the path, then set the image bitmap in your ImageView.

Categories

Resources