HI i am getting null pointer exception while sending image path to another activity
here is below my code
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
System.out.println(requestCode + ", " + resultCode);
if (requestCode == SELECT_PICTURE) {
if (data.getData() != null) {
Uri selectedImageUri = data.getData();
String path = getPath(selectedImageUri);
System.out.println("PATH = "+path);
Intent _intent = new Intent(MainActivity.this,AndroidFaceDetector.class);
_intent.putExtra("mypath", path);
startActivityForResult(_intent, CROPPED_FACE_IMAGE);
}
else
{
Toast.makeText(MainActivity.this, "Please try again", Toast.LENGTH_SHORT).show();
}
}
else {
setImage(data);
}
}
}
Now in AndroidFaceDetector class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new myView(this));
Intent _intent = getIntent();
path = _intent.getStringExtra("mypath");
System.out.println("Path is gettting "+path); //here i am getting null value
}
because
here i am getting path value null.. can any body solve this problem....
try like this in Second Activity
Intent myintent = getIntent();
if(null != myintent.getExtras())
{
// do your work here
String path = myintent.getExtras().getString("mypath");
}
else
{
// not here you can't get values
Toast.makeText(getApplicationContext(),"No Recor Here..",12).show();
}
In your second activity try with this code instead of yours,
path = _intent.getString("mypath");
(or)
Bundle bundle = this.getIntent().getExtras();
String pic = bundle.getString("mypath");
If you wish to pass image instead of URI means try this code
In your First activity,
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Intent intent = new Intent();
intent.setClass(AndroidPassingBitmap.this, AndroidReceiveBitmap.class);
intent.putExtra("Bitmap", bitmap);
startActivity(intent);
In your Second activity,
Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");
ImageView viewBitmap = (ImageView)findViewById(R.id.bitmapview);
viewBitmap.setImageBitmap(bitmap);
Write below Code to get data from intent, it will solve your problem.
Bundle bdl=getIntent().getExtras();
String path=bdl.getString("mypath");
And see below link for more information.
pass bitmap between activities in android
Related
I am creating a app that opens camera for user and after image captured it will be shows on ImageView! But the image in ImageView has very bad quality
here is the code:
public class Camera extends AppCompatActivity implements View.OnClickListener {
ImageView imgView;
Button camera, setBackground;
Intent i;
int cameraData = 0;
Bitmap bitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
initialize();
}
private void initialize() {
imgView = (ImageView) findViewById(R.id.bPicture);
camera = (Button) findViewById(R.id.bOpenCamera);
setBackground = (Button) findViewById(R.id.bSetBackground);
camera.setOnClickListener(this);
setBackground.setOnClickListener(this);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
Bundle extras = data.getExtras();
bitmap = (Bitmap) extras.get("data");
Bitmap resizedBmp = Bitmap.createScaledBitmap(bitmap, imgView.getWidth(),imgView.getHeight(), false);
imgView.setImageBitmap(resizedBmp);
}
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bOpenCamera:
i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
break;
case R.id.bSetBackground:
try {
WallpaperManager wallmngr = WallpaperManager.getInstance(this);
wallmngr.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
what should I do to increase image quality?
Use something other than the thumbnail. Quoting the documentation for ACTION_IMAGE_CAPTURE, with emphasis added:
The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.
So, specify a Uri in EXTRA_OUTPUT where a full camera image should be written to. Then, use an image-loading library, like Picasso, to load the photo into your ImageView.
Here is a sample app that demonstrates using EXTRA_OUTPUT.
This is how I made it work for me! Assuming you are capturing an image, and would like to show the captured image in a new Activity, you can follow this way:
First on the click of button you can:
public void cameraFuture(View view) // <-- onClick() method of Camera Button
{
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),
"MyPhoto.jpg");
outPutfileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
startActivityForResult(intent, TAKE_PIC);
}
Then on the onActivityResult() method, you can do this way:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PIC && resultCode==RESULT_OK){
Toast.makeText(this, outPutfileUri.toString(),Toast.LENGTH_LONG).show();
Intent bitIntent = new Intent(this, CameraTake.class);
bitIntent.putExtra("imageUri", outPutfileUri);
startActivity(bitIntent);
finish();
}
}
And in the next Activity, you can receive the file this way:
Intent receiveIntent = getIntent();
uri = receiveIntent.getParcelableExtra("imageUri");
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
receiveImg= null;
} else {
receiveImg= extras.getString("PASSER");
}
} else {
receiveImg= (String) savedInstanceState.getSerializable("PASSER");
}
File imgFile = new File(Environment.getExternalStorageDirectory(),"MyPhoto.jpg");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.camOut);
myImage.setImageBitmap(myBitmap);
}
http://www.c-sharpcorner.com/UploadFile/9e8439/how-to-make-a-custom-camera-ion-android/I have made a custom camera app and after click a picture i want to preview it in an image view but when i am previewing it in image view,image is being scaled from actual size of picture.
his is how I made it work for me! Assuming you are capturing an image, and would like to show the captured image in a new Activity, you can follow this way:
First on the click of button you can:
public void cameraFuture(View view) // <-- onClick() method of Camera Button
{
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),
"MyPhoto.jpg");
outPutfileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
startActivityForResult(intent, TAKE_PIC);
}
Then on the onActivityResult() method, you can do this way:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PIC && resultCode==RESULT_OK){
Toast.makeText(this, outPutfileUri.toString(),Toast.LENGTH_LONG).show();
Intent bitIntent = new Intent(this, CameraTake.class);
bitIntent.putExtra("imageUri", outPutfileUri);
startActivity(bitIntent);
finish();
}
}
And in the next Activity, you can receive the file this way:
Intent receiveIntent = getIntent();
uri = receiveIntent.getParcelableExtra("imageUri");
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
receiveImg= null;
} else {
receiveImg= extras.getString("PASSER");
}
} else {
receiveImg= (String) savedInstanceState.getSerializable("PASSER");
}
File imgFile = new File(Environment.getExternalStorageDirectory(),"MyPhoto.jpg");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.camOut);
myImage.setImageBitmap(myBitmap);
}
Hope it helps!
This link completely described what you want. Here is the summary:
1- Add this to the Manifest:
< uses-feature android:name="android.hardware.camera" android:required="true" />
2- Add this line to your class:
static final int REQUEST_IMAGE_CAPTURE = 1;
3- call this method where you want to open camera:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
4- Override onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
ImageView ivImage = (ImageView) findViewById(R.id.ivImage);
ivImage.setImageBitmap(imageBitmap);
}
}
I hope it help you.
I'm working on an app which allows user to choose a picture from gallery and then I start a activity to crop it.
I want to send the cropped image back to calling activity.
Both activities extend AppCompatActivity.
Calling activity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
// start image crop activity
String dataString = data.getDataString();
Intent intent=new Intent(this, CropPhotoActivity.class);
intent.putExtra("SELECTED_PICTURE_FOR_CROP", dataString);
startActivityForResult(intent, CROP_PICTURE);
}
else if(requestCode == CROP_PICTURE) {
// get cropped bitmap
Bitmap bitmap = (Bitmap) data.getParcelableExtra("CROPPED_IMAGE");
profilePhoto.setImageBitmap(bitmap);
}
}
}
In the crop image activity, I have a button, which on click should return back to calling activity:
Button okButton = (Button)findViewById(R.id.ok_button);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent returnIntent = new Intent();
returnIntent.putExtra("CROPPED_IMAGE", cropped_bitmap);
setResult(RESULT_OK, returnIntent);
finish(); // sometimes restarts app
}
});
Sometimes the bitmap gets returned correctly whereas sometimes it does not and the app gets restarted without error. Why is this happening? Does putExtra have anything to do with bitmap size or anything else?
You could try substituting
AppcompatActivity.this.finish()
(where AppcompatActivity is your class name)
for:
finish(); // sometimes restarts app
Or, create a method in the calling Activity:
public static void cropComplete(Activity activity)
{
activity.startActivity(activity, AnotherActivity.class);
activity.finish();
}
Theres's a limit for data length passed as extra in a intent. Try not passing the dataString value; instead you should save the image as a temporary file, pass the path in the intent and then load the image from your calling activity (or you can just save the dataString in a static helper class).
In the crop activity (saving bitmap code from Save bitmap to location):
// Save bitmap
String filename = "tempImage.png";
File sd = Environment.getExternalStorageDirectory();
File dest = new File(sd, filename);
FileOutputStream out = null;
try {
out = new FileOutputStream(dest);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null)out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// Get image file path
String path = dest.getAbsolutePath();
// Set result with image path
Intent returnIntent = new Intent();
returnIntent.putExtra("CROPPED_IMAGE_PATH", path);
setResult(RESULT_OK, returnIntent);
finish();
In the caller activity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(requestCode == CROP_PICTURE) {
// Get image file path
String imagePath = data.getStringExtra("CROPPED_IMAGE_PATH");
// Load image
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
}
}
I am trying to add a code in my current app so that on one button click ,camera should open and one snap should be taken.
here is my code :
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
try {
PackageManager pm =this.getPackageManager();
final ResolveInfo mInfo = pm.resolveActivity(i, 0);
Intent intent = new Intent();
intent.setComponent(new ComponentName(mInfo.activityInfo.packageName, mInfo.activityInfo.name));
intent.setAction(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
}
catch (Exception e){ Log.i(TAG, "Unable to launch camera: " + e); }
Error : it displays -> Complete action uing
Not able to figure it out what i am doin wrong, can any one help me out in this issue.
First
Add permission to your manifest
<uses-feature android:name="android.hardware.camera" android:required="fase" />
Second
static final int REQUEST_IMAGE_CAPTURE = 1;
#Override
protected void onCreate (Bundle savedInstanceState) {
//Your code
//dispatchTakePictureIntent() <-- call this on some button click etc.
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
Third
Retrieve your Snap
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
Retrieved Image is only a small size thumbnail. To get/save full size Image
I'm trying to put extras (string) in an intent. I use startActivityForResult and onActivityResult to get my extras on the other side.
But I can't get why it doesn't works ! Here's my code :
buttonCamera.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("abc", "test");
startActivityForResult(intent, PHOTO_RESULT);
}
});
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PHOTO_RESULT) {
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
if (extras != null) {
String abc = extras.getString("abc");
Toast.makeText(getApplicationContext(), abc, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "can't get", Toast.LENGTH_SHORT).show();
}
}
}
}
I always get an empty toast, so the extra is not null.. But I cant't get the String..
Thanks !
edit:
To tell the camera the file name you can use this:
File mFile = new File(path, filename);
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile));
activity.startActivityForResult(intent, TAKE_PHOTO_CODE);
The camera activity will get your extra intent data, but it won't return that "abc" string. You can't tell the camera activity to return "abc" for results. What are you trying to do with that abc string?
Obviously you will get an empty toast .
You are putting extra for the intent new Intent(MediaStore.ACTION_IMAGE_CAPTURE) and the Intent data you are getting in onActivity result is different , (Returned from Camera Activity ) .
so store value in a class variable will work fine .