Uri Returns null in onActivityResult - android

I am doing a module which needs to convert image into pdf. i have successfully implemented the camera and can display its image. but my problem is getting the uri of that image. i saw a code snippet here in StackOverflow and followed it but it returns null.
here is my sample code:
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
uri = data.getData();
if(uri == null)
{
tvUri.setText("null");
}else{
tvUri.setText(uri.toString());
}
}
}
to test if it is null, i proceeded to set the textview into its value if it has one, but if not, then i set it to null.

protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
Uri u = intent.getData();
}

try this
uri = data.getExtras().get("data");
instead of
uri = data.getData();

Related

No callback for startActivityForResult in case of MediaStore.INTENT_ACTION_VIDEO_CAMERA

The case is when user selects camera, he has the flexibility to either capture image or record video and the user shall be able to show the same in one's app.
For this case, MediaStore.INTENT_ACTION_VIDEO_CAMERA intent is used but no callback comes in either case of capturing image or recording video.
Intent intent = new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA);
startActivityForResult(intent, VIDEO_CAMERA);
For getting result, code used as follows:-
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
return;
}
if (resultCode == RESULT_OK) {
if (requestCode == VIDEO_CAMERA) {
Uri uri = data.getData();
}
}
}
To start the camera and get the result back in onActivityResult(), you should create an Intent with MediaStore.ACTION_IMAGE_CAPTURE:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Similarly, to capture a video, use MediaStore.ACTION_VIDEO_CAPTURE
I finally found a solution and it is as follows:-
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
Intent chooserIntent = Intent.createChooser(takePictureIntent, "Capture Image or Video");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takeVideoIntent});
startActivityForResult(chooserIntent,VIDEO_CAMERA);
And finally receives the callback in onActivityResult and I got the uri in this way:-
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
return;
}
if (resultCode == RESULT_OK) {
if (requestCode == VIDEO_CAMERA) {
Uri uri;
if (data == null || data.getData() == null) {
Bitmap bitmap (Bitmap)data.getExtras().get("data");
// TODO:Get uri from bitmap for image
uri = getImageUri(context, bitmap);
} else {
//Get uri for video
uri = data.getData();
}
}
}
}

How get Image Uri from Camera?

Help pls,
how i can get URI from camera Picture i check some articals and don't understand how dows it works, pls explain me or give some links on this topic here's my code:
private void createDirectory() {
directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Meassure Preassure Pic");
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == REQUEST_CODE_PHOTO && resultCode == RESULT_OK) {
if (intent != null && intent.getExtras() != null) {
Bitmap imageBitmap = (Bitmap) intent.getExtras().get("data");
ivPhoto.setImageBitmap(imageBitmap);
}
}
}
public void onClickPhoto(View view) {
Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (pictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(pictureIntent, REQUEST_CODE_PHOTO);
}
}
The onActivityResult method contains the data. Firstly you need to check if the data is null, after that you can use getdata() on returned intent to get URI. You can also get the real path of the captured image if you want to. Below is the code sample :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_GALLERY_CODE && resultCode == Activity.RESULT_OK) {
uri = data.getData();
String filePath = getRealPathFromURIPath(uri, getActivity());
File file = new File(filePath);
Log.d(TAG, "Filename " + file.getName());
}
}

onActivityResult returns null Intent

I am trying to capture image from camera. But it returns null Intent onActivityResult .
Here is my code
CaptureImageFromCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 7);
}
});
And onActivityResult is
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 7 && resultCode == RESULT_OK) {
try {
if(data.getData() == null) {
bitmap = (Bitmap)data.getExtras().get("data");
} else {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
}
ImageViewHolder.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I am new to Android so please explain answer in detail.
Thanks in advance..!
Start Your work by passing intent through this :
startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), 100);
so after capturing the image from camera use to get data about images through intent :
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri yourCapturedImage = intent.getData();
}
Make sure you have added camera permissions, write permissions in manifest and also cross verify run time permissions.
Use your Intent like below, and specify the path where captured image will be write -
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri outputUri = Uri.fromFile("path of your directory");
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
startActivityForResult(intent, 7);
You can also use setImageURI() in onActivityResult() instead of setImageBitmap()
First of all add the camera permissions in android manifest. If your android version is greater than lollipop then you need to add run time permissions See documentations here
<uses-permission android:name="android.permission.CAMERA"/>
and then your code will be
CaptureImageFromCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 7);
}
});
and then use setImageUrI() in your onActivityResult()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case 7:
/*case 7 for image results and display image in imageview via Uri*/
if (resultCode == RESULT_OK)
{
Uri imageUri = data.getData();
ImageViewHolder.setImageURI(imageUri);
}
return;
default:
return;
}
}
Uri imageUri = data.getData() will no more work..
first get bitmap from data like this:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
val imageBitmap = data.extras.get("data") as Bitmap
imageView.setImageBitmap(imageBitmap)
}
}
then save bitmap image.. please find details here:
https://developer.android.com/training/camera/photobasics

Draw bitmap in different activity

So my app should capture image with intent then draw that image in different activity, but it doesn't work. Here is my code:
Main activity:
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "pic.jpg");
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, 2);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2) {
Intent k = new Intent(MainActivity.this, FullActivity.class);
startActivity(k);
}
}
Second activity:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri selectedImage = imageUri;
iv.setImageBitmap(bp);
}
Can someone help me and show where the problem is. Thanks in advance.
You need to pass the Uri as a string with the Intent in your MainActivity.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2) {
Intent k = new Intent(MainActivity.this, FullActivity.class);
k.putExtra("uri", imageUri.toString());
startActivity(k);
}
}
Then get the string in your FullActivity and parse it to a Uri, then use that to set your ImageView.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
String uriString = getIntent().getStringExtra("uri");
Uri selectedImage = Uri.parse(uriString);
// you may also need to call imageView.setImageURI(null); here
// or get the bitmap first to use imageView.setImageBitmap();
imageView.setImageURI(selectedImage);
}
From here
Called when an activity you launched exits, giving you the requestCode
you started it with, the resultCode it returned, and any additional
data from it
Your onActivityResult never get called when starting Second activity
Try send the uri you receive in Main activity in Bundle
Uri imageUri = intent.getData();
Intent intent = new Intent(MainActivity.this, SecondActiviy.class);
intent.putExtra("mUri", imageUri.toString());
And you can get this uri on SecondActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String uri= getIntent().getStringExtra("mUri");
Uri imageUri= Uri.parse(uri);
Bitmap bitmap=MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
}
// Global Declaration<br>
private Uri fileUri;
private static final int REQUEST_CODE_PHOTO = 101;
// Click for capture Image
fileUri = getOutputMediaFileUri(REQUEST_CODE_PHOTO); // create a file to save the image
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
startActivityForResult(intent, REQUEST_CODE_PHOTO);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_PHOTO) {
if (resultCode == RESULT_OK) {
String PATH = fileUri.getPath();
Intent k = new Intent(MainActivity.this, FullActivity.class);
k.putExtra("KEY_FOR_PATH, PATH);
startActivity(k);
}
}
}
}
// In FullActivity
String pathOfFile = getIntent().getExtras().getString("KEY_FOR_PATH");
Bitmap myBitmap = BitmapFactory.decodeFile(pathOfFile);
//Now set myBitmap in ImageView
imageView.setImageBitmap(myBitmap)
// Add this method
private Uri getOutputMediaFileUri(int type){
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "YOUR IMAGE FOLDER");
if(imagesFolder != null && !imagesFolder.exists()) {
imagesFolder.mkdirs();
}
int photonum = AppTypeDetails.getInstance(MainActivity.this).getImageName();
AppTypeDetails.getInstance(ReportTab.this).setImageName(++photonum);
File image = new File(imagesFolder, String.format("image%08d.jpeg", photonum));
return Uri.fromFile(image);
}

Using intent to use Camera in Android

I am using the following code to use camera by using intent.
In the parameter of intent I am passing android.provider.MediaStore.ACTION_IMAGE_CAPTURE.
It is able to open the camera.
But the problem is that it stops unexpectedly.
The problem is that it gives null pointer exception on OnActivityResults.
I have used the below code:
public class demo extends Activity {
Button ButtonClick;
int CAMERA_PIC_REQUEST = 2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ButtonClick =(Button) findViewById(R.id.Camera);
ButtonClick.setOnClickListener(new OnClickListener (){
#Override
public void onClick(View view)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// request code
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if( requestCode == CAMERA_PIC_REQUEST)
{
// data.getExtras()
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
image.setImageBitmap(thumbnail);
}
else
{
Toast.makeText(demo.this, "Picture NOt taken", Toast.LENGTH_LONG);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Can anyone help me to solve this problem?
Try request code 1337.
startActivityForResult(cameraIntent , 1337);
This how I use it
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1337);
do you have the following declarations in your manifest ?:
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.autofocus" />
??
I used to do the same...
here is my call to intent:
File file = new File( _path );
Uri outputFileUri = Uri.fromFile( file );
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
intent.putExtra( MediaStore.EXTRA_VIDEO_QUALITY,1);
the only slight difference between my and your code - I have file path in URI sending among call
I have used the following code and it worked!
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
final Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
im.setImageDrawable(null);
im.destroyDrawingCache();
Bundle extras = data.getExtras();
Bitmap imagebitmap = (Bitmap) extras.get("data");
im.setImageBitmap(imagebitmap);
}
}
Using intent to use Camera in Android
##
Uri imageUri;
1:-
TextView camera = (TextView)findViewById(R.id.camera);
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(), new Date().getTime() + "myPic.jpg");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(cameraIntent, IMAGE_CAMERA_REQUEST);}
2:-
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == IMAGE_CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getActivity().getContentResolver().notifyChange(selectedImage, null);
ContentResolver contentResolver = getActivity().getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(contentResolver, selectedImage);
imageDialog(bitmap);
} catch (Exception e) {
Log.e("Camera", e.toString());
}
}
}
}}
I hope it's working for you.

Categories

Resources