How to add two different layouts to Listview? - android

I want to develop an interface like WhatsApp chat.
I implemented the whole interface with ListView. I can able to add text to ListView. But I don't know how to add an image to the same ListView as an attachment.
My main aim is to add text, images, audio files, and video files to single ListView.
Please help me.

Finally I got the answer.
Here is my code for combining the TextView and ImageView in ListView:
adding-more-than-two-different-layouts-to-listview.html

So if understand the question well, you would like to load an image from the SDcard or from the gallery and load into a listview item`s imageview, right ?
Load image from file:
File imgFile = new File("/sdcard/Images/test_image.jpg");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
or if you would like to select from gallery:
public void pickImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) {
if (data == null) {
//Display an error
return;
}
InputStream inputStream = context.getContentResolver().openInputStream(data.getData());
//Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap...
}
}
You can call these methods from the listview onItemClick listener, and return with the Bitmap, when you are finished the loading of the image.

Related

Capturing and displaying the same image

I am working on a semester project and it requires that I send images from an android device to server without storing the image. I want to make sure my cameta application is working properly. I want to check this using an ImageView that displays the captured image on the screen. How can I do it? I thought I could load from drawable but I cannot add files to drawable during runtime.
Thanks for any support
You don't need to save it to the drawable. These are the two steps:
1.Convert the captured image to Bitmap
2.Set the bitmap image to an imageview
Ref: StackOverflow Answer
private void takePicture(){ //you can call this every 5 seconds using a timer or whenever you want
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap picture = (Bitmap) data.getExtras().get("data");//this is your bitmap image and now you can do whatever you want with this
imageView.setImageBitmap(picture); //for example I put bmp in an ImageView
}
}

Use of captured image bitmap outside onActivityResult method

I am taking photo from my activity using camera intent as shown in the below code, but i need to use the captured image bitmap outside the onActivityResult() method but this is not working, i tried to declare the bitmap outside the method and then use outside the method but this did not work, it seems that the bitmap did not catch the image in the onActivityResult, so can anyone help in order to use the bitmap of captured image outside the method or to save it and get its path like using the select image from gallery, so my goal is to use the bitmap of captured image for image processing purpose outside the onActivityResult() method, below is my code, thanks
captureButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
MY_CAMERA_PERMISSION_CODE);
} else {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
});
and this the onActivityResult() method:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageSelected.setImageBitmap(photo);
}
}
I tried this steps and it worked for me
1) define a Bitmap Object above of your onCreate method like bellow
Bitmap photo;
2) instead of Bitmap photo = (Bitmap) data.getExtras().get("data"); in onActivityResult write bellow code
photo = (Bitmap) data.getExtras().get("data");
and after that you can access to your photo object everywhere in your activity you like

How to save Image to sqlite?

These codes can only capture and display image I took, and when I pressed save button, it only saves "Note title" onto my list View. And when I clicked "Note Title" it doesn't display the picture anymore. What should I add to save it and update it? Thanks.
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap mphoto = (Bitmap) data.getExtras().get("data");
imageviewid.setImageBitmap(mphoto);
}
}
public void takeImageFromCamera(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
private void saveNote(){
fillNameIfEmpty();
id = DbAccess.addNote(getBaseContext(), etName.getText().toString(), mFileName, DbContract.NoteEntry.TYPE_PHOTO, currentCat);
Toast.makeText(getApplicationContext(), R.string.toast_saved, Toast.LENGTH_SHORT).show();
}
private void updateNote(){
fillNameIfEmpty();
DbAccess.updateNote(getBaseContext(), id, etName.getText().toString(), mFileName, currentCat);
Toast.makeText(getApplicationContext(), R.string.toast_updated, Toast.LENGTH_SHORT).show();
}
Your DbAccess API doesn't know anything about an image in the note!
So I would extend your code to:
After picture is taken it saved in fail in application sandbox
Save filename of image assigned to note to database together with note
And don't forget to handle editing or deletion picture use cases!

I could select an image from gallery in android. But, i want to select next and previous images of the selected image. How do i do it?

I could select an image from gallery in android. But, i want to select next and previous images of the selected image. How do i do it?
This works fine.. for selecting and then displaying the image..now what do i do for selecting next and previous images?
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
});
#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) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
// Log.d(TAG, String.valueOf(bitmap));
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I could select an image from gallery in android
Your code happens to bring up a gallery app on your device. You are requesting ACTION_GET_CONTENT for image/*. This means:
Other apps, besides a gallery, can respond to that Intent and can be chosen by the user
There will be devices that do not have an app that would be considered a "gallery"
How do i do it?
Write your own gallery. There is no concept of "next" or "previous" with ACTION_GET_CONTENT.
There are many image picker libraries for Android that might form the basis for your own gallery UI.
I forgot to write the most important thing, which is code for previous and next is not working.. till now i have written code only for previous, coz once it works; next ll be just few changes ahead.
moveToprevious is coming false n cursor is declared as global.. so that when a query for cusor s fired in getcontentresolver() is fired, it gets the needed value..
Please look into this!

image put into ImageView from taken picture not always showing up

An image taken from the camera doesn't always show up in my ImageView. It seems like it is random when it wants to show up. Why is it doing this?
Activity that takes picture
private void takePicture() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_PICTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String spokenText;
// Handles picture taking after finished
if (requestCode == REQUEST_PICTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap image = (Bitmap) extras.get("data");
ImageView imageResult = (ImageView) findViewById(R.id.result_image);
imageResult.setImageBitmap(image);
ImageHandler imageHandler = new ImageHandler(this);
imageHandler.writeToFile(image, step.getChecklistId(), step.getOrder());
step.setImageFilename(imageHandler.getFilename(step.getChecklistId(), step.getOrder()));
finishStep();
}
}
XML to show picture
<ImageView
android:id="#+id/result_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:contentDescription="#string/taken_img_desc" />
Are you using notifyDataSetChanged for your adapter?
Is your view being refreshed? Try .invalidate();
One more thing: You might experience that the camera intent does not return an image or an uri to an image. I created a wrapper to help you with that. See the whole discussion on stackoverflow here or find the code on github.

Categories

Resources