Unable to save images taken using app? - java

I have spent much time trying to resolve this trivial problem but seem to be going in circles even after reading many tutorials and previously asked questions.
I am a novice at programming but want to get better and believe that learning through mistakes is the best way.
Can someone please help identify the problem with this code and what can be done in order to allow the pictures taken by the device's camera to be saved onto the phone afterwards rather than being discarded and previewed.
Thanks in advance!
Java code for relevant class:
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
public class Activity_Camera extends Activity implements View.OnClickListener {
public static final int cameraData = 1;
ImageButton ib;
ImageView iv;
Intent i;
Bitmap bmp;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Info:
Intent i = new Intent(this, Help.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
initialise();
}
private void initialise() {
iv = (ImageView) findViewById(R.id.ivPicReturn);
ib = (ImageButton) findViewById(R.id.ibTakePic);
ib.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.ibTakePic:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//
i.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri());
//
startActivityForResult(i, cameraData);
break;
}
}
//
private String getOutputMediaFileUri() {
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "image.jpg");
Uri uriSavedImage = Uri.fromFile(image);
Intent imageIntent = null;
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
return null;
}
//
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
iv.setImageBitmap(bmp);
}
}
}

because you're returning null return null; in your getOutputMediaFileUri
try this instead
public void onClick(View v) {
switch (v.getId()) {
case R.id.ibTakePic:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri());
startActivityForResult(i, cameraData);
break;
}
}
private Uri getOutputMediaFileUri() {
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "image.jpg");
return Uri.fromFile(image);
}

Related

How to share tts as and audio file in android studio

I'm new to programming, and I'm creating an app with texttospeech and I wanna be able to share the tts output as an audio file to other apps like whatsapp for example. I have heard about synthesizetofile() but I don't know anything about it.
Here's my code:
Main Activity class
package com.example.texttospeech;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import java.io.File;
import java.util.HashMap;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
EditText etInput;
Button btDone, btShare;
MediaPlayer mMediaPlayer;
TextToSpeech textToSpeech;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etInput = findViewById(R.id.et_input);
btDone = findViewById(R.id.bt_done);
btShare = findViewById(R.id.btn_share);
textToSpeech = new TextToSpeech(getApplicationContext()
, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int i) {
if (i == TextToSpeech.SUCCESS) {
int lang = textToSpeech.setLanguage(Locale.ENGLISH);
}
}
});
btDone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//String s = etInput.getText().toString();
//int speech = textToSpeech.speak(s, TextToSpeech.QUEUE_FLUSH, null);
HashMap<String, String> map = new HashMap<>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "speak");
File myDir = getApplicationContext().getFilesDir();
String documents = "documents/data";
File documentsFolder = new File(myDir, documents);
documentsFolder.mkdir();
String path = "/"+documents+"/"+"test.mp3";
textToSpeech.synthesizeToFile("text to speech to audio", map, path);
mMediaPlayer = new MediaPlayer();
try{
mMediaPlayer.setDataSource(path);
mMediaPlayer.prepare();
}
catch (Exception e) {
Log.d("ROYs", e.toString());
e.printStackTrace();
}
mMediaPlayer.start();
}
});
btShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bundle bundle = new Bundle();
Intent intent = new Intent( Intent.ACTION_SEND );
intent.setType("audio/mp3");
startActivity( intent );
}
});
}
private void shareAudio()
{
Bundle bundle = new Bundle();
Intent intent = new Intent( Intent.ACTION_SEND );
intent.setType("audio/mp3");
startActivity( intent );
}
}
Anyone who helps, would be appreciated very much!

How do I extract more information from places nearby using google places API?

My app only extracts name and address from the location selected from google places API. I want it to extract phone numbers as well. How do I do that? Below is my MapActivity.JAVA that enables the google places api and shows nearby places. I followed a tutorial and it only had extracted name and address, so I'm stuck now. Any help would be much appreciated. Thanks :)
MapActivity.JAVA
package com.golo.acer.mrestro4;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlacePicker;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
public class MapActivity extends AppCompatActivity {
private static final int PLACE_PICKER_REQUEST = 1;
private TextView mName;
private TextView mAddress;
private TextView mAttributions;
private static final LatLngBounds BOUNDS_MOUNTAIN_VIEW = new LatLngBounds(
new LatLng(27.690668, 85.313401), new LatLng(27.692634, 85.318969));
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_picker);
mName = (TextView) findViewById(R.id.textView);
mAddress = (TextView) findViewById(R.id.textView2);
mAttributions = (TextView) findViewById(R.id.textView3);
Button pickerButton = (Button) findViewById(R.id.pickerButton);
pickerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
PlacePicker.IntentBuilder intentBuilder =
new PlacePicker.IntentBuilder();
intentBuilder.setLatLngBounds(BOUNDS_MOUNTAIN_VIEW);
Intent intent = intentBuilder.build(MapActivity.this);
startActivityForResult(intent, PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException
| GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
});
}
public void moveToInternalChoiceActivity(View view) {
Intent intent = new Intent(this, InternalChoiceActivity.class);
startActivity(intent);
}
#Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST
&& resultCode == Activity.RESULT_OK) {
final Place place = PlacePicker.getPlace(this, data);
final CharSequence name = place.getName();
final CharSequence address = place.getAddress();
String attributions = (String) place.getAttributions();
if (attributions == null) {
attributions = "";
}
mName.setText(name);
mAddress.setText(address);
mAttributions.setText(Html.fromHtml(attributions));
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}

How to save image into gallery Android ap

I'm making an android application, when clicking on a button it opens the camera however after taking the picture the image doesn't save. I want to be able to save it into my gallery album.
public void onClickbtnCamera(View v){
Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uriSavedImage=Uri.fromFile(new File("/storage/emulated/0/DCIM/Camera/"));
intent1.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(intent1,3);
}
You are passing in a directory in EXTRA_OUTPUT. EXTRA_OUTPUT needs to point to a file:
package com.commonsware.android.camcon;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;
public class CameraContentDemoActivity extends Activity {
private static final int CONTENT_REQUEST=1337;
private File output=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File dir=
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
output=new File(dir, "CameraContentDemo.jpeg");
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(i, CONTENT_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == CONTENT_REQUEST) {
if (resultCode == RESULT_OK) {
Intent i=new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(output), "image/jpeg");
startActivity(i);
finish();
}
}
}
}
(from this sample project)

why is the below code force closes on using built in camera in android project?

package com.example.thenewjay;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
public class Camera extends Activity implements View.OnClickListener {
ImageView iv;
Button but;
ImageButton ib;
final static int cameraData = 0;
Intent i;
Bitmap bmp;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
initialize();
InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
bmp = BitmapFactory.decodeStream(is);
}
private void initialize() {
iv = (ImageView) findViewById(R.id.ivReturnedPic);
ib = (ImageButton) findViewById(R.id.ibTakePic);
but = (Button) findViewById(R.id.bSetWall);
but.setOnClickListener(this);
ib.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bSetWall:
try {
getApplicationContext().setWallpaper(bmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.ibTakePic:
i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 1);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
iv.setImageBitmap(bmp);
}}
}
why is the below code force closes on using built in camera in android
project?
Because you are missing setContentView in onCreate before calling initialize().
You will need to set layout for current Activity before calling findViewById to access views from current screen xml.do it as in onCreate:
...
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout_name);
initialize();
....

Menu buttons work, Soft buttons not working. Same Code basically

Title says it all, Im new to SQL and trying to change the selection the user makes but placing buttons in the screen and not use the MENU button. Seems like the buttons aren't instantiated but the code looks right to me...what am i missing??
package com.example.worldcountriesbooks;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ViewCountry extends Activity implements OnClickListener{
private long rowID;
private TextView nameTv;
private TextView capTv;
private TextView codeTv;
private TextView newEt;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.view_country);
Button a = (Button)findViewById(R.id.editbutton);
Button b = (Button)findViewById(R.id.deletebutton);
a.setOnClickListener(this);
b.setOnClickListener(this); //Set them up right here...
setUpViews();
Bundle extras = getIntent().getExtras();
rowID = extras.getLong(CountryList.ROW_ID);
}
private void setUpViews() {
nameTv = (TextView) findViewById(R.id.nameText);
capTv = (TextView) findViewById(R.id.capText);
codeTv = (TextView) findViewById(R.id.codeText);
newEt = (TextView)findViewById(R.id.newText);
}
#Override
protected void onResume()
{
super.onResume();
new LoadContacts().execute(rowID);
}
private class LoadContacts extends AsyncTask<Long, Object, Cursor>
{
DatabaseConnector dbConnector = new DatabaseConnector(ViewCountry.this);
#Override
protected Cursor doInBackground(Long... params)
{
dbConnector.open();
return dbConnector.getOneContact(params[0]);
}
#Override
protected void onPostExecute(Cursor result)
{
super.onPostExecute(result);
result.moveToFirst();
// get the column index for each data item
int nameIndex = result.getColumnIndex("name");
int capIndex = result.getColumnIndex("cap");
int codeIndex = result.getColumnIndex("code");
int newIndex = result.getColumnIndex("newb");
nameTv.setText(result.getString(nameIndex));
capTv.setText(result.getString(capIndex));
codeTv.setText(result.getString(codeIndex));
newEt.setText(result.getString(newIndex));
result.close();
dbConnector.close();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.view_country_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.editItem:
Intent addEditContact =
new Intent(this, AddEditCountry.class);
addEditContact.putExtra(CountryList.ROW_ID, rowID);
addEditContact.putExtra("name", nameTv.getText());
addEditContact.putExtra("cap", capTv.getText());
addEditContact.putExtra("code", codeTv.getText());
addEditContact.putExtra("newb", newEt.getText());
startActivity(addEditContact);
return true;
case R.id.deleteItem:
deleteContact();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void deleteContact()
{
AlertDialog.Builder alert = new AlertDialog.Builder(ViewCountry.this);
alert.setTitle(R.string.confirmTitle);
alert.setMessage(R.string.confirmMessage);
alert.setPositiveButton(R.string.delete_btn,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int button)
{
final DatabaseConnector dbConnector =
new DatabaseConnector(ViewCountry.this);
AsyncTask<Long, Object, Object> deleteTask =
new AsyncTask<Long, Object, Object>()
{
#Override
protected Object doInBackground(Long... params)
{
dbConnector.deleteContact(params[0]);
return null;
}
#Override
protected void onPostExecute(Object result)
{
finish();
}
};
deleteTask.execute(new Long[] { rowID });
}
}
);
alert.setNegativeButton(R.string.cancel_btn, null).show();
}
public void onClick(View arg0) {
switch (arg0.getId())
{
case R.id.editItem:
Intent addEditContact =
new Intent(this, AddEditCountry.class);
addEditContact.putExtra(CountryList.ROW_ID, rowID);
addEditContact.putExtra("name", nameTv.getText());
addEditContact.putExtra("cap", capTv.getText());
addEditContact.putExtra("code", codeTv.getText());
addEditContact.putExtra("newb", newEt.getText());
startActivity(addEditContact);
break;
case R.id.deleteItem:
deleteContact();
break;//finish them up here and they do nothing...
}
}
}
Now the menu buttons work great so not sure whats up...Thanks for looking
The menu buttons work great because the switch statement for it is proper.
Your onClick is not working properly however. This is because The cases are for different ids than what the buttons provide. You want R.id.editbutton and R.id.deletebutton instead.
Your method should look like:
public void onClick(View arg0) {
switch (arg0.getId())
{
case R.id.editbutton: //updated
Intent addEditContact =
new Intent(this, AddEditCountry.class);
addEditContact.putExtra(CountryList.ROW_ID, rowID);
addEditContact.putExtra("name", nameTv.getText());
addEditContact.putExtra("cap", capTv.getText());
addEditContact.putExtra("code", codeTv.getText());
addEditContact.putExtra("newb", newEt.getText());
startActivity(addEditContact);
break;
case R.id.deletebutton: //updated
deleteContact();
break;
}
}
}

Categories