Reputation: 10877
I've have develop my own camera app for mandatory configuration, when i try to show the captured image in next activity which displays whether to save it or not?
I'm not able to fetch the image which i captured and displays on my ImageView. I'm absolutely getting absPathUri
with proper path.
Code snippets:-
imgView = (ImageView)findViewById(R.id.picView);
Bundle b= getIntent().getExtras();
absPathUri = Uri.parse(b.getString("URI"));
Toast.makeText(getApplicationContext(), ""+absPathUri, Toast.LENGTH_SHORT).show();
if(absPathUri!=null)
{
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE));
imgView.setImageURI(absPathUri);
}
On Further dive in the reason why I'm unable to set the ImageView, throws the Null Pointer Exception
which is due to File Not Found
. If applicatoin is in debug mode it displays the proper Image on ImageView. It seems that mediaStore get Refreshed till the debugging hits.
File imgFile = new File(getPath(absPathUri));
Toast.makeText(getApplicationContext(), "ImageFile Exists"+imgFile.exists(), Toast.LENGTH_SHORT).show();
if(imgFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imgView.setImageBitmap(myBitmap);
}
public String getPath(Uri photoUri) {
String filePath = "";
if (photoUri != null) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
try
{
Cursor cursor = getContentResolver().query(photoUri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
}catch(Exception e)
{
Toast.makeText(getApplicationContext(), ""+e, Toast.LENGTH_SHORT).show();
}
}
return filePath;
}
Tried Solution:-
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE));
Can anybody guide me where I'm getting wrong in displaying Image on ImageView?
Upvotes: 1
Views: 3297
Reputation: 10877
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);
}
Upvotes: 1
Reputation: 57173
The reference clearly states: The path to the file is contained in the Intent.mData
field.
Therefore, you need
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, absPathUri));
You probably also need to let the broadcast complete before you get the results; using
imgView.post(new Runnable() { public void run() { imgView.setImageURI(absPathUri); } }
should suffice.
Upvotes: 0
Reputation: 5308
I don't really know what's wrong in your code, and my answer uses a completely different approach to taking a photo with the camera and displaying it in an ImageView, but it's a working solution that might help you.
In your first Activity (call it CameraActivity), you can do the following:
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class CameraActivity extends PortraitActivity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
private Bitmap photo;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
}
/** Method to open camera and take photo */
public void takePhoto(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
/** Method called after taking photo and coming back to current Activity */
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
photo = (Bitmap) data.getExtras().get("data");
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", photo);
startActivity(intent);
}
}
}
In the second activity (call it NewActivity), just put the following code in the onCreate method, after having assigned imageView
to the corresponding view in the XML layout.
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
imageView.setImageBitmap(photo);
Upvotes: 0