onActivityResult is not displaying image - android

So, I want to take a picture and display it on the screen. I can press the button and take a picture but it isn't showing it on the ImageView. I can't find the solution with searching onActivityresult.
What I have now:
public class MainActivity extends AppCompatActivity {
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnCamera = findViewById(R.id.btnCamera);
imageView = findViewById(R.id.imageView);
// Open camera and take picture
btnCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = (Bitmap)data.getExtras().get("Data");
imageView.setImageBitmap(bitmap);
}
}

Use this:
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
Instead of:
Bitmap bitmap = (Bitmap)data.getExtras().get("Data");

try this
if (requestCode == 0 && resultCode == RESULT_OK )
{
Uri imgUri = data.getData();
try {
Bitmap bm = MediaStore.Images.Media.getBitmap(getContentResolver(), imgUri);
imageView.setImageBitmap(bm);}}

Related

sd card photos not displayed when picking them from Image gallery , Android studio,

I am using this code to pick an image from gallery and display it in an imageView, the problem that this is working just for photos in internal storage, and when I pick an image from my sd card it's not displayed in my imageView
I hope you have a solution.
public class MainActivity extends AppCompatActivity {
ImageView img ;
Button btn ;
Uri imageUri ;
private static final int PICK_IMAGE = 1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView) ;
btn = findViewById(R.id.button) ;
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
openGallery() ;
}
});
}
public void openGallery(){
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) ;
startActivityForResult(intent,PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode==PICK_IMAGE && data != null){
imageUri = data.getData() ;
img.setImageURI(imageUri);
}else{
Toast.makeText(this," you haven't selected",Toast.LENGTH_LONG).show();
}
}
}
I am using those permissions in my manifest file :
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE">
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
I asked and I will answer, the problem was that most of my sd card photos are 2.0 MB or more and they can't be treated directly by the imageView so I added some lines in onActivityResult()
public class MainActivity extends AppCompatActivity {
ImageView img ;
Button btn ;
Uri imageUri ;
private static final int PICK_IMAGE = 1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView) ;
btn = findViewById(R.id.button) ;
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
openGallery() ;
}
});
}
public void openGallery(){
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI) ;
startActivityForResult(intent,PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode==PICK_IMAGE && data != null){
imageUri = data.getData() ;
Bitmap bitmap ;
try {
bitmap=MediaStore.Images.Media
.getBitmap(this.getContentResolver(),imgUri);
Bitmap mBitmap = Bitmap.createScaledBitmap(bitmap, 160, 160, true);
img.setImageBitmap(mBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}else{
Toast.makeText(this," you haven't selected",Toast.LENGTH_LONG).show();
}
}
I hope this will be helpful for those who can face the same problem

Not set image from camera to imageview in android [duplicate]

This question already has answers here:
Capture Image from Camera and Display in Activity
(19 answers)
Closed 5 years ago.
I am trying to display a image captured from camera inside imageview in android
Selecting a photo from the correct gallery works
But the camera fails: "app has stopped"
images.get(2).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
current_image = 2;
if (!fill_images[current_image]) {
show_dialog();
}else {
show_delete_dialog();
};
}
});
public void show_dialog() {
final ArrayList<String> list = new ArrayList<String>();
list.add("select from gallery");
list.add("take a photo");
AlertDialog.Builder builder = new AlertDialog.Builder(NewAdActivity.this);
builder.setAdapter(new ArrayAdapter<String>(NewAdActivity.this, R.layout.row, R.id.mytext, list)
, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if (i == 0) {//gallery Intent gallery_intent=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI) ; startActivityForResult(Intent.createChooser(gallery_intent,"select image"),2);
}
else if (i == 1){//camera
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file=new File(Environment.getExternalStorageDirectory(),"file"+String.valueOf(System.currentTimeMillis()+".jpg"));
uri= Uri.fromFile(file);
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
camera_intent.putExtra("return-data",true);
startActivityForResult(camera_intent,1);
}
}
});
builder.show();
}
At first make sure that you declared all necessary permission on manifest.xml file. Then try with it -
public class MainActivity extends AppCompatActivity {
private static final int SELECTED_PICTURE=1;
private ImageView iv;
private Button btnClick;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
iv=(ImageView)findViewById(R.id.imageView1);
btnClick = (Button) findViewById(R.id.btnClick);
btnClick.setOnClickListener(new View.OnCLickListener(){
public void onClick(View view) {
btnClick(view);
}
});
}
public void btnClick(View v){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, SELECTED_PICTURE);
}
}
#Override
protected 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");
iv.setImageBitmap(photo);
}
}
}
And dont forget to add permission to manifest.xml -
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
new Camera.PictureCallback() {
#Override
public void onPictureTaken(final byte[] data, Camera camera) {
bytes = data;
}
}
Then convert the bytes to bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Then set imageview
imageView.setImageBitmap(bitmap);

Picture from camera not showing up in second activity

I feel sorry to come and ask people for help, but I've been stuck with this problem for a while and it's driving me mad.
I'm trying to take a button from MainActivity where it puts you into the camera and takes a picture. It then takes you to the next Activity where there is a preview of the very same image. But whenever I arrive to the next activity the image doesn't show up, just a placeholder. What am I doing wrong? Am I missing something in the code?
I've been looking around and tried to use Uri, but apparently it's not permitted anymore as you need to give specific permissions for the action, so FileProvider is recommended, but honestly It's a little bit too hard for a beginner like me. I tried and failed for hours.
MainActivity
public class MainActivity extends AppCompatActivity {
int CAPTURE_REQUEST;
Button button;
Bitmap thumbnail;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAPTURE_REQUEST);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == CAPTURE_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
sendImage();
}
}
}
private void sendImage() {
Intent intent = new Intent(MainActivity.this, NextActivity.class);
intent.putExtra("image", thumbnail);
startActivity(intent);
}}
NextActivity
public class NextActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
ImageView imageView = (ImageView) findViewById(R.id.imageView);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
Bundle extras = getIntent().getExtras();
if (extras != null) {
Bitmap image = (Bitmap) extras.get("image");
if (image != null) {
imageView.setImageBitmap(image);
}
}
}}
Change onActivityResult to
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == CAPTURE_REQUEST) {
thumbnail = (Bitmap) data.getExtras().get("data");
sendImage();
}
}
You are overriding your global variable with the local declaration.

How to save captured image in savedinstance so that I can retreive back after orientation

Below code captures image using camera and displays in activity, when orientation changes, I lose image captured, how can i retrieve image back after orientation, how to save the image in savedinstance
public class MainActivity extends AppCompatActivity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
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 && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
//Bitmap photo = ImageUtils.getInstant().getCompressedBitmap("data");
imageView.setImageBitmap(photo);
}
}
}
Either you can retrieve the path of the picture from phone or simply save the bitmap in file system before orientation change. Store the path in bundle and then in onConfigurationChanged(Configuration newConfig) set the image to image view.
Hope this helps.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(photo!=null)
imageView.setImageBitmap(photo);
}
Bitmap photo;// move to member variable
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
// photo is member variable
photo = (Bitmap) data.getExtras().get("data");
//Bitmap photo = ImageUtils.getInstant().getCompressedBitmap("data");
imageView.setImageBitmap(photo);
}
}

onActivityResult not getting triggered in Android

I am making an android application, which takes an image from camera and then displays it. However, I am unable to display the clicked image probably because the onActivityResult() is not triggered.
Here is my piece of code. Can anyone suggest me what am i missing ?
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final int CAMERA_PIC_REQUEST = 1337;
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
#override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("Message1", "I reached 2");
//super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST) {
// do something
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(thumbnail);
}
}
});
}
}
onActivityResult() must be declared in your Activity class (not inside the onClickListener). If you correct the "#override" ('o' must be capitalized), typo before your current onActivityResult() declaration, you'll see what I mean...
See the Activity.onActivityResult() documentation.
Here's how your class should look like:
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final int CAMERA_PIC_REQUEST = 1337;
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("Message1", "I reached 2");
//super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST) {
// do something
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(thumbnail);
}
}
}

Categories

Resources