Question

Create own camera as I'need Focus ON for taking picture. Camera works perfectly fine as expected. Passing URI between activities.

I've ImageView in Next Screen which i used to set the Image with

imgView.setImageURI(absPathUri);

Checked that absPathUri is not null. Still ImagView is blank with no pictures. Doing the same in debug mode brings the Picture in ImageView.

Can anybody please tell where I'm doing wrong?

Also send broadcast to re-scan the device but still no success with following snippet.

 Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
 File f = new File(absPathUri.getPath());
 Uri contentUri = Uri.fromFile(f);
 mediaScanIntent.setData(contentUri);
 this.sendBroadcast(mediaScanIntent);

Uses Below snippet to generate absPathUri

 if (Environment.MEDIA_MOUNTED.equals(state)) 
    {
        imageDirectory = new File(AppConstants.IMAGE_DIRECTORY_PATH);
    }
    else
    {
        imageDirectory = new File(AppConstants.IMAGE_DIRECTORY_PATH_DATA);
    }

    imageDirectory.mkdirs();
    File tempFile = new File(imageDirectory, getVideoName()+ jpg);
    Uri outputFileUri = Uri.fromFile( tempFile );
    absPathUri = outputFileUri;


  public static String getVideoName()
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    try {
        name = sdf.format(new Date(System.currentTimeMillis()));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return name;
}

Below is the Layout which i used to show the ImageView in xml.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg"
 >

<ImageView
    android:id="@+id/picView"
    android:layout_above="@+id/buttonPanel"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="center"
    android:src="@drawable/accounts_glyph_password_default"
    />
<LinearLayout
    android:id="@id/buttonPanel"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal"
     >

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onDiscard"
         />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:text="@string/save"
        android:onClick="onSave"
        android:textColor="#ffffff" />
</LinearLayout>
</RelativeLayout>
Was it helpful?

Solution

As suggested by gnuanu, setImageURI() is not better to use as reading and decoding on the UI thread, which can cause a latency hiccup.

Better to use the following:-

setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.

Still these methods didn't solve my problem. As I was taking Picture with Camera and onClick of it sending to Next Activity which may cause latency hiccups.

So better to pass to another activity after some peroid of time . . just a sec is totally convient to get through it.

Snippet which solve my issue:-

final Intent picIntent = new Intent(CameraActivity.this,PicSaveActivity.class);
picIntent.putExtra("URI", pathUri.toString());
Handler handler = new Handler() 
{
public void handleMessage(Message msg) {
    startActivityForResult(picIntent, PICTAKEN);
    };
};
 Message msg = handler.obtainMessage();
 handler.sendMessageDelayed(msg, 1000);

In Next Activity, catch the URI and rotate the Image as it would be in landscape mode.

 if(absPathUri!=null)
{
    Bitmap myImg = BitmapFactory.decodeFile(absPathUri.getPath());
    Matrix matrix = new Matrix();
    matrix.postRotate(90);
    Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(),
            matrix, true);
    imgView.setImageBitmap(rotated);
    }

OTHER TIPS

See here: ImageView setImageBitmap not working on certain devices I found that in my case, I needed to call invalidate after setImageBitmap:

imgView.setImageBitmap(bitmap);
imgView.invalidate();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top