onActivityResult returns with data = null - java

Ok so this here is the intent I am sending
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
startActivityForResult(intent, REQUEST_CODE);
And then in the onActivityResult I am doing this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("Intent name:",data.toString());
if (requestCode == REQUEST_CODE){
if (resultCode == Activity.RESULT_OK){
Toast.makeText(this, "Image saved to \n" + fileUri.toString() , Toast.LENGTH_LONG).show();
Toast.makeText(this, "Result Code: " + resultCode , Toast.LENGTH_LONG).show();
//Bitmap mBitMap = BitmapFactory.decodeFile(data.getData().toString());
//imageView.setImageBitmap(mBitMap);
}
else if (resultCode == RESULT_CANCELED){
Toast.makeText(this, "Capture Cancelled", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, "Capture failed", Toast.LENGTH_LONG).show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
The LogCat is showing a NullPointerException at the line that says Image Saved....
And also this:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null}
This happens whether i try to use the data object or the fileUri field of my class.
Why is data being returned null?
Why is it that even though I am using a field of the class i still get the same error?

Whenever you save an image by passing EXTRAOUTPUT with camera intent ie
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
in a file, the data parameter inside the onActivityResult always return null. So, instead of using data to retrieve the image , use the filepath to retrieve the Bitmap.
So onActivityResult would be something like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
String[] fileColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(imageUri,
fileColumn, null, null, null);
String contentPath = null;
if (cursor.moveToFirst()) {
contentPath = cursor.getString(cursor
.getColumnIndex(fileColumn[0]));
Bitmap bmp = BitmapFactory.decodeFile(contentPath);
ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setImageBitmap(bmp);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Capture Cancelled", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(this, "Capture failed", Toast.LENGTH_LONG)
.show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Make sure that you have taken imageUri or fileUri as a global variable so that you can access it inside onActivityResult as well. Best of luck

The correct/preferred way to handle data in these cases would be as:
In called Activity set data to the Intent , then setResult code as RESULT_OK and then finish that activity.
In this recieving activity , check the result code.. and retrieve data from Intent variable as :intent.getExtra("... "); //The variables which you have set in the child activity that has been closed now..

Related

how to pick a file in SD card?

I'm trying to make an app with Android Studio that can select a file in SD card and get its path, like an OpenFileDialog, I've tried this:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
However, it does not work, how can I do it ?
Try this:
Intent mediaIntent = new Intent(Intent.ACTION_GET_CONTENT);
mediaIntent.setType("*/*"); //set mime type as per requirement
startActivityForResult(mediaIntent,0);
You can change type according to your need.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0
&& resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
Log.d("", "Video URI= " + videoUri);
}
}

Four Activities don't display Toast

I have four activities:
Activity A
private void addCard() {
Intent intent = new Intent(MainActivity.this, GetNumberActivity.class);
startActivityForResult(intent, REQUEST_CODE_CREATE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_CREATE) {
if (resultCode == RESULT_OK) {
if (data.hasExtra("data")) {
// Card has been create
Toast.makeText(getApplication(), "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();
}
}
}
}
Activity B
Intent intent = new Intent(GetNumberActivity.this, ScanQrCodeActivity.class);
intent.putExtra(EXTRA_MESSAGE, uunitValue);
startActivityForResult(intent, REQUEST_CODE);
Then in the second activity I have to pass data to the third activity.
Activity C
Card card = new Card(path3, base32, nameCard, intervalTotp, passwordHotp, getDate(), expirationDate, hotpValue);
Intent intent = new Intent(ScanQrCodeActivity.this, Stage3Activity.class);
intent.putExtra("card", card);
startActivity(intent);
finish();
Activity D
Intent data = new Intent(Stage3Activity.this,MainActivity.class);
data.putExtra("data", card);
startActivityForResult(data, RESULT_OK);
When I press the button on Activity A, the Toast is not shown.
You need to update your code as follow
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE_CREATE) {
if (data.hasExtra("data")) {
// Card has been create
Toast.makeText(getApplication(), "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();
}
}
}
First check for RESULT_OK and then proceed further
Happy Coding!
Replace getApplication() with this (the context of the current activity)
Toast.makeText(this.class, "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();
Use getApplicationContext() instead of getApplication() in your makeText() method

android studio, how to save on save,change DATA

I have been making paint app
it has a save option and load, but every time I save another image go to gallery
i want to 'save on save' option too, to load image change and save on it.
the code save:
drawView.setDrawingCacheEnabled(true);
//attempt to save
String ima= MediaStore.Images.Media.insertImage(
getContentResolver(), drawView.getDrawingCache(),
UUID.randomUUID().toString() + ".png", "drawing");
//feedback
if (ima != null) {
Toast savedToast = Toast.makeText(getApplicationContext(),
"Drawing saved to Gallery!", Toast.LENGTH_SHORT);
savedToast.show();
} else {
Toast unsavedToast = Toast.makeText(getApplicationContext(),
"Oops! Image could not be saved.", Toast.LENGTH_SHORT);
unsavedToast.show();
}
drawView.destroyDrawingCache();
}
saveDialog.show();
the load code:
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
saveDialog.show();
}
}
#Override
protected void onActivityResult ( int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
columnIndex = cursor.getColumnIndex(filePathColumn[0]);
temp = cursor.getString(columnIndex);
cursor.close();
a= BitmapFactory.decodeFile(temp);
Drawable d = new BitmapDrawable(getResources(), a);
drawView.setBackgroundColor(Color.WHITE);
drawView.startNew();
drawView.setBackground(d);
Rather than trying to have a save on save method, perhaps try instead of loading a default image you load the image you would like to edit. The file path exists since you are saving them in the drawable area and you are able to access the images in your drawable.

How can i select an image from gallery and display that in another activity..?

i have created the code to select image from gallery,but i can not pass that value to another activity through bundle..please help me
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
enter code here
i need to pass the SelectedImageUri to another activity as bundle
Use this
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
Intent intent = new Intent(this , Second_activity.class );
intent.putExtra("image_path", selectedImagePath);
startActivity(intent);
}
it will start the Second Activity, then on the second Activity receive those values by this
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("image_path");
//use value
}
I know is not exactly what you were looking for but if you pass selectedImagePath using i.putExtra("photoPath", selectedImagePath);, you can later load the image using only the path.
I need to pass the SelectedImageUri to another activity as bundle
=> FYI, Uri class itself implement Parcelable, so you can add and get value into/from Intent directly.
// Add a Uri instance to an Intent
intent.putExtra("SelectedImageUri", SelectedImageUri);
// Get a Uri from an Intent
Uri uri = intent.getParcelableExtra("SelectedImageUri");

Building a Camera App - Receive

I am new to Android programming and I'm writing an application in Java that opens the camera take a photo and save it. I made it via Intents but I can't see onActivityResult running.
I have tested it into my phone (Samsung Galaxy S) and when I take the photo I receive a preview of that photo having two buttons one Save and the other Cancel. I haven't added something to my code to do this so I think it's something that camera does. I want after capturing the image to run onActivityResult (after I press the Save button on the preview).
But how I'm going to return a result to start onActivityResult after pressing the Button Save on the preview?
I FORGOT to tell that after i press save my entire app is terminated.
Here is my Code
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TakePicButton = (Button) findViewById(R.id.TakePicture);
TakePicButton.setOnClickListener((android.view.View.OnClickListener) this);
}
#Override
public void onDestroy(){
super.onDestroy();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
} else {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.TakePicture){
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
try the below code, you will have to modify it a bit, it will help you get From Library and From Camera both, the SELECT_PICTURE is used for getting image from library
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case SELECT_PICTURE:
Uri selectedImageUri = data.getData();
filemanagerstring = selectedImageUri.getPath();
selectedImagePath = getPath(selectedImageUri);
if (selectedImagePath != null)
myFile = new File(selectedImagePath);
else if (filemanagerstring != null)
myFile = new File(filemanagerstring);
if (myFile != null) {
Bitmap bmp_fromGallery = decodeImageFile(selectedImagePath);
break;
case CAMERA_REQUEST:
Bitmap bmp_Camera = (Bitmap) data.getExtras().get("data");
break;
default:
break;
}
}

Categories

Resources