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.
Related
Hi How to write text in photos? I searched in google but I couldn't find where to search and find. Please provide me tutorial or guide for this solution. Taking a picture from gallery or from drawable and writing text on photos.
So far, I selected an image from gallery and displayed in imageview. But I couldn't find how to write text on that photo. Please guide me to provide solution.
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);
}
}
Thanks.
You can obtain Canvas from the Bitmap and draw directly on it.
Suppose you got image path, decoded it into the Bitmap object, then you can do following
Bimtap bitmap = loadBitmap(path);
Canvas c = new Canvas(bitmap);
c.drawText(text, x, y, paint)
Here's more detailed code
Draw text on bitmap
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.
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);
}
}
I am trying to use Android's built-in gallery. I am able to get the gallery and the albums, but whenever I want to display the image, the gallery straightaway directs me back to my app. I am unable to view the image despite it has been called.
This is my code:
public class CameraTab extends Activity implements OnClickListener{
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_tab);
ImageButton cameraBtn = (ImageButton)findViewById(R.id.camera_btn);
cameraBtn.setOnClickListener(this);
ImageButton galleryBtn = (ImageButton)findViewById(R.id.gallery_btn);
galleryBtn.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == this.findViewById(R.id.camera_btn)){
/// some codes here
}
if (v == this.findViewById(R.id.gallery_btn)){
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);
}
}
}
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);
}
}
Can anyone please help me? Any help would be appreciated!! Thanks!!
The problem is a misunderstanding of Intent.ACTION_GET_CONTENT intent. It's intented to be used to choose a content (in this case image/*) from the archive.
If you want to show an image, just create a new activity with an ImageView in its layout. Pass the image URI using setData.
I think the Action you want to use is Intent.ACTION_VIEW.
try this
final Intent intent = new Intent(Intent.ACTION_VIEW);
final Uri uri = <The URI to your file>;
// Uri either from file eg.
final Uri uri = Uri.fromFile(yourImageFileOnSDCard);
// or from media store like your method getPath() does but with the URI
// from http://developer.android.com/reference/android/provider/MediaStore.Images.Media.html#EXTERNAL_CONTENT_URI
intent.setDataAndType(uri, "image/*");
startActivity(intent);
The string "image/* is the mime type to be used.
Now the gallery is opened with the given picture selected. To return to your app the user has to press the back button, as usual ;)
private Context context;
public void onCreate(Bundle savedInstanceState) {
...
context = this;
}
I used a scaled Bitmap below because on some devices the images in the Gallery might be too large for being displayed on an ImageView (I had this problem before).
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Bitmap b = new BitmapDrawable(context.getResources(),
selectedImagePath).getBitmap();
int i = (int) (b.getHeight() * (512.0 / b.getWidth()));
bitmap = Bitmap.createScaledBitmap(b, 512, i, true);
// To display the image, you need to set it to an ImageView here
ImageView img = (ImageView) findViewById(R.id.myImageView);
img.setImageBitmap(bitmap);
}
}
}
I want to delete/remove or clear image from imageView every time when user again click to set another image to the imageView. i am getting OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=7239KB, Allocated=2769KB, Bitmap Size=8748KB)
here is my code:
ImageView imageView;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
Bitmap yourSelectedImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
protected void onResume() {
super.onResume();
imageView = (ImageView) findViewById(R.id.imageView);
((ImageView) findViewById(R.id.imageView))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
goToGallery();
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
/*Toast.makeText(getBaseContext(), "" + selectedImagePath, 1000)
.show();
*///editText2.setText(selectedImagePath);
// Convert file path into bitmap image using below line.
yourSelectedImage = BitmapFactory
.decodeFile(selectedImagePath);
// put bitmapimage in your imageview
imageView.setImageBitmap(yourSelectedImage);
}
}
}
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);
}
private void goToGallery()
{
// in onCreate or any event where your want the user to
// select a file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
}
Setting a resource to 0 did not work for me. The following did work though:
imageView.setImageBitmap(null);
use the special resource Id '0'. It is not a valid res id, hence the image is blanked.
image.setImageDrawable(null);
will clear the image in the imageview.
When you set another image to the current ImageView the previous one is no longer used for the ImageView. Hence, no need to do extra work.
viewToUse.setImageResource(android.R.color.transparent);
I think using setImageResource with a color identifier will give you crashing issues on Android 2.2.1, make sure to test it.
edit_countflag.setBackgroundColor(0);