-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from EPICSGroup/ibrahim
Ibrahim image attach form
- Loading branch information
Showing
7 changed files
with
415 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
app/src/main/java/com/sf/stormwaterutilityandroid/ImageAttachForm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
package com.sf.stormwaterutilityandroid; | ||
|
||
import android.Manifest; | ||
import android.content.Intent; | ||
import android.content.pm.PackageManager; | ||
import android.graphics.Bitmap; | ||
import android.net.Uri; | ||
import android.os.Bundle; | ||
import android.provider.MediaStore; | ||
import android.widget.Button; | ||
import android.widget.ImageView; | ||
|
||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.core.app.ActivityCompat; | ||
import androidx.core.content.ContextCompat; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
public class ImageAttachForm extends AppCompatActivity { | ||
private static final int OPEN_GALLERY_REQUEST = 2; | ||
private static final int OPEN_CAMERA_REQUEST = 3; | ||
|
||
private Button uploadCameraImageBtn; | ||
private Button uploadGalleryImageBtn; | ||
private Button uploadImageBackBtn; | ||
|
||
|
||
private ArrayList<Uri> imageURIs; | ||
|
||
private ArrayList<ImageView> imageViews; | ||
|
||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_image_attach_form); | ||
|
||
this.uploadCameraImageBtn = findViewById(R.id.uploadCameraImageBtn); | ||
this.uploadGalleryImageBtn = findViewById(R.id.uploadGalleryImageBtn); | ||
this.uploadImageBackBtn = findViewById(R.id.uploadImageBackBtn); | ||
|
||
// this.imageView = findViewById(R.id.ImageView1); | ||
|
||
|
||
// this.imageUri = null; | ||
|
||
this.imageURIs = new ArrayList<>(3); | ||
|
||
imageViews = new ArrayList<>(); | ||
|
||
this.imageViews.add(findViewById(R.id.ImageView1)); | ||
this.imageViews.add(findViewById(R.id.ImageView2)); | ||
this.imageViews.add(findViewById(R.id.ImageView3)); | ||
|
||
initActionListeners(); | ||
} | ||
|
||
private void initActionListeners() { | ||
|
||
uploadImageBackBtn.setOnClickListener(view -> { | ||
if (imageURIs == null) { | ||
setResult(RESULT_CANCELED); | ||
finish(); | ||
} | ||
|
||
Intent data = new Intent(); | ||
// data.putExtra("imageURIs", imageURIs.toArray()); | ||
Bundle args = new Bundle(); | ||
args.putSerializable("IMAGEURIS", (Serializable) imageURIs); | ||
data.putExtra("BUNDLE", args); | ||
setResult(RESULT_OK, data); | ||
finish(); | ||
}); | ||
|
||
|
||
// pick from gallery | ||
uploadGalleryImageBtn.setOnClickListener(view -> { | ||
chooseGalleryImage(); | ||
}); | ||
|
||
// pick from camera | ||
uploadCameraImageBtn.setOnClickListener(view -> { | ||
// request camera permission if not granted already | ||
if (ContextCompat.checkSelfPermission(ImageAttachForm.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { | ||
ActivityCompat.requestPermissions(ImageAttachForm.this, | ||
new String[]{ | ||
Manifest.permission.CAMERA | ||
}, | ||
100); | ||
} | ||
|
||
chooseCameraImage(); | ||
}); | ||
|
||
imageViews.get(0).setOnClickListener(view -> { | ||
imageURIs.remove(0); | ||
updateImageViews(); | ||
}); | ||
|
||
imageViews.get(1).setOnClickListener(view -> { | ||
imageURIs.remove(1); | ||
updateImageViews(); | ||
}); | ||
|
||
imageViews.get(2).setOnClickListener(view -> { | ||
imageURIs.remove(2); | ||
updateImageViews(); | ||
}); | ||
} | ||
|
||
private void chooseCameraImage() { | ||
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | ||
startActivityForResult(intent, OPEN_CAMERA_REQUEST); | ||
} | ||
|
||
|
||
private void chooseGalleryImage() { | ||
Intent intent = new Intent(); | ||
intent.setType("image/*"); | ||
intent.setAction(Intent.ACTION_GET_CONTENT); | ||
startActivityForResult(intent, OPEN_GALLERY_REQUEST); | ||
} | ||
|
||
@Override | ||
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { | ||
super.onActivityResult(requestCode, resultCode, data); | ||
if (requestCode == OPEN_GALLERY_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) { | ||
Uri newImageUri = data.getData(); | ||
//this.imageView.setImageURI(imageUri); | ||
addImage(newImageUri); | ||
|
||
// uploadPicture(); | ||
} else if (requestCode == OPEN_CAMERA_REQUEST && resultCode == RESULT_OK && data != null && data.getExtras() != null) { | ||
Bundle extras = data.getExtras(); | ||
Bitmap imageBitmap = (Bitmap) extras.get("data"); | ||
|
||
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); | ||
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes); | ||
String path = MediaStore.Images.Media.insertImage(getContentResolver(), imageBitmap, "Title", null); | ||
// this.imageUri = Uri.parse(path); | ||
Uri newImageUri = Uri.parse(path); | ||
addImage(newImageUri); | ||
|
||
|
||
// uploadPicture(); | ||
} | ||
} | ||
|
||
// private void uploadPicture() { | ||
// final String randomKey = UUID.randomUUID().toString(); | ||
// StorageReference storageRef = storageReference.child("images/" + randomKey); | ||
// storageRef.putFile(imageUri) | ||
// .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { | ||
// @Override | ||
// public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { | ||
// System.out.println("SUCCESSS!!!"); | ||
// Task<Uri> downloadUri = taskSnapshot.getStorage().getDownloadUrl(); | ||
// System.out.println("downloadUri: " + downloadUri); | ||
// } | ||
// }) | ||
// .addOnFailureListener(new OnFailureListener() { | ||
// @Override | ||
// public void onFailure(@NonNull Exception e) { | ||
// System.out.println("FAILEDDD"); | ||
// } | ||
// }); | ||
// } | ||
|
||
// adds image to imageURIs list and to the recycle view of displayed images | ||
private void addImage(Uri newImageUri) { | ||
System.out.println("Image URI HEREEE!!!: " + newImageUri); | ||
|
||
|
||
if (imageURIs.size() == 3) { | ||
// send message to remove 1 | ||
System.out.println("EARLY RETURN !!!!!"); | ||
return; | ||
} | ||
|
||
this.imageURIs.add(newImageUri); | ||
|
||
System.out.println("ImageURIs:\n" + imageURIs); | ||
updateImageViews(); | ||
} | ||
|
||
private void updateImageViews() { | ||
// set images to first views | ||
int i = 0; | ||
for (i = 0; i < imageURIs.size(); i++) { | ||
imageViews.get(i).setImageURI(imageURIs.get(i)); | ||
} | ||
|
||
// set the rest to invisible | ||
while (i < 3) imageViews.get(i++).setImageURI(null); | ||
} | ||
} |
Oops, something went wrong.