Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

my taken snap does not appear in image view

I am trying to put my captured snap in the image view .For bigger devices like nexus 7 and sony xperiac ,it is taking the snap ,but the image won't even show up and i can see that RESULT_OK ,by setting a flag bit .so what do i do now ?

package gatesapps.blogspot.com;

import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Random;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

String mCurrentPhotoPath;// for photo path
ImageView mImageView;
static boolean flag=false;

TextView tv;
Button test;
static final int REQUEST_TAKE_PHOTO = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mImageView=(ImageView)findViewById(R.id.mImageView);
    tv=(TextView)findViewById(R.id.tv);
    test=(Button)findViewById(R.id.test);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@SuppressLint("SimpleDateFormat") @SuppressWarnings("unused")
private File createImageFile() throws IOException {
    // Create an image file name
    @SuppressWarnings("deprecation")
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(0, 0, 0));
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

public void snap(View view){



Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
    // Create the File where the photo should go
    File photoFile = null;
    try {
        photoFile = createImageFile();
    } catch (IOException ex) {
        // Error occurred while creating the File

    }
    // Continue only if the File was successfully created
    if (photoFile != null) {
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(photoFile));
        startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
    }
}



}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        flag=true;
        test.setVisibility(View.VISIBLE);
        setPic();

    }   

} 
private void setPic() {
    // Get the dimensions of the View
    int targetW = mImageView.getWidth();
    int targetH = mImageView.getHeight();

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    mImageView.setImageBitmap(bitmap);
}

Ans my xml is

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity" >

<ImageView
    android:id="@+id/mImageView"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:src="@drawable/back"
    android:contentDescription="preview of the pic"
    android:scaleType="fitCenter" 
    android:layout_gravity="center_horizontal"/>

 <Button 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Snap the picture"
   android:textColor="#3399FF"   
   android:onClick="snap"
   />
 <TextView 
   android:layout_width="match_parent"
   android:layout_height="75dp"
   android:text="Click snap the picture to take a pic"
   android:id="@+id/tv"
   android:textAlignment="center"
   android:textColor="#FF0000"
   android:textSize="25dp"
   />
 <Button 
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="Test"
   android:id="@+id/test"
   android:onClick="test"
   />
 </LinearLayout>
 </ScrollView>
like image 765
Vamsi Pavan Mahesh Avatar asked Nov 10 '22 13:11

Vamsi Pavan Mahesh


2 Answers

Replace  mCurrentPhotoPath = "file:" +image.getAbsolutePath(); 
with    mCurrentPhotoPath =  image.getAbsolutePath();
like image 154
Palak Avatar answered Nov 14 '22 22:11

Palak


but still the 1/4 th of the image on the image view is existing(previously i am having an image in the imageview,i.e,before taking the pic)

How do you define the ImageView with id R.id.mImageView? Make sure you don't have the background attribute set:

<ImageView
    android:id="@+id/mImageView"
    ....
    ....
    android:background="@drawable/originalDrawable" />

Instead, set the src attribute:

android:src="@drawable/originalDrawable"

Reason: The background fills the ImageView. scaleType works with the src drawable. This is probably why you are seeing 1/4 of the old image. Oh, and, setImageBitmap(bitmap) sets the src.

I don't know why the pictures are not getting shown for big phones. I'll edit my answer if I figure that out.

like image 28
user3264740 Avatar answered Nov 14 '22 23:11

user3264740