Loading an image from local android storage to WebView - android

Is it possible to load a picture to the WebView from the local android phone storage ? Basically what I'm trying to do is, take a picture, then display that picture inside the WebView (like a preview picture) then after filling more information, upload that picture with whole data to the database. Everything is in android studio Java code, the WebView are obviously .html files which I load from the assets folder.
I was thinking maybe I need to create an <img id="image"></img> then get the ID somehow ? But how do I store the picture inside even if I manage to get the ID ?
I've done all the camera stuff, so I can open the camera (through my WebView) and take a picture, the picture saves inside the path which I do know and I know the name of the picture. Also I can currently select the picture from the phone, but the thing is it only selects and does not do anything, so the selection is useless.
If You need any code, I'll edit.

So I found a solution to what I wanted to do and I'm posting an answer here if anyone someday wonders into this post and wants to know the solution.
Basically after taking a picture I call the javascript function which puts the taken image inside my .html file between <img></img> tags as the source.
This is my onActivityResult in MainActivity.java:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) {
//Getting the full image path of the image taken
final String imagePath = "file://"+ currentPhotoPath;
webView.post(new Runnable() {
#Override
public void run() {
webView.evaluateJavascript("postImage('" + imagePath + "')", null);
}
});
getLocation();
if (data == null) {
//Display an error
Log.d("performClick", "Error: data == null");
return;
}
}
}
What it does is it calls the javascript function postImage and passes imagePath as a parameter and the function in my hello.js file is:
function postImage(imagePath){
document.body.innerHTML = "<img src=\""+ imagePath + "\">";
}
Now once I take the picture it appears on my WebView as an image without needing to reload the page or loading another URL with only the image.

Related

Loading Image bitmap from user picked image into imageview doesn't set image

A beginner here..
I'm setting a image into Image View that will be picked by user, But when I'm picking image the imageview doesn't get the image loaded.
M app is just opening the file chooser, I'm choosing the image and its just not loading the picked image in imageview.
<ImageView
android:layout_marginRight="5dp"
android:id="#+id/logoImageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center|end"
/>
in java..
ImageView logoimage=findViewById(R.id.logoImageView);
I have this onclick listener on my Imageview..
logoImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent fileChoosingIntent =new Intent(Intent.ACTION_OPEN_DOCUMENT);
fileChoosingIntent.setType("image/*");
startActivityForResult(fileChoosingIntent,10);
}
});
OnActivityResults method
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if(resultCode==10){
logoImage.setImageBitmap(BitmapFactory.decodeFile(data.getData().getPath()));
}
}
androdi studio is giving this warning "Method invocation 'getData' may
produce 'NullPointerException'" on data.getData() method...
I also have tried glide
Glide.with(this).load(data.getData()).into(logoImage);
I'm getting same results, blank imageview.
Rest of code is confidential( Can't a beginner have secret code..? :p)
NOTE- Its not showing anything in logcat
Thank you so much for your suppport... :-)
You've mistaken the codes in the onActivityResult method. The requestCode is the one that identifies your request.
From the Activity source code:
requestCode The integer request code originally supplied to startActivityForResult(), allowing you to identify who this result came from.
resultCode The integer result code returned by the child activity through its setResult().
You should use resultCode to make sure that your request was successful though.
When it comes to the NullPointerException warning, you should always check if used nullable variable isn't in fact null before accessing its fields or methods.
So your startActivityResult() method using Glide should look more like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if(requestCode == 10) {
if (resultCode == RESULT_OK && data != null) {
Glide.with(this).load(data.getData()).into(logoImage);
} else {
// optional null data handling
}
}
}
logoImage.setImageBitmap(BitmapFactory.decodeFile(data.getData().getPath()));
You are not getting a file, so decodeFile() is not going to work.
The best solution, by far, is to add Glide or Picasso to your project, then give the Uri (data.getData()) to the library to load into your ImageView. To be blunt: anyone who doesn't do this needs a very good justification.
The most direct equivalent of your current code is to use getContentResolver().openInputStream() to get an InputStream on the content identified by the Uri. Then, use BitmapFactory.decodeStream() to populate the ImageView. This will do that work on the main application thread (irritating peer developers/managers) and freezing your UI (irritating users). That's why you should just use Glide or Picasso, as they handle the background threading for you, along with lots of other things (e.g., caching).

How to get the path of the last uploaded image in firebase storage with Android?

I've learned how to select an image from the gallery, how to upload it into a firebase storage and also display it in onActivityResult and everything works fine. My problem is, when i restart the activity the image is gone. This is my code:
#Override
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("Photos").child(uri.getLastPathSegment());
String path = storageReference.getPath(); //get the path of the last uploaded image
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);
}
});
}
}
I have created a method named displayLastImage(). When i call this method from onCreate like this:
private void displayLastImage() {
StorageReference newStorageReference = storageReference.child("Photos/car.jpg");
Glide.with(this).using(new FirebaseImageLoader()).load(newStorageReference).into(imageView);
}
works perfect but when i'm calling the method from onCreate using the path instead of "Photos/46" like this:
private void displayLastImage() {
StorageReference newStorageReference = storageReference.child(path);
Glide.with(this).using(new FirebaseImageLoader()).load(newStorageReference).into(imageView);
}
i get this error: java.lang.IllegalArgumentException: childName cannot be null or empty. How do i get the path of the last uploaded image so i can display it correctly?
Thanks in advance!
When you upload an image, you store it at a path that is based on the image the user selected from the gallery.
String path = storageReference.getPath();
When you restart the activity, the path will not have been initialized, so you're trying to look up an image at an unknown path.
This means that you'll need to "remember" the path between the calls to the activity. You could store it in the Shared Preferences of your app. These are persisted between activities.
More common is to store the image path (or its download URL) in cloud storage, such as the Firebase Database. You can see an example of that in the Firebase Codelab for Android.

Barcode app Scan result display on screen

I am trying to make a barcode scanning app. I am stuck at the point where I am able to scan the barcode but now I want to show the barcode image along with decoded barcode number and other details on the screen and then provide a button to proceed to next screen. How should I go about it? I am unable to understand should I call an intent to new activity or the layout view. If I call the new activity, how do I pass the barcode that's decoded and other details to new activity?
Help.
Want something like this after scanning a barcode:
you can get and use that barcode anywhere, as:
uid is a textview where i have added the result from ZXing (Zebra Crossing) library activity.:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
Log.e("test 1",String.valueOf(requestCode));
if (resultCode == RESULT_OK) {
Log.e("test 2",intent.getStringExtra("SCAN_RESULT_FORMAT"));
Log.e("test 3",intent.getStringExtra("SCAN_RESULT"));
Toast.makeText(getApplicationContext(), intent.getStringExtra("SCAN_RESULT"), Toast.LENGTH_LONG).show();
uid.setText(intent.getStringExtra("SCAN_RESULT")) ;
} else if (resultCode == RESULT_CANCELED) {
Log.e("test 4",String.valueOf(requestCode));
}
}
}
Over Image you are seeing in sample image is a generated barcode not a actual picture captured from the camera.
For this you can use iText is a great Java PDF library. They also have an API for creating barcodes. You don't need to be creating a PDF to use it.
BarcodeEAN codeEAN = new BarcodeEAN();
codeEAN.setCodeType(codeEAN.EAN13);
codeEAN.setCode("9780201615883");
Image imageEAN = codeEAN.createImageWithBarcode(cb, null, null);

Android how to process photo

I am working on an android camera-based app with use of Intent. After capturing a photo I can see that photo and two buttons appear - "Save" and "Cancel". What I want to do is to not wait for user to choose one of these two buttons, but start processing this photo and then depending on the result of processing do futher actions.
I've been doing it this way so far :
CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
protected void startCameraActivity()
{
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
// the method below is my method for setting proper path for my image file
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
startActivityForResult( intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE );
}
this method is invoked when I launch my app. So I start my app with camera.
Then I take a photo. And I can choose "Save" or "Cancel". When I choose one this method is invoked :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
onPhotoTaken(); // processing...
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
After receiving proper resultCode I load that image from file and then start processing it.
And now my quesiton is : If I can get that image before onActivityResult method is invoked? It is invoked after clicking on one of these buttons.
( I want to do it the similar way google googles does it - user captures a photo and that photo is being processed right away )
You're going to need to implement your own picture taking activity, something along the lines of this (which includes source code at the end of the page).
It takes some time to set it up straight, compared to using simple Intent, but after that you have direct access to all camera features, including camera image even before it's made available to the calling activity.

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;
}
}

Categories

Resources