I am newbie for android and trying to include a file uploader in android application. But I can't find in any relevant control for that. I want to place a control like this Image
What should I do for achieving this task?
There is no inbuilt control for uploading a file in Android. In java we are having a file-chooser to browse the file. in android you have to do this to select a file either from camera or gallery.
final String[] items = new String[] { "Take picture",
"Choose from Gallery" };
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.select_dialog_item, items);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Image");
builder.setIcon(R.drawable.copy_selected);
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) { // pick from
// camera
if (item == 0) {
imageFilepath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/tmp_img.jpeg";
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri = Uri.fromFile(new File(Environment
.getExternalStorageDirectory().getAbsolutePath(),
"/tmp_img.jpeg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
mImageCaptureUri);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
} else { // pick from file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Complete action using"), PICK_FROM_FILE);
}
}
});
final AlertDialog dialog = builder.create();
Now on onActivityResult you need to handle this according to your need.Thanks
Related
How can i upload images from camera to my web server ,
I was able to upload using image chooser like in this tuto
https://www.simplifiedcoding.net/android-upload-image-to-server/
, but i would like to choose images from camera and galery instead
public void uploadMultipart() {
//getting the actual path of the image
String path = getPath(filePath);
//Uploading code
try {
String uploadId = UUID.randomUUID().toString();
//Creating a multi part request
new MultipartUploadRequest(this, uploadId, Constants.UPLOAD_URL)
.addFileToUpload(path, "image") //Adding file
.addParameter("name", name) //Adding text parameter to the request
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(2)
.startUpload(); //Starting the upload
} catch (Exception exc) {
Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show();
}
}
//method to show file chooser
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
//handling the image chooser activity result
#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) {
filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I also found this method , but it didn't work for me
How can replace the file chooser method in the above code properly to get the image path and name , and then upload it as in the tuto
private void selectImage() {
final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(UploadImageTest.this);
builder.setTitle("Add Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo"))
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);
}
else if (options[item].equals("Choose from Gallery"))
{
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
}
else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
For Uploading a captured image to the server the whole your code remains the same you just need to pass your captured image file path into your uploadMultipart function.
You will get captured image file path in your onActivityResult.
For android studio, I am doing a module where I have to take an image as well as all type files and store into our server. (i.e my sql I am using common backend.) Through Spring MVC, I am doing this project. I am not able to save the file location to mysql.
We have to save only the uploaded location, but that location is our server.
I tried to take the image from mobile, but after taking the image it is not saving. I also granted permission to externalsd.
my code is:
//image selection
private void selectImage() {
Dialog dialog = new Dialog(getActivity());
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose Image Source");
builder.setItems(new CharSequence[] { "Gallery", "Camera" },
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
switch (which) {
case 0:
Intent intent = new Intent(
Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
Intent chooser = Intent
.createChooser(
intent,
"Choose a Picture");
startActivityForResult(
chooser,
ACTION_REQUEST_GALLERY);
break;
case 1:
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(
cameraIntent,
ACTION_REQUEST_CAMERA);
break;
default:
break;
}
}
});
builder.show();
dialog.dismiss();
}
This is my code. I want to select an image from either camera or gallery.
For this i have to check the camera support feature of a device.
public void selectImage() {
final String[] items = new String[]{"Camera", "Gallery"};
final Integer[] icons = new Integer[]{R.drawable.ic_camera, R.drawable.ic_gallery};
ListAdapter adapter = new ArrayAdapterWithIcon(getActivity(), items, icons);
new AlertDialog.Builder(getActivity()).setTitle("Select Picture")
.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Camera")) {
if (//here i want to check is there mobile exists camera or not) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
}
} else if (items[item].equals("Gallery")) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
}
}).show();
}
I have tried a lot but no success.
import android.hardware.Camera;`
Use Camera.getNumberOfCameras();. If it returns value greater than 0, it means your device has a camera.
Use this to show gallery to pick image from:
private void showGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, 1234);
}
Use this to open camera (checks if device has camera or not):
private void showCamera() {
// get the package manager and check if device has camera
PackageManager pm = getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// ensure that there is some activity to handle camera intents
if (takePictureIntent.resolveActivity(pm) != null) {
startActivityForResult(takePictureIntent, 5678);
}
} else {
Snackbar.make(mView, "your device dont have camera", Snackbar.LENGTH_LONG).show();
}
}
NOTE: Make sure you declare permission for Camera in the manifest as:
<manifest ... >
<uses-feature android:name="android.hardware.camera"
android:required="true" />
...
</manifest>
For more details, check this out.
I have the following requirement:
From the mobile application , I want to open the default gallery of the Android tablet / device , which will contain both the image and video files and select them.
Is there any option to view both image and video files on Android Gallery?
Kindly provide your suggestions.
I think, There is no such an option to open both images videos at a time..... But we can filter it before open like this
final CharSequence[] options = {"Images", "Videos", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(OpenGallery.this);
builder.setTitle("Select From...");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Images")) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, 1);
} else if (options[item].equals("Videos")) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, 1);
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
dialog.dismiss();
}
});
builder.show();
just try this..
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(
"content://media/internal/images/media"));
startActivity(intent);
Hope it helps ..!
try this
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("images/*,video/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
Hi I found the following code snippet from another question of stackoverflow to open Camera and Gallery Intent chooser combined. Here's the code.
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT,null);
galleryIntent.setType("image/*");
galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("Code", OPEN_CAMERA); <-- Not working
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INTENT, galleryIntent);
chooser.putExtra(Intent.EXTRA_TITLE, "Snap Option");
chooser.putExtra("Code", OPEN_GALLERY); <-- Not working
Intent[] intentArray = {cameraIntent};
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooser,1);
What I want to do is check which intent chooser was clicked when I will be in the onActivityResult . But as mentioned in the code I tried to putExtra params to differentiate and in the onActivityResult I am doing the following to get the code.
int Code = intent.getExtras().getInt("Code");
But this gives me a NullPointerException . How can I do this please help ?
You have to used something for pick the image from..
Like alert box,dialog,custom dialog and many more.i am just guide you how to used alertdialog.
final CharSequence[] items = { "Take Photo", "Choose from Library","Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
//you can take photo from camera
/*Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, REQUEST_CAMERA);*/
} else if (items[item].equals("Choose from Library")) {
//you can pick photo from library/gallery
/* Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE);*/
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();