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;
}
}
Related
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.
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
In one of my android projects I'm facing the following issue on a Samsung Tablet with Android 5.0.2: From a Fragment, the user can start to choose a file:
private static final int REQUEST_CHOOSE_FILE = 1;
...
private void chooseFile() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
this.startActivityForResult(intent, REQUEST_CHOOSE_FILE);
}
The activity result is handled via
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CHOOSE_FILE && resultCode == Activity.RESULT_OK && data != null) {
Uri uri = data.getData();
String path = Util.getPath(this.getBaseActivity().getApplicationContext(), uri);
...
}
}
with Util.getPath(...) taken from Get real path from URI, Android KitKat new storage access framework
This works fine for nearly everything - but not for files taken from the "Audio" link in the left part of the filechooser. In this case I'm not able to determine the file extension with my helper function. Everything I get is the name of the file...
I'm a bit stuck, especially because the filechooser isn't showing the extension, too.
Does anyone know this issue and could give me a hint to work around it?
This works fine for nearly everything
No, it does not. It works for a small subset of providers, and fewer with each passing month.
Does anyone know this issue
The issue is that you are relying upon intrinsically unreliable code.
could give me a hint to work around it?
If you want to use the content associated with a Uri, use a ContentResolver. In particular, if you want to know the MIME type of the content, use getType() on ContentResolver. From there, you can attempt to come up with a relevant file extension, such as through MimeTypeMap.
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.
The Android platform have a number of "ready easy use" dialog such as
ProgressDialog, DatePickerDialog and TimePickerDialog, these are fire and wait
boxes, that is, they handle UI, right data and return something.
Is there a similar dialogbox for the mediastorage ?
I want something like "AudioFilePickerDialog" which shows a UI to the user
where the user pick a audio file and return the path/uri to the audio file.
Do I need to build this dialog box up myself or does it exists somewhere ?
One of the few examples I have found is
Given an Android music playlist name, how can one find the songs in the playlist?
but this handles playlists.
/Stefan
I found a tutorial for something like a FileChooser here. You should be able to make it only show Music-Files (like .mp3).
Also, to browser the SDcard of your Android Device, you can use the standard Java File-class, like in normal Java.
Try this
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, REQUEST_MEDIA);//REQUEST_MEDIA is some const int to operate with in onActivityResult
here you'll be brought a dialog (activity tbh) to choose an audio from mediastore.
Handle the result in onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_MEDIA && resultCode == RESULT_OK) {
String audioID = data.getDataString();
...
}
}