Android Getting null image data - java

I am trying to get the image from the camera. It works fine. I can take a photo and show it on the image view. Actually, I want to send this photo to my server after took. To do that, I try to pull the image in onActivityResult. But, when i check the Intent data, it always return null.Even though, the application runs fine and display the image. Why am I getting null for Intent data? Could you please help me?
Here is the code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ACTION_TAKE_PHOTO_B: {
if (resultCode == RESULT_OK) {
Log.e("TAG","data: "+data);
display_Photo();
//Process the image to send but, data is null
}
break;
}
}
}
Log cat:
data : Null

In the code you are using there are two button onclick handlers that call:
dispatchTakePictureIntent(int actionCode)
One button calls it with the enum ACTION_TAKE_PHOTO_B while the other call sit with the enum ACTION_TAKE_PHOTO_S
If you are passing in ACTION_TAKE_PHOTO_B, then the expected result of data in the returned intent is null.
The reason why is that before dispatchTakePictureIntent calls the intent MediaStore.ACTION_IMAGE_CAPTURE, it sets an extra intent parameter based on the actionCode passed in:
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
According to the documentation for ACTION_IMAGE_CAPTURE:
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.
So, because the EXTRA_OUTPUT parameter is being set, your image is being written to disk instead of being returned as data in the intent. To get the file location, you can inspect mCurrentPhotoPath which is written to before the intent is launched.

Related

Android: How to programmatically click on a button previously located in an activity in the activity stack, from a fragment?

When I am in a fragmentB, how to simulate a click on a button located in an activity in the stack.
LoginActivityA => fragmentB.
I want to simulate something like this:
LoginActivityA.btnClick() from the fragmentB
I tried to use this tickets with no success: Ticket1, Ticket2, Ticket3
Here is my code:
FragmentB:
//BEGIN TEST
String message="hello ";
Intent intent = new Intent(getActivity(), LoginActivity.class);
intent.putExtra("MESSAGE",message);
getActivity().setResult(2,intent);
getActivity().startActivityForResult(intent,2);
getActivity().finish();//finishing activity
//END TEST
LoginActivityA:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
//do the things u wanted
}
}
I precise that I try not to get multiple LoginActivity and Fragment launched. I would like to get only one stack with LoginActivity => Fragment
Any ideas?
EDIT:
I tried this from this tichet with no success:
((LoginActivity)getActivity()).login();
I launched the activity function from the presented fragment with an error
"No acceptable module found. Local version is 0 and remote version is
0."
You can use an interface with an onClick method that is implemented by your activity and then in fragment B call the method onClick whenever you want and pass the activity to it.
Although I presume what you are doing is wrong and you should change your mind around it.
This is not a good practice, but if you want you can call your activity UI elements by
((Button) getActivity().findViewById(R.id.button_id)).performClick();
Always remember, Android is a well architect platform, if you are trying to do something which is difficult, probably you should not do it. Rethink about your your design patterns.

Android - While handling an image is it better to store it or use it in temporary memory?

I'm trying to build an app that takes a picture with the camera an sends it back to the main activity to display it in an ImageVew. I saw a tutorial that saves the image in the SD Card when the picture is taken. I'm able to save the file but I'm having difficulties getting the location of the stored image.
I think that storing the image in the SD Card is too much work since that image is not that important.
Is there a way to "save" the image I just took with the camera in a BitMap element? if so is it more efficient than storing it in the SD Card?
Here is my MainActivity.java:
public class MainActivity extends AppCompatActivity {
private Uri imgLocation;
ImageView mImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button capture = (Button) findViewById(R.id.am_btn_camera);
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imgLocation = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "fname_" +
String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(MediaStore.EXTRA_OUTPUT, imgLocation);
startActivityForResult(intent, 1);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
Log.e("URI",imgLocation.toString());
mImageView.setImageBitmap(BitmapFactory.decodeFile(imgLocation.toString()));
}
}}
It is better to just save it on the external/internal storage and then access it. It is even better to use a third-party library to handle all the image rendering and their lazy-loading like:
Glide
Picasso
Fresco
Most of these libraries will focus on handling resources from HTTP URLs, but they can still be used to load information from a local file or a Content Provider.
Handling images with Bitmaps might cause OutOfMemoryError easily as shown in this link. Although, in this case, since you are only using 1 Bitmap, there is not a lot to worry about.
It's almost always better to save it in the file system and pass the filepath within activities.
Android Bitmaps literally consume a lot of memory, and it's never a good idea to keep it in memory unless it's really required.
I know, it's a bit of a hassle to store it in the file system and pass the file reference, but it'll prevent a lot of unwanted outOfMemory errors later within your app.
Use Picasso. It will automatically cache the file for you on the filesystem.
If you need to retain that file on disk for longer periods, you must save it yourself.

android remove data from getintent

I have an activity for handling deeplink which is a browsable activity
suppose user clicks a link on another app and my browsable activity handles that intent
and start the app, , then user minimise the app by pressing back button
class code for handling intent data
Uri link = getIntent().getData();
if user reopen app from running tasks getIntent() still have data
onDestroy method of browsable activity
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
setIntent(null);
}
setIntent(null) not working
so my question is how can i remove data from intent permanently
I am a little late on the answer here but I was dealing with a similar issue and found a solution. The reason you are still seeing data in the intent is because your app was originally started with an intent that contained data. That original intent is stored somewhere in Androids' ActivityManager and is immutable, to my understanding. When user reopens the app from "recent tasks", Android uses that original intent to recreate the application.
There is a workaround to this, however. In your apps onCreate() method, you can check to see if the Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY is set, which would allow your app to distinguish if it is being started from "recent tasks" or not, and therefore, you can avoid using the data contained in the intent.
Putting the following snippet in your onCreate() method would return true if the app is being opened from "recent tasks"
(getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0)
you have to remove data one by one key. i think you can't remove all data with one line.
if you want to remove specific key than you should use ==> getIntent().removeExtra("key"); or
getIntent().setAction("");
it will remove your data.
for more ==> Clearing intent
To remove the content it works best for me with the following lines of code.
//Clear DATA intent
intent.setData(null);
intent.replaceExtras(new Bundle());
intent.setFlags(0);
After that they can verify that the intent does not contain data

capture audio from google recognizer intent [duplicate]

I am working on application that will record the voice of the user and save the file on the SD card and then allow the user to listen to the audio again.
I am able to allow the user to record his voice using the RecognizerIntent, but I cant figure out how to save the audio file and allow the user to hear the audio. I would appreciate it if someone could help me out. I have displayed my code below:
// Setting up the onClickListener for Audio Button
attachVoice = (Button) findViewById(R.id.AttachVoice_questionandanswer);
attachVoice.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Speak");
startActivityForResult(voiceIntent, VOICE_REQUEST);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == VOICE_REQUEST && resultCode == RESULT_OK){
}
There is an example of how to do audio capture using MediaRecorder in the Android Developer Documentation.
I would recommend saving the files on the SD Card and then have your gallery code check the SD card to see which files to display. You can get the directory of the SD Card using the Environment.getExternalStorageDirectory() method. It would be best to save your files in a subdirectory of the SD Card root directory.
Make sure you give your applications the Permissions it will need. At the very least it will need RECORD_AUDIO and WRITE_EXTERNAL_STORAGE.
Also you have to see these tutorials:
http://www.androiddevblog.net/android/android-audio-recording-part-1
http://www.androiddevblog.net/android/android-audio-recording-part-2
If you really want to record audio via the speech recognition API then you could use the RecognitionService.Callback which has a method
void bufferReceived(byte[] buffer)
This gives you access to the recorded audio buffer as speech is being recorded and recognized. (No information is provided about the sample rate though.) You can then save the obtained buffers into a file for a later playback. I think keyboard apps use this call to display the waveform of the recorded speech. You have to implement the UI yourself.
The bare RecognizerIntent.ACTION_RECOGNIZE_SPEECH just returns a set of words/phrases without any audio.

Beautiful way to come over bug with ACTION_IMAGE_CAPTURE

I faced problem: ACTION_IMAGE_CAPTURE intent's behaviour depends on hardware manufacturer.
I think, best way to get photo from camera inserted in photo gallery must be something like following
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, CAPTURE_IMAGE_REQUEST);
and then get uri in onActivityResult:
switch (requestCode) {
case CAPTURE_IMAGE_REQUEST: {
if(resultCode == Activity.RESULT_OK) {
Uri uri = intent.getData();// content uri of photo in media gallery
//do something with this
}
break;
}
But I see, that this doesn't work on many devices; moreover, I found several different scenarios of Camera app behaviour:
some devices have bug with this event, so there is no way to get fullsized photos, and you can get 512px wide photo using tmp file in public directory only
some devices (including mine) insert taken photo into gallery, but does not return Uri. (getData() returns null, intent extras have only boolean key 'specify-data', value = true) If I try to get photo through the public tempfile then photo will be inserted into both gallery and tempfile.
some devices don't insert taken photos to gallery - and I must do it manually
I dont know, but there can be other different scenarious
So, is there best practices in managing such problems to cover a wide range of devices and manufacturers?
In this case I need take photo from camera, get it inserted into gallery, then get uri of photo in gallery.
A part of this has already been answered :
Check the implementation of hasImageCaptureBug()
For the null problem, maybe you can watch over the Gallery folder?
I don't think you should be checking for other cases. Google and Android OEM's monitor those issues so report them and hopefully they will be fixed.

Categories

Resources