I have made an app uploading pictures to server.
I succeed in just accessing to my basic gallery app after clicking the button
but after going to my basic gallery app , I want to choose my pictures(more than one) and make my own button to control(uploading or sending) them.
In android instagram, it controls like it!
But when accessing gallery app, this activity is not made by me, so I don't know how to control to get and send data.
What I done (helped by stack overflow) so far is below:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Intent.createChooser"), SELECT_PICTURE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Toast.makeText(this, "onActivityResult 실행", Toast.LENGTH_LONG).show();
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
But this code works, after accessing gallery app, when I select pictures, app is gone!
create an array of string String[] imagUrls=new String[n] to add image urls every time and insted of img.setImageURI(selectedImageUri); do like imageUrls[0]=selectedImageUri,imageUrls[0]=selectedImageUri ... and finally upload all images from the array.
Related
Fetching images one by one to android app then saving count of pictures on textview. Here if same image fetched again in to android app then don't want to increase the count of textview, how to handle this?
public class GetImageActivity extends Activity {
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView img;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView)findViewById(R.id.ImageView01);
((Button) findViewById(R.id.Button01))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
You must be getting a URI of the image. Maintain a HashSet of the URIs. HashSet will remove the duplicates for you.
In the textview you can just show the size of the HashSet, that will always show the unique number of images.
PS: md5 is also a solution, but TOO TOO slow for your use case. In case you have the same image coming with different URIs (same images placed at different places on the storage), you may have to go for md5 if you want to make that unique.
you can do it by using md5 checksum
if two images are identical then their checksum value will also be identical.
I am developing an android application for the first time and I was wondering how can I have a option of having the user upload an image. Like for example, in a contact manager the user has the option of uploading an image of a contact. I was wondering how can I do that in my android application. Any help would be appreciated.
You can start from scratch..
1)Take a look at INTENT
Android Intent
Then take a look at this blogpost
Image Upload
Hope this helps you
In this one you want to click the image view to get image from sd card.. If you want to change to button then replace the listener from image to button.
int RESULT_LOAD_IMAGE = 1;
image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
getting image from sd card:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
I have this code to pick the images from gallery or camera:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SELECT_PICTURE_ACTIVITY_REQUEST_CODE:
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media.DISPLAY_NAME };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
ext = filePath
.substring(filePath.lastIndexOf(".") + 1);
photod = BitmapFactory.decodeFile(filePath);
new AsyncTaskOne().execute(new String[] {});
(fileNameIndex);
}
}
break;
case SELECT_CAMERA_ACTIVITY_REQUEST_CODE:
if (requestCode == CAMERA_REQUEST) {
photod = (Bitmap) data.getExtras().get("data");
new AsyncTaskOne().execute(new String[] {});
}
}
}
When I press the confirm button I invoke this listener
OnClickListener confirm = new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent("com.striget.eu.UpdatePhoto");
i.putExtra(PropertiesUpdatedPhoto.EXTRA_PHOTO, codedPhoto);
i.putExtra(PropertiesUpdatedPhoto.EXTRA_EXT, ext);
startActivity(i);
}
};
(where codedPhoto is the image coded in base64 by another method)
to send the image and its extension in another activity
Everything works fine with small images but if I chose a medium size image or large photo(also if isn't very large), the app freezes, the screen becomes black and If I wait some minutes return on the current activity without showing any error in the stack and the invoked intent PropertiesUpdatedPhoto doesn't start.
How I could fix this problem?
This problem is discuss here, we should keep intent extra content as small as possible.
My suggestion is instead of passing image, perhaps passing the image URI or Path would be a better solution. Then only load the image in that Activity.
Example :
OnClickListener confirm = new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent("com.striget.eu.UpdatePhoto");
i.setData(PropertiesUpdatedPhoto.EXTRA_PHOTO, # Image URI Here # );
// Or i.putExtra(PropertiesUpdatePhoto.EXTRA_PHOTO, # Image Path Here #);
i.putExtra(PropertiesUpdatedPhoto.EXTRA_EXT, ext);
startActivity(i);
}
};
how to pick images from gallery into an android application?
I need to browse images form my gallery and choose them for my app .how it can be done ?
You'll need to use an intent to the gallery (well, "a gallery").
You can find a code example here (I haven't done it but it looks like it will work):
http://www.androidsnippets.com/get-file-path-of-gallery-image
Use this Code:
fromGalleryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//takePhotoFromGallery = true;// edited
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select Picture"),10);
}
});
And then add this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10 && resultCode == Activity.RESULT_OK) {
Uri contentUri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
imagePath = cursor.getString(column_index);
tempBitmap = BitmapFactory.decodeFile(imagePath); // this is your image
}
}
In Kotlin you can do this.
In onCreate method
binding.updatephoto.setOnClickListener {
contract.launch("image/*")
}
After onCreate
private val contract = registerForActivityResult(ActivityResultContracts.GetContent()){
it?.let {
val imageUri = it
}
}
I'm launching camera from my application. I'm using Android.provider.MediaStore.ACTION_IMAGE_CAPTURE with extra android.provider.MediaStore.EXTRA_OUTPUT. If I use EXTRA_OUTPUT or not, photo is added to gallery.
How to get the Uri of the image in Gallery? I haven't found a clean way to do this, and I don't want to use the Uri of last added image to Gallery, because I can not be sure that it is the photo user took.
Edit:
Let me add here, that I do know how to get the image. Currently I'm using my Uri which I'm passing with Intent as an EXTRA_OUTPUT. That is not the problem. The problem is that using this method I get two photos written to sdcard, and I prefer that only one is written. Or in case of two is written, one in gallery and one in my own Uri, I would like to use one of those Uri:s and delete the other one. In both of the cases, I need the Uri of the photo, which is saved to Gallery by default.
If you start the camera by calling
startActivityForResult(cameraIntent, REQUEST_CODE_CAPTURE_IMAGE_ACTIVITY); // start the camera
in onActivityResult you can capture the mediaURI
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE_CAPTURE_IMAGE_ACTIVITY
&& resultCode == RESULT_OK) {
Uri imageUri = null;
if (data != null){
imageUri = data.getData();
}
}
}
REQUEST_CODE_CAPTURE_IMAGE_ACTIVITY is a static integer value that's used to identify the request that was made through the intent. This is so if you have multiple startActivityForResults you can identify which one is coming back.
Note that if you pre-insert a URI through EXTRA_OUTPUT you will NOT get the URI back through data.getData(); You will have to store the pre-inserted URI in a variable and retrieve it in your onActivityResult while checking to see if imageUri is null..
Ex.
Uri imageUri = null;
if (data != null){
imageUri = data.getData();
}
if (imageUri == null && preinsertedUri != null)
imageUri = preinsertedUri;
// do stuff with imageUri
Getting a Uri from data.getData() in onActivityResult() was not a solution. This worked on one out of three test devices, and failed also on Motorola Defy emulator.
So here is what I ended up doing:
Call camera with Android.provider.MediaStore.ACTION_IMAGE_CAPTURE, no EXTRA_OUTPUT
Get the Uri of image that is stored in Gallery
Read Uri from result
If previous one fails, get the Uri of last added image from MediaStore, using code from here:
http://kevinpotgieter.wordpress.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/
This seems to be working nicely with test devices, but I'm still not completely happy with this solution because I'm not totally ensured that the image added lastly is always the image user captured.
public class SampleImageActivity extends Activity {
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView img;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView)findViewById(R.id.ImageView01);
((Button) findViewById(R.id.Button01))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}