Audio Recorder Android - android

I'm doing a aplication to recorder the sound, but when I try to record the sound,the emulator is stopped because it ask me to insert an SD card. What I have to do? I couldn't do without an SD card ?
Thank you!
package proiektua.proiektua;
import java.io.FileDescriptor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
int peticion = 1;
Uri url1;
private int STATE_CONFIGURE;
private int mState = STATE_CONFIGURE;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void grabar(View v) {
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, peticion);}
public void setOutputFile(FileDescriptor fd) {
switch (mState) {
case STATE_RECORD:
throw new RuntimeException("setOutputFile cannot be called while recording!");
case STATE_RELEASED:
throw new RuntimeException("setOutputFile called on an already released recorder!");
default:
break;
}
}
public void reproducir(View v) {
MediaPlayer mediaPlayer = MediaPlayer.create(this, url1);
mediaPlayer.start();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == peticion) {
url1 = data.getData();
}
}
}

You have the option to create an SDCard through the mksdcard command and associate it with the amulator.

please look at this function:
public void setOutputFile (FileDescriptor fd)
Added in API level 3
Pass in the file descriptor of the file to be written. Call this after setOutputFormat() but before prepare().
You can decide where to store the file.
also look at this example.
http://androidfreakers.blogspot.co.il/2011/09/using-mediarecorder.html

Related

FileObserver not working with intent

I need to use camera intent and keep track of the new images taken by the intent. As I need to take multiple photos at a time, I used the intent MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA. Here is my code:
package com.arko.cameraintent;
import android.content.Intent;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.io.File;
public class CameraMainActivity extends AppCompatActivity {
private static final int REQ_CODE = 0;
AppCompatActivity main_activity = this; // save it for future use
DirectoryFileObserver directoryFileObserver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_main);
}
public void takePhoto(View view) // fired when a button in the main application is clicked. This starts the camera application.
{
Intent callCamAppIntent = new Intent();
callCamAppIntent.setAction(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
Toast.makeText(this, dir, Toast.LENGTH_SHORT).show();
directoryFileObserver = new DirectoryFileObserver(dir);
directoryFileObserver.startWatching();
startActivityForResult(callCamAppIntent, REQ_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQ_CODE){
Toast.makeText(this, "picture taken successfully", Toast.LENGTH_SHORT).show();
directoryFileObserver.stopWatching();
}
}
}
The code of DirectoryFileObserver class:
package com.arko.cameraintent;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.FileObserver;
import android.support.annotation.Nullable;
import java.io.File;
public class DirectoryFileObserver extends FileObserver {
String rootDir;
static final int mask = (FileObserver.CREATE | FileObserver.MODIFY | FileObserver.DELETE);
public final String DIRECTORY_NAME = "Simplified Intent";
public DirectoryFileObserver(String path) {
super(path, mask);
rootDir = path;
}
#Override
public void onEvent(int event, #Nullable String path) {
switch(event){
case FileObserver.CREATE:
String outputDir = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +
Environment.DIRECTORY_PICTURES +
File.separator + DIRECTORY_NAME;
// do something here
break;
case FileObserver.MODIFY:
// will be implemented later
break;
case FileObserver.DELETE:
// will be implemented later
break;
}
}
}
But the onEvent function never gets executed, as suggested by Debug. What is the reason behind it? I have even changed the default save location of the camera application to Internal Storage (initially it was set to SD card) and ran my app again, but nothing changed. I have mentioned both READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions in the manifest file.

How to record and save a video using Google Glass

I am fairly new to android studio and developing with android. I have been trying this for months now, I need to record and save a video using Google Glass. The code that I currently have follows. This is code inspired from https://developers.google.com/glass/develop/gdk/camera#images
When I run this process, the video will start recording and then when the recording is stopped, it will give me the option "tap to accept" When I tap, nothing happens. I understand this is because my method "processVideoWhenReady" does absolutely nothing.
I would like to know if anyone could help me with what I should include in that method to save the video and allow it to be viewed by the user within my application.
Any literature or websites that have useful information about developing for Google Glass would also be a huge help if possible. Google's documentation doesn't seem very helpful to me and very limited.
import android.app.Activity;
import android.graphics.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.content.Intent;
import android.os.FileObserver;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.VideoView;
import com.google.android.glass.content.Intents;
import com.google.android.glass.widget.CardBuilder;
import java.io.File;
import java.io.IOException;
public class RecordActivity extends Activity {
private static final int CAMERA_VID_REQUEST = 1337;
static final int REQUEST_VIDEO_CAPTURE = 1;
public VideoView mVideoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_record);
takeVideo();
}
private void takeVideo(){
Intent startVideo = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (startVideo.resolveActivity(getPackageManager()) != null){
startActivityForResult(startVideo, REQUEST_VIDEO_CAPTURE);
}
//RecordActivity.this.startActivityForResult(startVideo, CAMERA_VID_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
//System.out.println("Entered onActivityResult() method");
String a = getIntent().getStringExtra("record");
System.out.println(a);
if(requestCode == CAMERA_VID_REQUEST && resultCode == RESULT_OK) {
//System.out.println("Result Okay");
Uri videoUri = data.getData();
mVideoView.setVideoURI(videoUri);
String videoPath = data.getStringExtra(Intents.EXTRA_VIDEO_FILE_PATH);
processVideoWhenReady(videoPath);
}
super.onActivityResult(requestCode, resultCode, data);
}
protected void processVideoWhenReady(final String videoPath){
final File videoFile = new File(videoPath);
if (videoFile.exists()){
//Process Video
}
else {
final File parentDirectory = videoFile.getParentFile();
FileObserver observer = new FileObserver(parentDirectory.getPath(), FileObserver.CLOSE_WRITE | FileObserver.MOVED_TO) {
private boolean isFileWritten;
#Override
public void onEvent(int event, String path) {
if (!isFileWritten) {
//make sure file was created in directory expected
File affectedFile = new File(parentDirectory, path);
isFileWritten = affectedFile.equals(videoFile);
if (isFileWritten) {
stopWatching();
runOnUiThread(new Runnable() {
#Override
public void run() {
processVideoWhenReady(videoPath);
}
});
}
}
}
};
observer.startWatching();
}
}
}

Can't pass BluetoothAdapter object via putExtra method of Intent

I am trying to pass my BluetoothAdapter BA to another activity using putExtra(String str, Bundle bundle) method of Intent, but the compiler of my Android studio shows an error in that. When I hover over a red curved line that appears under the method, it shows me
Can't resolve method 'putExtra(java.lang.String, android.bluetooth.BluetoothAdapter)'
This is what I'm talking about
If I understand it correctly, Bundle is basically any object, hence there shouldn't be any problem passing a BluetoothAdapter to another activity via putExtra.
This is my MainActivity.java
package vertex2016.mvjce.edu.bluealert;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import static java.lang.Thread.sleep;
public class MainActivity extends AppCompatActivity {
public BluetoothAdapter BA = BluetoothAdapter.getDefaultAdapter();
private int REQ_CODE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void connect(View v)
{
if(BA == null)
Toast.makeText(MainActivity.this, "System Doesn't Support Bluetooth", Toast.LENGTH_SHORT).show();
else if(!BA.isEnabled())
{
Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBT, REQ_CODE);
}
else {
Toast.makeText(MainActivity.this, "ALREADY ON!!", Toast.LENGTH_SHORT).show();
searchBTDevices();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode!=RESULT_CANCELED) {
Toast.makeText(MainActivity.this, "TURNED ON!", Toast.LENGTH_SHORT).show();
searchBTDevices();
}
else
Toast.makeText(MainActivity.this,"FAILED TO ENABLE BLUETOOTH", Toast.LENGTH_LONG).show();
}
public void searchBTDevices()
{
Thread searchThread = new Thread() {
#Override
public void run() {
Intent searchBT = new Intent(getApplicationContext(), SearchBTDevice.class);
searchBT.putExtra("BT_ADAPTER", BA);
startActivity(searchBT);
}
};
searchThread.start();
}
}
And this is my SearchBTDevices.java that is supposed to receive the extra info.
package vertex2016.mvjce.edu.bluealert;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class SearchBTDevice extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_btdevice);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
Where am I going wrong?
Thank you for your time!!
Intents can only be used to pass primitives, Parcelable and Serializable objects. BluetoothAdapter does not support either of these and it can't be built from primitives, so you can't create a Parcelable that will help you.
So when faced with a situation like this, you need to pass in parameters that the Activty can use to re-build or retrieve the object necessary for execution.
If you need the ability to use different BluetoothAdapters, then you need to tell SearchBTDevices which BluetoothAdapter you want to check and have SearchBTDevices retrieve the adapter itself. For example,
public class SearchBTDevices extends Activity {
public static final String PARAM_ADAPTER_TYPE = "adapterType";
public static final int ADAPTER_DEFAULT = 1;
public static final int ADAPTER_ALTERNATE = 2;
private BluetoothAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int adapterType = getIntent().getIntExtra(PARAM_ADAPTER_TYPE, ADAPTER_DEFAULT);
switch(adapterType) {
case PARAM_ADAPTER_ALTERNATE:
mAdapter = getAlternateAdapter(); // This is whatever method you need to get it.
break;
default:
mAdapter = getDefaultAdapter();
}
}
}
Now, whenever you need to start this Activity, just do this:
Intent intent = new Intent(getContext(), SearchBTDevices.class);
intent.putExtra(SearchBTDevices.PARAM_ADAPTER_TYPE, SearchBTDevices.ADAPTER_ALTERNATE);
startActivity(intent);
So when SearchBTDevices starts, it will pull the parameter from the intent and know that it needs to use the alternate adapter.

How can I share data between a checkbox/radiogroup and textview in two different activities on eclipse?

I need to put a list of items in different pages that can be selected through checkboxes and radio buttons and the overall price (addition of each selected item) should be added to the main page.
How can I do this?
here's my code for the main page:
package com.example.mcdonalds;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
public class MenuPage extends Activity {
private static final String CheckBox = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_page);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_page, menu);
return true;
}
public void gotoBeefPage(View view) {
Intent intent1 = new Intent(this,BeefPage.class);
startActivity(intent1);
}
public void gotoChickenPage(View view) {
Intent intent2 = new Intent(this,ChickenPage.class);
startActivity(intent2);
}
public void gotoKidsMenu(View view) {
Intent intent3 = new Intent(this,KidsMenu.class);
startActivity(intent3);
}
public void gotoDessertPage(View view) {
Intent intent4 = new Intent(this,DessertPage.class);
startActivity(intent4);
}
and here's a code for one of the pages that contains checkboxes:
package com.example.mcdonalds;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CheckBox;
public class BeefPage extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_beef_page);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.beef_page, menu);
return true;
}
If I understand your question correctly, you're creating an app where the user can go to the different pages, add their dishes (1 beef, 2 chicken, 2 desserts, etc.), and then return to the main page for a summary of their order with a total price?
It seems like you're trying to solve this problem entirely in the view (Activity) code. The better way to structure it is to have all our activities operate on a shared Data Model object. You can even have your main activity create the object and then pass it to the individual sub-activities through the Intent.
Modifying your code:
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
public class MenuPage extends Activity {
private static final String CheckBox = null;
private Order myOrder = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_page);
this.myOrder = new Order();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_page, menu);
return true;
}
public void gotoBeefPage(View view) {
Intent intent1 = new Intent(this,BeefPage.class);
intent1.putExtra("Order",this.myOrder);
startActivityForResult(intent1, requestCode);
}
public void gotoChickenPage(View view) {
Intent intent2 = new Intent(this,ChickenPage.class);
intent2.putExtra("Order",this.myOrder);
startActivityForResult(intent2, requestCode);
}
public void gotoKidsMenu(View view) {
Intent intent3 = new Intent(this,KidsMenu.class);
intent3.putExtra("Order",this.myOrder);
startActivityForResult(intent3, requestCode);
}
public void gotoDessertPage(View view) {
Intent intent4 = new Intent(this,DessertPage.class);
intent4.putExtra("Order",this.myOrder);
startActivityForResult(intent4, requestCode);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
// Update your local copy of myOrder with what you return in data.
}
You will have to make sure that your Order object conforms to the Parcelable or Bundle interfaces in order to pass it through an intent (I think you can also use Serializable).

Android Camera activity stays open

I'm trying to take a picture with my Android app but when I take the image, the camera display doesn't go away. The code I am using is below. I also used the SDK on Google's developer website SDK.
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Camera;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mainButton = (Button)findViewById(R.id.mainBtn);
mainButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
//Intent userCreationIntent = new Intent(v.getContext(), SecondviewActivity.class);
//startActivity(userCreationIntent);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
return false;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView = (ImageView)findViewById(R.id.imageView1);
imageView.setImageBitmap(photo);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
The problem is within your onTouchListener for the Button!
I just ran your code and changed the onTouchListener to a onClickListener for the Button instead and the code is working.
See my revised code here:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mainButton = (Button) findViewById(R.id.mainBtn);
mainButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
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) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(photo);
}
}
}
I'm not precisely sure, why you're using the onTouchListener for a Button, but it's not working apparently - really weird behavior actually ;-)
EDIT: Just a small update. I tried to debug the code and if you use the onTouchListener instead of the onClickListener, when clicking on the button, you actually trigger 3 MotionEvents: MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE and MotionEvent.ACTION_UP.
Now the first event MotionEvent.ACTION_DOWN will trigger your intent to show the camera and when you click OK after you've taken a picture, the next MotionEvent MotionEvent.ACTION_MOVE is waiting in line to be triggered and this will send you to the camera activity once again. Now after taking one more picture and clicking OK, you return to your activity and now the last MotionEvent MotionEvent.ACTION_UP is waiting in line and triggers a 3rd call to the camera activity. After the last camera call, you will be able to get back to your activity without problems ;-)
Why the onClickListener doesn't do this, is because it handles a "full" click which could be MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE and MotionEvent.ACTION_UP and so all 3 events is happening in ONE click instead.
Hope this helps ;-)
This is happening because you are not attaching a path with the intent as to tell Android where to store the image. I had the same problem too.
Try the following code: ( I just added 4 lines )
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Camera;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
// Creating the Uri where Camera saves a picture each time
String imagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + System.currentTimeMillis() + "_myCameraImage.jpg";
File imageFile = new File(imagePath);
imageUri = Uri.fromFile(imageFile);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mainButton = (Button)findViewById(R.id.mainBtn);
mainButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
//Intent userCreationIntent = new Intent(v.getContext(), SecondviewActivity.class);
//startActivity(userCreationIntent);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
return false;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView = (ImageView)findViewById(R.id.imageView1);
imageView.setImageBitmap(photo);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Before you copy and paste this code, make sure that you have necessary import statements in your code like import statements for File, Uri etc.
I strongly hope it will work now :)

Categories

Resources