How to create image views inside onActivityResult method in Android? - android

I want to dynamically create image views inside the onActivityResult method.
If i define my ImageView like this: imageView = (ImageView) findViewById(R.id.image_view); works perfectly with this code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PHOTO && resultCode == RESULT_OK) {
Uri uri = data.getData();
StorageReference photoStorageReference = storageReference.child(uri.getLastPathSegment());
photoStorageReference.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUri = taskSnapshot.getDownloadUrl();
Picasso.with(StorageActivity.this).load(downloadUri).fit().centerCrop().into(imageView);
}
});
}
}
But if i create the image views inside the onActivityResult method like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PHOTO && resultCode == RESULT_OK) {
Uri uri = data.getData();
StorageReference photoStorageReference = storageReference.child(uri.getLastPathSegment());
photoStorageReference.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUri = taskSnapshot.getDownloadUrl();
ImageView imageView = new ImageView(getApplicationContext());
linearLayout.addView(imageView);
Picasso.with(StorageActivity.this).load(downloadUri).fit().centerCrop().into(imageView);
}
});
}
}
The image views are not displayed. I tried to create programmatically those image views in the onCreate method, but same problem. Nothing is displayed. If i create in stead of ImageView, buttons, the buttons are correctly displayed. What is wrong with my code?
Thanks in advance!

You forgot to specify the LayoutParameters for the newly added view. Like below
imageView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

Related

Parsing Uri with CircleImageView

How do I Parse uri with de.hdodenhof.circleimageview.CircleImageView?
My code
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
final Uri uri = data.getData();
StorageReference path = mStoragereference.child("Photos").child(uri.getLastPathSegment());
path.putFile(uri).addOnSuccessListener(new OnSuccessListener < UploadTask.TaskSnapshot > () {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
UserProfileChangeRequest profileUpdate = new UserProfileChangeRequest.Builder()
.setPhotoUri(Uri.parse(userPhoto))
.build();
if (userPhoto == null) {
Toast.makeText(EditInfo.this, "Error updating image",
Toast.LENGTH_SHORT).show();
}
Thanks for your response!
Since CircleImageView is a subclass of ImageView it inherits most of its properties/methods from ImageView.
Unfortunately it seems that ImageView doesn't expose its source URI, so you'll have to keep the URI that you pass into the CircleImageView around.

Uri Returns null in onActivityResult

I am doing a module which needs to convert image into pdf. i have successfully implemented the camera and can display its image. but my problem is getting the uri of that image. i saw a code snippet here in StackOverflow and followed it but it returns null.
here is my sample code:
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
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);
uri = data.getData();
if(uri == null)
{
tvUri.setText("null");
}else{
tvUri.setText(uri.toString());
}
}
}
to test if it is null, i proceeded to set the textview into its value if it has one, but if not, then i set it to null.
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
Uri u = intent.getData();
}
try this
uri = data.getExtras().get("data");
instead of
uri = data.getData();

Not able to upload an Image in Firebase Storage

This is the my code ( check this file full code on GitHub) for Uploading an image to Firebase Storage.
// ImagePickerButton shows an image picker to upload a image for a message
mPhotoPickerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
i.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(i, "Complete action using"), RC_PHOTO_PICKER);
}
});
This is the OnActivityResult Method:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
//get the reference to stored file at database
StorageReference selectedImageFilePath = mStorageReference.child(selectedImageUri.getLastPathSegment());
//upload file to firebase
selectedImageFilePath.putFile(selectedImageUri).addOnSuccessListener(MainActivity.this, new OnSuccessListener < UploadTask.TaskSnapshot > () {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
#SuppressWarnings("VisibleForTests")
String downloadUrl = taskSnapshot.getDownloadUrl().toString();
Message message = new Message(null, userName, downloadUrl);
messagesDatabaseReference.push().setValue(message);
}
});
}
}
}
Picking an Image from the Gallery works fine but I'm not able to upload the selected Image on Firebase Storage. Correct my code If I'm wrong at any site. However, I manually entered these lines in my manifest file
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I'm currently running the app in my own mobile device connected via USB( Moto G4 - Nougat)
data.getData() could return an URI that does not point to a File.
You should use
InputStream is = getContentResolver().openInputStream(data.getData())
and than use
selectedImageFilePath.putStream(is)
Also you could add an onFailureListener to the upload task and debug what is going wrong.
In addition, if I recall correctly, I do not think you need the storage permissions when you directly read the data in onActivityResult.
Try this for button click :
//Select Image functionality
btnselectImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 2);
}
});
For saving image to fire base :
//Storing and getting image from firebase
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
final Uri uri = data.getData();
StorageReference filepath = mStorageRef.child("Photos").child(name);
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUri = taskSnapshot.getDownloadUrl();
//Getting url where image is stored
saveImage(downloadUri);
Picasso.with(getActivity()).load(downloadUri).fit().centerCrop().into(profileImage);
}
});
}
This works fine for me, Hope this will help.
Instead of else if statement, I modified it by writing a seperate if statement.
if (requestCode == RC_SIGN_IN) {
if (resultCode == RESULT_OK) {
}else if (resultCode == RESULT_CANCELED) {
}
// This was the change I made
if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {
}
}
If ( //sign in)
{
If (//result ok)
{}
else if (//result cancel)
{}
}
else if (//rc photot picker && result ok)
{ //your storage code here.....}
tats works perfectly.....check out the braces carefully....

App crashes when trying to upload to Firebase

When I try to upload to Firebase my app is crashing with the following error.
I have an 'upload' button that is being used to invoke the camera. Upon clicking it, the image is not uploaded to Firebase.
My code:
private Button mUploadBtn;
private ImageView mImageView;
private static final int CAMERA_REQUEST_CODE = 1;
private StorageReference mStorage;
Uri photoURI;
private ProgressDialog mProgressDialog;
private static final int GALLERY_INTENT = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStorage = FirebaseStorage.getInstance().getReference();
mUploadBtn = (Button) findViewById(R.id.upload);
mImageView = (ImageView) findViewById(R.id.imageView);
mProgressDialog = new ProgressDialog(this);
mUploadBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
mProgressDialog.setMessage("Uplaoding");
mProgressDialog.show();
Uri uri = data.getData();
StorageReference filepath = mStorage.child("Photos").child("file");
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
mProgressDialog.dismiss();
Toast.makeText(MainActivity.this,"Done",Toast.LENGTH_LONG).show();
}
});
}
}
It looks like you're not working with the camera intent correctly. The code you show is expecting that the intent returned by the camera app contains a Uri to the captured image via its getData() method. That's not the way it works.
I recommend you follow this tutorial instead to work with the camera.
override this method
and do the following steps
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK)
{
Uri uri = data.getData();
StorageReference image_path = storageReference.child("Camera Photos").child(uri.getLastPathSegment());
image_path.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(MainActivity.this, "Image Uploaded", Toast.LENGTH_SHORT).show();
}
});
}
}

Nullpointer exception when I tried to upload a picture to firebase taken from the camera

I'm trying to upload a picture taken from the camera and I got Nullpointer exception, here its the code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
uri= data.getData();
mImagenIv.setImageURI(uri);
StorageReference filepath = mStorage.child("fotos").child(uri.getLastPathSegment());
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getApplicationContext(),"si señor",Toast.LENGTH_LONG).show();
}
});
} else if(requestCode == CAMERA_INTENT && resultCode == RESULT_OK){
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
Uri uri= data.getData();
mImagenIv.setImageBitmap(bitmap);
StorageReference filepath = mStorage.child("fotos").child(uri.getLastPathSegment());
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getApplicationContext(),"si señor",Toast.LENGTH_LONG).show();
}
});
}
}
The thing its that when I use the same code when I select a picture from the storage that code its working as expected.The null pointer I got its on the following line:
StorageReference filepath = mStorage.child("fotos").child(uri.getLastPathSegment());
Could someone helpme with that?
Assuming that the startActivityForResult() call for CAMERA_INTENT is using ACTION_IMAGE_CAPTURE, data.getData() should always return null. If Bitmap bitmap = (Bitmap) data.getExtras().get("data"); is working, that Bitmap is your photo. You will need to get that to Firebase by one means or another. If the Firebase API does not accept a Bitmap, you may have to use compress() to write that Bitmap to some temporary file to use with Firebase.

Categories

Resources