Android ImageView content disapearing after showing - android

My application does this: takes a photo, then show the photo in an ImageView. The weird thing is that the photo is displayed for about a second (after taking it with the camera), and then the ImageView is empty again.
This is my code:
publish.xml
<ImageView
android:id="#+id/itemImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
PublishActivity.java
package ar.com.guiagratis;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class PublishActivity extends Activity {
final int TAKE_PICTURE_REQUEST_CODE = 115;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.publish);
}
public void btnNextClick(View v) {
// TODO: disable all buttons
//Intent intent=new Intent(getApplicationContext(), TakePhotoActivity.class);
// startActivityForResult(intent, TAKE_PICTURE_RESULT_CODE);
Toast.makeText(getApplicationContext(), "Sacate una foto viteh", Toast.LENGTH_SHORT).show();
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photoFile = new File(Environment.getExternalStorageDirectory(), "Photo.png");
Uri imageUri = Uri.fromFile(photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PICTURE_REQUEST_CODE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case TAKE_PICTURE_REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
File photoFile = new File(Environment.getExternalStorageDirectory(), "Photo.png");
Uri imageUri = Uri.fromFile(photoFile);
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
imageUri, Toast.LENGTH_LONG).show();
Bitmap myBitmap = BitmapFactory.decodeFile(imageUri.getPath());
BitmapDrawable ob = new BitmapDrawable(myBitmap);
ImageView myImage = (ImageView) findViewById(R.id.itemImage);
myImage.setBackgroundDrawable(ob);
Toast.makeText(getApplicationContext(), "Qué linda foto! ", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Hubo un problema al subir la imágen... ", Toast.LENGTH_SHORT).show();
}
}
}
}
Any help will be appreciated.

You're using setBackgroundDrawable(Drawable drawable) which sets the View's background.
If you want to change the ImageView's content you need to use
setImageDrawable(Drawable drawable)
or
setImageBitmap(Bitmap bm)

Ok, I finally found the problem. I was not aware that I need to use onSaveInstanceState and onRestoreInstanceState to store the values I don't want to loose from an Activity when I'm doing certain things, like initiating the Camera and taking a photo.
So I added this code:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putBoolean("photoUploaded", photoUploaded);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
photoUploaded = savedInstanceState.getBoolean("photoUploaded");
if (photoUploaded) {
File photoFile = new File(Environment.getExternalStorageDirectory(), "Photo.png");
Uri imageUri = Uri.fromFile(photoFile);
Bitmap myBitmap = BitmapFactory.decodeFile(imageUri.getPath());
BitmapDrawable ob = new BitmapDrawable(myBitmap);
ImageView myImage = (ImageView) findViewById(R.id.uploadedImage);
myImage.setBackgroundDrawable(ob);
}
}
and now is working.
Not sure how to do to make this question as solved.

Related

send image as a bit map (By Intent or Sharedpreference)

I want to pass image in one activity to another activity.I tried many time.but my app gonna crash.plz edit my code with necessary changes.
Sender activity:
camera=(ImageButton)findViewById(R.id.camera);
gallery=(ImageButton)findViewById(R.id.gallery);
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i =new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
startActivityForResult(i, 50);
}
});
gallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 40);
}
});
}
Receiver activity:
package com.androidlink.navigation_bottom;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class Image_set_Activity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_set_);
ImageView IV = (ImageView) findViewById(R.id.simpleImageView);
}
}
in onActivityResult
Bitmap photo = (Bitmap) data.getExtras().get("data");
Log.d("===uploadPhoto","camera : "+photo);
*//* Passing BITMAP to the Second Activity *//*
Intent intentCamera = new Intent(this, SecondAvtivity.class);
IntentCamera.putExtra("BitmapImage", photo);
startActivity(intentCamera);
in seconActivity
Bitmap fromCamera = getIntent().getParcelableExtra("BitmapImage");
imageView.setImageBitmap(fromCamera)
Try to Use This Code
Frist Classs
ImageView img=findViewById(R.id.imgId);
BitmapDrawable bmd= (BitmapDrawable) img.getDrawable();
Bitmap bt= bmd.getBitmap();
final byte[] imgarry = bt.getNinePatchChunk();
Intent intent = new Intent (FirstClass.this,SecondClass.class);
Intent.putExtra(“imgs”,imgarry);
startActivity(intent);
Second Class
Intent in = getIntent();
byte[] ary= in.getByteArrayExtra(“imgs”);
Bitmap bm= BitmapFactory.decodeByteArray(ary,0,ary.length);
imageView.setImageBitmap(bm);

How to send image one activity to another activity?

I am new in android.i want to pass image in one activity to another activity.I tried many time.but my app gonna crash.plz edit my code with necessary changes.
Sender activity:
camera=(ImageButton)findViewById(R.id.camera);
gallery=(ImageButton)findViewById(R.id.gallery);
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i =new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
startActivityForResult(i, 50);
}
});
gallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 40);
}
});
}
Receiver activity:
package com.androidlink.navigation_bottom;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class Image_set_Activity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_set_);
ImageView IV = (ImageView) findViewById(R.id.simpleImageView);
}
}
Try to override onActivityResult(). For further reading - https://developer.android.com/training/basics/intents/result
Try this code For sending the image
ImageView imageview = (ImageView) findViewById(R.id.Tab);
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
final byte[] arr = bitmap.getNinePatchChunk();
imageView.setOnClickListener(View v)
{
Intent intent = new Intent (MainActivity.this,bbb.class);
Intent.putExtra(“image”,arr);
startActivity(intent);
}
For getting the image
Intent I = getIntent();
byte[] arr1 = i.getByteArrayExtra(“image”);
Bitmap map = BitmapFactory.decodeByteArray(arr,0,arr.length);
imageView.setImageBitmap(map);

Select and crop Image from gallery,Displayed in a Circular Shape Imageview

I am trying to fetch Image from Gallery. when user chooses a picture from Gallery, they need to ask crop the Image then the cropped image should be displayed in ImageView.
Here's what I tried:
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class Details extends AppCompatActivity {
ImageView i1,i2;
int num =2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
android.support.v7.app.ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.hide();
}
i1 = (ImageView)findViewById(R.id.prof1);
i2 = (ImageView)findViewById(R.id.prof2);
i1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent img_uopload = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
img_uopload.putExtra("crop","true");
img_uopload.putExtra("aspectX",1);
img_uopload.putExtra("aspectY",1);
img_uopload.putExtra("outputX",200);
img_uopload.putExtra("outputY",200);
img_uopload.putExtra("return-data",true);
startActivityForResult(img_uopload,num);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == num && resultCode == RESULT_OK ){
if(data!=null){
Bundle extras = data.getExtras();
if(extras !=null){
Bitmap img = extras.getParcelable("data");
i1.setImageBitmap(img);
}
else{
Toast.makeText(getApplicationContext(),"Not Read",Toast.LENGTH_LONG).show();
}
}
}
}
}
Here the bundle value returns null.
In android manifest, I have included.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
UAC :
when user clicks image button,Gallery Opens
when user chooses image from gallery,He/She should ask to crop the image
Cropped Image should placed in ImageView
Currently I am not able to use any Github libraries.
After choosing an image user needs to crop the image as like this:
This is my screen:
You can use Crop intent check this :
try {
String picUri="your irl";
Intent myCropIntent = new Intent("com.android.camera.action.CROP");
myCropIntent.setDataAndType(picUri, "image/*");
myCropIntent.putExtra("crop", "true");
myCropIntent.putExtra("aspectX", 1);
myCropIntent.putExtra("aspectY", 1);
myCropIntent.putExtra("outputX", 128);
myCropIntent.putExtra("outputY", 128);
myCropIntent.putExtra("return-data", true);
startActivityForResult(myCropIntent, CROP_PIC_REQUEST_CODE);
}
catch (ActivityNotFoundException e) {
String errorMessage = "No Activity found";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
else you can also use this crop library simple-crop-image-lib
Rounded image view use this library RoundedImageView

Android Camera activity stays open

I'm trying to take a picture with my Android app but when I take the image, the camera display doesn't go away. The code I am using is below. I also used the SDK on Google's developer website SDK.
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Camera;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mainButton = (Button)findViewById(R.id.mainBtn);
mainButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
//Intent userCreationIntent = new Intent(v.getContext(), SecondviewActivity.class);
//startActivity(userCreationIntent);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
return false;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView = (ImageView)findViewById(R.id.imageView1);
imageView.setImageBitmap(photo);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
The problem is within your onTouchListener for the Button!
I just ran your code and changed the onTouchListener to a onClickListener for the Button instead and the code is working.
See my revised code here:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mainButton = (Button) findViewById(R.id.mainBtn);
mainButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(photo);
}
}
}
I'm not precisely sure, why you're using the onTouchListener for a Button, but it's not working apparently - really weird behavior actually ;-)
EDIT: Just a small update. I tried to debug the code and if you use the onTouchListener instead of the onClickListener, when clicking on the button, you actually trigger 3 MotionEvents: MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE and MotionEvent.ACTION_UP.
Now the first event MotionEvent.ACTION_DOWN will trigger your intent to show the camera and when you click OK after you've taken a picture, the next MotionEvent MotionEvent.ACTION_MOVE is waiting in line to be triggered and this will send you to the camera activity once again. Now after taking one more picture and clicking OK, you return to your activity and now the last MotionEvent MotionEvent.ACTION_UP is waiting in line and triggers a 3rd call to the camera activity. After the last camera call, you will be able to get back to your activity without problems ;-)
Why the onClickListener doesn't do this, is because it handles a "full" click which could be MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE and MotionEvent.ACTION_UP and so all 3 events is happening in ONE click instead.
Hope this helps ;-)
This is happening because you are not attaching a path with the intent as to tell Android where to store the image. I had the same problem too.
Try the following code: ( I just added 4 lines )
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Camera;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
// Creating the Uri where Camera saves a picture each time
String imagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + System.currentTimeMillis() + "_myCameraImage.jpg";
File imageFile = new File(imagePath);
imageUri = Uri.fromFile(imageFile);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mainButton = (Button)findViewById(R.id.mainBtn);
mainButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
//Intent userCreationIntent = new Intent(v.getContext(), SecondviewActivity.class);
//startActivity(userCreationIntent);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
return false;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView = (ImageView)findViewById(R.id.imageView1);
imageView.setImageBitmap(photo);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Before you copy and paste this code, make sure that you have necessary import statements in your code like import statements for File, Uri etc.
I strongly hope it will work now :)

Android Camera : Picture should not be stored locally but should be saved temporarily in the App

I am new to android and am trying to make an android app where the user can click an image and save it to a database . However i do not want the image to be stored locally in the gallery folder. Or everytime a picture is taken it saves itself in a self made directory on the phone and keeps replacing earlier pics .I donot want all pictures to be stored on the phone .
Below is my current code :
package com.example.camerastart;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class CameraMainActivity extends Activity
{
private static final int CAMERA_PIC_REQUEST = 2500;
private Button cam_button;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_main);
cam_button = (Button) findViewById(R.id.button1);
cam_button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
// TODO Auto-generated method stub
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK)
{
Bitmap image = (Bitmap) data.getExtras().get("data");
ImageView imageview = (ImageView) findViewById(R.id.imageView1);
imageview.setImageBitmap(image);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_camera_main, menu);
return true;
}
}
any help will be appreciated
You can make a folder, and save all the images to this folder.
just remember to add .nomedia file to this folder, and then the gallery will not show those files

Categories

Resources