I have a problem, that I can't fix. I have spent almost all day today trying to get this to work, but I can't and I am out of any ideas, so decided to ask for your help.
Also, I have looked around google and stackoverflow for answers, none of which helped me.
The problem regards photo capturing with android. I have an activity, where I can "create" a new item that is later on stored in a database. I can pass it a name and location, as well as take a photo that binds to it. When I press "add" it is saved to the database. Everything works, except the Camera intent. I want to be able to take a photo in that activity, that binds to the "object" I'm creating, so later on I can view that photo when I click on a particular "object" on a list. I do have permissions (write and read) in my AndroidManifest, but still they dont help.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="18"
/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18"
/>
<uses-feature android:name="android.hardware.camera"
android:required="false"
/>
My android version is 6.0.
This code is the most recent compilation of solutions that I have gathered to try to make it work. I would very much appreciate your help with my issue.
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class CreateItemFragment extends Fragment {
private static final int REQUEST_PHOTO = 2;
private static final String TAG = "button clicked";
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
private TextView mNameText;
private TextView mLocationText;
private Button mAddButton;
private ImageButton mPhotoButton;
private ImageView mPhotoView;
private String mCurrentPhotoPath;
private File mPhotoFile;
private Item mItem;
private List<Item> mItems;
private static ItemStash sItemStash;
public static CreateItemFragment newInstance(){
return new CreateItemFragment();
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_create_item, container, false);
mNameText = (TextView)view.findViewById(R.id.item_name);
mLocationText = (TextView)view.findViewById(R.id.item_location);
sItemStash = sItemStash.get(getContext());
mAddButton = (Button)view.findViewById(R.id.add_button);
mAddButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mNameText.getText().length() > 0 && mLocationText.getText().length() > 0){
sItemStash.addItem(
new Item(mNameText.getText().toString(),
mLocationText.getText().toString()));
Toast.makeText(getActivity(), mNameText.getText().toString() + " added to the list!", Toast.LENGTH_SHORT).show();
}
mNameText.setText("");
mLocationText.setText("");
}
});
mPhotoButton = (ImageButton)view.findViewById(R.id.photo_button);
//final Intent captureImage = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
/*
PackageManager packageManager = getActivity().getPackageManager();
boolean canTakePhoto = mPhotoFile != null && captureImage.resolveActivity(packageManager) != null;
mPhotoButton.setEnabled(canTakePhoto);
if(canTakePhoto) {
Uri uri = Uri.fromFile(mPhotoFile);
captureImage.putExtra(MediaStore.EXTRA_OUTPUT, uri);
}
*/
mPhotoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
verifyStoragePermissions(getActivity());
capturePhotoIntent();
//startActivityForResult(captureImage, REQUEST_PHOTO);
//mPhotoFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString());
//mPhotoFile = new File(mItem.setPhotoFilename(mNameText.getText().toString()));
}
});
mPhotoView = (ImageView)view.findViewById(R.id.item_photo);
//updatePhotoView();
return view;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == REQUEST_PHOTO && resultCode == Activity.RESULT_OK){
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap)extras.get("data");
//mPhotoFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString());
mPhotoView.setImageBitmap(imageBitmap);
}
}
/*
private void updatePhotoView(){
if(mPhotoFile == null || !mPhotoFile.exists()){
Log.d(TAG, "is clicked");
mPhotoView.setImageDrawable(null);
}else{
Bitmap bitmap = PhotoUtils.getScaledBitmap(mPhotoFile.getPath(), getActivity());
mPhotoView.setImageBitmap(bitmap);
}
}
*/
private void capturePhotoIntent(){
Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
PackageManager packageManager = getActivity().getPackageManager();
if(photoIntent.resolveActivity(packageManager) != null){
File photoFile = null;
try{
photoFile = createImageFile();
}catch (IOException ioe){
ioe.printStackTrace();
}
if(photoFile != null) {
photoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(photoIntent, REQUEST_PHOTO);
}
}
}
public File createImageFile() throws IOException {
verifyStoragePermissions(getActivity());
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName,
".jpg",
storageDir
);
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
public static void verifyStoragePermissions(Activity activity) {
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
}
*EDIT
Forgot to paste in my stack trace. I do get this:
04-21 22:34:56.688 3574-3604/com.bignerdranch.android.tingleapp
W/OpenGLRenderer: Fail to change FontRenderer cache size, it already initialized
04-21 22:34:56.704 3574-3574/com.bignerdranch.android.tingleapp D/android.widget.GridLayout: horizontal constraints: x1-x0>=163, x2-x1>=1080, x2-x0<=1080 are inconsistent; permanently removing: x2-x0<=1080.
04-21 22:34:56.708 3574-3574/com.bignerdranch.android.tingleapp D/android.widget.GridLayout: vertical constraints: y2-y0>=745, y2-y1<=136, y1-y0<=136 are inconsistent; permanently removing: y2-y1<=136.
04-21 22:34:56.819 3574-3604/com.bignerdranch.android.tingleapp E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb8767340
04-21 22:34:57.533 3574-3574/com.bignerdranch.android.tingleapp W/System.err: java.io.IOException: open failed: EACCES (Permission denied)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err: at java.io.File.createNewFile(File.java:939)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err: at java.io.File.createTempFile(File.java:1004)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err: at com.bignerdranch.android.tingleapp.CreateItemFragment.createImageFile(CreateItemFragment.java:173)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err: at com.bignerdranch.android.tingleapp.CreateItemFragment.capturePhotoIntent(CreateItemFragment.java:154)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err: at com.bignerdranch.android.tingleapp.CreateItemFragment.access$300(CreateItemFragment.java:30)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err: at com.bignerdranch.android.tingleapp.CreateItemFragment$2.onClick(CreateItemFragment.java:108)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err: at android.view.View.performClick(View.java:5226)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err: at android.view.View$PerformClick.run(View.java:21266)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err: at android.os.Looper.loop(Looper.java:168)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5845)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
04-21 22:34:57.539 3574-3574/com.bignerdranch.android.tingleapp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
04-21 22:34:57.539 3574-3574/com.bignerdranch.android.tingleapp W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
04-21 22:34:57.539 3574-3574/com.bignerdranch.android.tingleapp W/System.err: at libcore.io.Posix.open(Native Method)
04-21 22:34:57.539 3574-3574/com.bignerdranch.android.tingleapp W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
04-21 22:34:57.539 3574-3574/com.bignerdranch.android.tingleapp W/System.err: at java.io.File.createNewFile(File.java:932)
04-21 22:34:57.539 3574-3574/com.bignerdranch.android.tingleapp W/System.err: ... 14 more
the method verisfyStoragePermission() was supposed to fix that error as I found in other topics, but I still get this error
Related
I am trying to build a music recording app and the recording and stopping part is working, but it looks like the app cannot play the recorded file because it throws this error:
java.io.FileNotFoundException: /storage/emulated/0/CKENCAudioRecording.3gp: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:492)
W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:160)
W/System.err: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1259)
W/System.err: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1230)
W/System.err: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1195)
W/System.err: at com.example.karaokebuddies.record$3.onClick(record.java:99)
W/System.err: at android.view.View.performClick(View.java:8160)
W/System.err: at android.widget.TextView.performClick(TextView.java:16193)
W/System.err: at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
W/System.err: at android.view.View.performClickInternal(View.java:8137)
W/System.err: at android.view.View.access$3700(View.java:888)
W/System.err: at android.view.View$PerformClick.run(View.java:30236)
W/System.err: at android.os.Handler.handleCallback(Handler.java:938)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err: at android.os.Looper.loop(Looper.java:246)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:8462)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:596)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.Linux.open(Native Method)
W/System.err: at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
W/System.err: at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
W/System.err: at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:8323)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:478)
The error is referring to this line inside the play.setOnClicklistener.
mediaPlayer.setDataSource(AudioSavePathInDevice);
//AudioSavePathInDevice = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + CreateRandomAudioFileName(5) + "AudioRecording.3gp";
I've searched through a lot of similar cases in stack overflow, but none really helped.
I included necessary permissions and set requestLegacyExternalStorage to true:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
android:requestLegacyExternalStorage="true"
Here's my record.java:
package com.example.karaokebuddies;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.io.IOException;
import java.util.Random;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
public class record extends AppCompatActivity {
private Button play, stop, record, stopPlaying;
private MediaRecorder myAudioRecorder;
private String outputFile;
private String AudioSavePathInDevice = null;
Random random;
String RandomAudioFileName = "ABCDEFGHIJKLMNOP";
public static final int RequestPermissionCode = 1;
private MediaPlayer mediaPlayer;
boolean isRecording = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.record);
play = (Button) findViewById(R.id.play);
stop = (Button) findViewById(R.id.stop);
record = (Button) findViewById(R.id.record);
stopPlaying = (Button) findViewById(R.id.button_stopPlaying);
stop.setEnabled(false);
play.setEnabled(false);
stopPlaying.setEnabled(false);
random = new Random();
record.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (checkPermission()) {
AudioSavePathInDevice = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + CreateRandomAudioFileName(5) + "AudioRecording.3gp";
MediaRecorderReady();
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
isRecording = true;
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
record.setEnabled(false);
stop.setEnabled(true);
Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
} else {
requestPermission();
}
}
});
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isRecording) {
myAudioRecorder.stop();
}
myAudioRecorder.release();
isRecording = false;
stop.setEnabled(false);
play.setEnabled(true);
record.setEnabled(true);
stopPlaying.setEnabled(false);
Toast.makeText(getApplicationContext(), "Audio Recorded successfully", Toast.LENGTH_LONG).show();
}
});
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) throws IllegalArgumentException, SecurityException, IllegalStateException {
// MediaPlayer mediaPlayer = new MediaPlayer();
stop.setEnabled(false);
record.setEnabled(false);
stopPlaying.setEnabled(true);
mediaPlayer = new MediaPlayer();
try {
// mediaPlayer.setDataSource(outputFile);
mediaPlayer.setDataSource(AudioSavePathInDevice);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
Toast.makeText(getApplicationContext(), "Playing Audio", Toast.LENGTH_LONG).show();
}
});
stopPlaying.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stop.setEnabled(false);
record.setEnabled(true);
stopPlaying.setEnabled(false);
play.setEnabled(true);
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
MediaRecorderReady();
}
}
});
}
public void MediaRecorderReady() {
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(AudioSavePathInDevice);
}
public String CreateRandomAudioFileName(int string) {
StringBuilder stringBuilder = new StringBuilder(string);
int i = 0;
while (i < string) {
stringBuilder.append(RandomAudioFileName.charAt(random.nextInt(RandomAudioFileName.length())));
i ++;
}
return stringBuilder.toString();
}
private void requestPermission() {
ActivityCompat.requestPermissions(record.this, new String[]{WRITE_EXTERNAL_STORAGE, RECORD_AUDIO}, RequestPermissionCode);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case RequestPermissionCode:
if (grantResults.length > 0) {
boolean StoragePermission = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean RecordPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED;
if (StoragePermission && RecordPermission) {
Toast.makeText(record.this, "Permission Granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(record.this, "Permission Denied", Toast.LENGTH_LONG).show();
}
}
break;
}
}
public boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(getApplicationContext(), WRITE_EXTERNAL_STORAGE);
int result1 = ContextCompat.checkSelfPermission(getApplicationContext(), RECORD_AUDIO);
return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED;
}
}
As I was searching for a solution, I was wondering if the cause of this error is because I own a Galaxy s21+, which does not have an external storage, and I'm trying to save the recorded file into external storage directory. If so, would there be a solution to my situation? (i.e. saving to an internal storage?). Or, is there something else that I'm doing wrong?
Thank you in advance for your support!
First of all, when Android references "External Storage", it doesn't refer to an SD card. From this link:
"Categories of storage locations
Android provides two types of physical storage locations: internal storage and external storage. On most devices, internal storage is smaller than external storage. However, internal storage is always available on all devices, making it a more reliable place to put data on which your app depends."
The File Not Found could be related to two issues:
You do not have the correct permissions
The folder or file doesn't exist
If you review the error in your Logcat, you can usually determine the specific details. Also, using a File app on your device, you should be able to see if file with that name actually does in that folder.
Double-check to ensure that the permissions have been granted/accepted.
What level SDK are you targeting? If you have targetSdkVersion 30 in your app's build.gradle file, you need to update your code to adhere to a recent policy change that Google made.
If you aren't sure if the directory exists, you can use code such as this to create it:
final File root = new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)));
// Create the directory if it doesn't exist
if (!root.exists()) {
boolean wasSuccessful = root.mkdirs();
// Display Toast message if the directory creation wasn't successful
if (!wasSuccessful) {
mToast = Toast.makeText(this, R.string.photo_directory_not_created_error, Toast.LENGTH_SHORT);
mToast.show();
}
}
I know there are a few of these on SO already but none of them really were able to help my issue, but when I am running the code and start recording audio and then press my stop button it always fails because it is in the wrong state. I am not sure how I would go about fixing my states for this.
Here is my MainActivity.java code:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.io.IOException;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
public class MainActivity extends AppCompatActivity {
Button buttonStartRecording, buttonStopRecording, buttonPlayLastRecordAudio,
buttonStopPlayingRecording;
String AudioSavePathInDevice = null;
MediaRecorder mediaRecorder;
public static final int RequestPermissionCode = 1;
MediaPlayer mediaPlayer;
AudioManager audioManager;
boolean isAudioPlayInSameDevice = true;
// AudioRouter audioRouter;
RadioGroup mRadioGroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonStartRecording = (Button) findViewById(R.id.start_recording);
buttonStopRecording = (Button) findViewById(R.id.stop_rec);
buttonPlayLastRecordAudio = (Button) findViewById(R.id.play_last_rec);
buttonStopPlayingRecording = (Button) findViewById(R.id.stop_playing_btn);
// mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
buttonStopRecording.setEnabled(false);
buttonPlayLastRecordAudio.setEnabled(false);
buttonStopPlayingRecording.setEnabled(false);
buttonStartRecording.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
public void onClick(View view) {
// Check audio permission
if (checkPermission()) {
AudioSavePathInDevice =
Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "AudioRecording.3gp";
// Start Media recorder
MediaRecorderReady();
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
buttonStartRecording.setEnabled(false);
buttonStopRecording.setEnabled(true);
Toast.makeText(MainActivity.this, "Recording started",
Toast.LENGTH_LONG).show();
} else {
requestPermission();
}
}
});
buttonStopRecording.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonStopRecording.setEnabled(false);
buttonPlayLastRecordAudio.setEnabled(true);
buttonStartRecording.setEnabled(true);
buttonStopPlayingRecording.setEnabled(false);
// Stop Media recorder
mediaRecorder.stop();
Toast.makeText(MainActivity.this, "Recording Completed",
Toast.LENGTH_LONG).show();
}
});
buttonPlayLastRecordAudio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) throws IllegalArgumentException,
SecurityException, IllegalStateException {
int selectedId = 1;
if (selectedId == 1) {
isAudioPlayInSameDevice = true;
} else {
isAudioPlayInSameDevice = false;
}
// if you want to play audio on your Mobile speaker then set isAudioPlayInSameDevice true
// and if you want to play audio to connected device then set isAudioPlayInSameDevice false.
if (isAudioPlayInSameDevice) {
audioManager.setMode(audioManager.STREAM_MUSIC);
audioManager.setSpeakerphoneOn(true);
} else {
audioManager.setSpeakerphoneOn(false);
audioManager.setMode(audioManager.MODE_NORMAL);
}
audioManager.setBluetoothScoOn(false);
audioManager.stopBluetoothSco();
buttonStopRecording.setEnabled(false);
buttonStartRecording.setEnabled(false);
buttonStopPlayingRecording.setEnabled(true);
mediaPlayer = new MediaPlayer();
try {
// Start media player
System.out.println("Recorded Audio Path-" + AudioSavePathInDevice);
mediaPlayer.setDataSource(AudioSavePathInDevice);
if (isAudioPlayInSameDevice) {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "Recording Playing",
Toast.LENGTH_LONG).show();
}
});
buttonStopPlayingRecording.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonStopRecording.setEnabled(false);
buttonStartRecording.setEnabled(true);
buttonStopPlayingRecording.setEnabled(false);
buttonPlayLastRecordAudio.setEnabled(true);
if (mediaPlayer != null) {
// Stop Media Player
mediaPlayer.stop();
mediaPlayer.release();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
MediaRecorderReady();
}
}
}
});
}
private BroadcastReceiver mBluetoothScoReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
System.out.println("ANDROID Audio SCO state: " + state);
if (AudioManager.SCO_AUDIO_STATE_CONNECTED == state) {
/*
* Now the connection has been established to the bluetooth device.
* Record audio or whatever (on another thread).With AudioRecord you can record with an object created like this:
* new AudioRecord(MediaRecorder.AudioSource.MIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
* AudioFormat.ENCODING_PCM_16BIT, audioBufferSize);
*
* After finishing, don't forget to unregister this receiver and
* to stop the bluetooth connection with am.stopBluetoothSco();
*/
}
}
};
#RequiresApi(api = Build.VERSION_CODES.O)
public void MediaRecorderReady() {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_2_TS);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setOutputFile(AudioSavePathInDevice);
}
#Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
registerReceiver(mBluetoothScoReceiver, intentFilter);
audioManager = (AudioManager) getApplicationContext().getSystemService(getApplicationContext().AUDIO_SERVICE);
// Start Bluetooth SCO.
audioManager.setMode(audioManager.MODE_NORMAL);
audioManager.setBluetoothScoOn(true);
audioManager.startBluetoothSco();
// Stop Speaker.
audioManager.setSpeakerphoneOn(false);
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mBluetoothScoReceiver);
// Stop Bluetooth SCO.
audioManager.stopBluetoothSco();
audioManager.setMode(audioManager.MODE_NORMAL);
audioManager.setBluetoothScoOn(false);
// Start Speaker.
audioManager.setSpeakerphoneOn(true);
}
private void requestPermission() {
ActivityCompat.requestPermissions(MainActivity.this, new
String[]{WRITE_EXTERNAL_STORAGE, RECORD_AUDIO}, RequestPermissionCode);
}
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case RequestPermissionCode:
if (grantResults.length > 0) {
boolean StoragePermission = grantResults[0] ==
PackageManager.PERMISSION_GRANTED;
boolean RecordPermission = grantResults[1] ==
PackageManager.PERMISSION_GRANTED;
// if (StoragePermission && RecordPermission) {
// Toast.makeText(BluetoothAudioRecorder.this, "Permission Granted",
// Toast.LENGTH_LONG).show();
// } else {
// Toast.makeText(BluetoothAudioRecorder.this,"Permission Denied",Toast.LENGTH_LONG).show();
// }
}
break;
}
}
public boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(getApplicationContext(),
WRITE_EXTERNAL_STORAGE);
int result1 = ContextCompat.checkSelfPermission(getApplicationContext(),
RECORD_AUDIO);
return result == PackageManager.PERMISSION_GRANTED &&
result1 == PackageManager.PERMISSION_GRANTED;
}
}
And then my logcat of when the error occurs:
2019-11-24 11:26:54.440 29627-29683/com.example.esense_application E/libc: Access denied finding property "vendor.gralloc.disable_ahardware_buffer"
2019-11-24 11:26:54.435 29627-29627/com.example.esense_application W/RenderThread: type=1400 audit(0.0:17779): avc: denied { read } for name="u:object_r:vendor_default_prop:s0" dev="tmpfs" ino=24699 scontext=u:r:untrusted_app:s0:c7,c257,c512,c768 tcontext=u:object_r:vendor_default_prop:s0 tclass=file permissive=0
2019-11-24 11:26:54.487 29627-29627/com.example.esense_application I/System.out: ANDROID Audio SCO state: 1
2019-11-24 11:26:54.487 29627-29627/com.example.esense_application I/System.out: ANDROID Audio SCO state: 2
2019-11-24 11:26:54.868 29627-29627/com.example.esense_application I/System.out: ANDROID Audio SCO state: 1
2019-11-24 11:26:57.769 29627-29627/com.example.esense_application I/System.out: ANDROID Audio SCO state: 1
2019-11-24 11:27:00.647 29627-29627/com.example.esense_application W/System.err: java.io.FileNotFoundException: /storage/emulated/0/AudioRecording.3gp: open failed: EACCES (Permission denied)
2019-11-24 11:27:00.647 29627-29627/com.example.esense_application W/System.err: at libcore.io.IoBridge.open(IoBridge.java:496)
2019-11-24 11:27:00.647 29627-29627/com.example.esense_application W/System.err: at java.io.RandomAccessFile.<init>(RandomAccessFile.java:289)
2019-11-24 11:27:00.647 29627-29627/com.example.esense_application W/System.err: at java.io.RandomAccessFile.<init>(RandomAccessFile.java:152)
2019-11-24 11:27:00.647 29627-29627/com.example.esense_application W/System.err: at android.media.MediaRecorder.prepare(MediaRecorder.java:1046)
2019-11-24 11:27:00.647 29627-29627/com.example.esense_application W/System.err: at com.example.esense_application.MainActivity$1.onClick(MainActivity.java:67)
2019-11-24 11:27:00.647 29627-29627/com.example.esense_application W/System.err: at android.view.View.performClick(View.java:7140)
2019-11-24 11:27:00.647 29627-29627/com.example.esense_application W/System.err: at android.view.View.performClickInternal(View.java:7117)
2019-11-24 11:27:00.647 29627-29627/com.example.esense_application W/System.err: at android.view.View.access$3500(View.java:801)
2019-11-24 11:27:00.647 29627-29627/com.example.esense_application W/System.err: at android.view.View$PerformClick.run(View.java:27351)
2019-11-24 11:27:00.647 29627-29627/com.example.esense_application W/System.err: at android.os.Handler.handleCallback(Handler.java:883)
2019-11-24 11:27:00.648 29627-29627/com.example.esense_application W/System.err: at android.os.Handler.dispatchMessage(Handler.java:100)
2019-11-24 11:27:00.648 29627-29627/com.example.esense_application W/System.err: at android.os.Looper.loop(Looper.java:214)
2019-11-24 11:27:00.648 29627-29627/com.example.esense_application W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7356)
2019-11-24 11:27:00.648 29627-29627/com.example.esense_application W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2019-11-24 11:27:00.648 29627-29627/com.example.esense_application W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
2019-11-24 11:27:00.648 29627-29627/com.example.esense_application W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2019-11-24 11:27:00.648 29627-29627/com.example.esense_application W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
2019-11-24 11:27:00.648 29627-29627/com.example.esense_application W/System.err: at libcore.io.Linux.open(Native Method)
2019-11-24 11:27:00.648 29627-29627/com.example.esense_application W/System.err: at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
2019-11-24 11:27:00.648 29627-29627/com.example.esense_application W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252)
2019-11-24 11:27:00.648 29627-29627/com.example.esense_application W/System.err: at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
2019-11-24 11:27:00.648 29627-29627/com.example.esense_application W/System.err: at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7255)
2019-11-24 11:27:00.648 29627-29627/com.example.esense_application W/System.err: at libcore.io.IoBridge.open(IoBridge.java:482)
2019-11-24 11:27:00.648 29627-29627/com.example.esense_application W/System.err: ... 15 more
2019-11-24 11:27:03.528 29627-29627/com.example.esense_application E/MediaRecorder: stop called in an invalid state: 4
2019-11-24 11:27:03.528 29627-29627/com.example.esense_application D/AndroidRuntime: Shutting down VM
2019-11-24 11:27:03.529 29627-29627/com.example.esense_application E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.esense_application, PID: 29627
java.lang.IllegalStateException
at android.media.MediaRecorder.stop(Native Method)
at com.example.esense_application.MainActivity$2.onClick(MainActivity.java:98)
at android.view.View.performClick(View.java:7140)
at android.view.View.performClickInternal(View.java:7117)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27351)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2019-11-24 11:27:03.540 29627-29627/com.example.esense_application I/Process: Sending signal. PID: 29627 SIG: 9
Your MediaRecorder is throwing when you try to stop() it because it has never entered the "recording" state. In fact, it never even entered the "prepared" state, because your call to prepare() did not complete successfully.
SOLUTION
Do not allow a call to start() until the prepare() call has returned (without throwing).
Do not allow a call to stop() until the start() call has returned (without throwing).
Make sure you have chosen a valid location for your output file. Try using getExternalFilesDir( null ) instead of Environment.getExternalStorageDirectory(). The bad file path is the reason your prepare() call is currently failing with an EACCESS.
As you can see, swallowing exceptions without addressing their root cause (i.e., just doing a e.printStackTrace() and carrying on), can rapidly lead to problems -- even in rough code used for learning/experimentation. If you are going to add an exception handler, it is better to provide real error handling -- and always make sure you understand why an exception is being thrown.
I am new on android and working on my android app's login activity for which em using php mysql with volley library. But every time I run my app on emulator it shows the message Unfortunately, app has stopped. Here the login activity code is:
package com.example.u.locationtracker;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private EditText pass1, email1;
private Button login;
private TextView link_reg;
private ProgressBar loading;
private String URL_LOGIN= "http://192.168.1.1/register.php";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
email1= (EditText) findViewById(R.id.etemail1);
pass1= (EditText) findViewById(R.id.etpassl);
loading= (ProgressBar) findViewById(R.id.loading1);
link_reg= (TextView) findViewById(R.id.signup);
login= (Button) findViewById(R.id.btnlogin);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String mEmail= email1.getText().toString().trim();
String mpass= pass1.getText().toString().trim();
if(!mEmail.isEmpty() || !mpass.isEmpty()){
Login(mEmail, mpass);
}else{
email1.setError("Please Enter Email...!");
pass1.setError("Please Enter Password...!");
}
}
});
link_reg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent it= new Intent(MainActivity.this, Register.class);
startActivity(it);
}
});
}
private void Login(final String email, final String pass) {
loading.setVisibility(View.VISIBLE);
login.setVisibility(View.GONE);
StringRequest stringRequest= new StringRequest(Request.Method.POST, URL_LOGIN,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try{
JSONObject jsonObject= new JSONObject(response);
String success= jsonObject.getString("Success");
JSONArray jsonArray= jsonObject.getJSONArray("Login");
if(success.equals("1")){
for (int i= 0; i < jsonArray.length(); i++){
JSONObject object= jsonArray.getJSONObject(i);
Toast t= Toast.makeText(MainActivity.this,
"Login Successful", Toast.LENGTH_LONG);
t.show();
loading.setVisibility(View.GONE);
}
}
}catch (JSONException e) {
e.printStackTrace();
loading.setVisibility(View.GONE);
login.setVisibility(View.VISIBLE);
Toast t1= Toast.makeText(MainActivity.this,
"Error" + e.toString(),
Toast.LENGTH_LONG);
t1.show();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
loading.setVisibility(View.GONE);
login.setVisibility(View.VISIBLE);
Toast t2= Toast.makeText(MainActivity.this,
"Error" + error.toString(),
Toast.LENGTH_LONG);
t2.show();
}
})
{
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params= new HashMap<>();
params.put("Email", email);
params.put("Password", pass);
return params;
}
};
RequestQueue requestQueue= Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
According to logcat the problem is:
at com.example.u.locationtracker.MainActivity.onCreate(MainActivity.java:39)
here the php code:
<?php
if ($_SERVER['REQUEST_METHOD']=='POST') {
$email= $_POST['email1'];
$pass= $_POST['pass1'];
require_once 'connect.php';
$select= "SELECT * FROM user_table WHERE Email= '$email' ";
$r= mysqli_query($conn, $select);
$result= array();
$result['login']= array();
if (mysqli_num_rows($r)=== 1) {
$row= mysqli_fetch_assoc($r);
if ( password_verify($pass, $row['Pass']) ) {
$index['Name']= $row['Name'];
$index['Email']= $row['Email'];
array_push($result['login'], $index);
$result['success']= "1";
$result['message']= "Success";
echo json_encode($result);
mysql_close($conn);
}else{
$result['success']= "0";
$result['message']= "Error";
echo json_encode($result);
mysql_close($conn);
}
}
}
?>
Here is the Logcat:
02-03 21:03:21.626 2006-2012/? E/jdwp: Failed writing handshake bytes: Broken pipe (-1 of 14)
02-03 21:03:21.806 2006-2006/? E/dalvikvm: Could not find class 'android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper', referenced from method android.support.v4.view.ViewCompat.addOnUnhandledKeyEventListener
02-03 21:03:21.806 2006-2006/? E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method android.support.v4.view.ViewCompat.dispatchApplyWindowInsets
02-03 21:03:21.826 2006-2006/? E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method android.support.v4.view.ViewCompat.onApplyWindowInsets
02-03 21:03:21.826 2006-2006/? E/dalvikvm: Could not find class 'android.view.View$OnUnhandledKeyEventListener', referenced from method android.support.v4.view.ViewCompat.removeOnUnhandledKeyEventListener
02-03 21:03:21.836 2006-2006/? E/dalvikvm: Could not find class 'android.support.v4.view.ViewCompat$1', referenced from method android.support.v4.view.ViewCompat.setOnApplyWindowInsetsListener
02-03 21:03:22.756 2006-2006/com.example.u.locationtracker E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
02-03 21:03:27.786 2006-2006/com.example.u.locationtracker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.u.locationtracker, PID: 2006
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.u.locationtracker/com.example.u.locationtracker.MainActivity}: android.view.InflateException: Binary XML file line #45: Error inflating class ImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2193)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5019)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #45: Error inflating class ImageView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.u.locationtracker.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5019)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f07005d a=-1 r=0x7f07005d}
at android.content.res.Resources.loadDrawable(Resources.java:2068)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.widget.ImageView.<init>(ImageView.java:129)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:72)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:182)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.u.locationtracker.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5019)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
The problem seems to lie in this line:
setContentView(R.layout.activity_main);
Are you sure, the layout exists and doesn't have compile errors?
A full stacktrace would be more helpful
When I'm trying to run the app I get the exception which is shown in the Title.
This is my xml File:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.example.ruben.fileapp.MainActivity.PlayButton
android:id="#+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.example.ruben.fileapp.MainActivity.RecordButton
android:id="#+id/record_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
This is my MainActivity.java:
package com.example.ruben.fileapp;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = "AudioRecordTest";
private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
private static String mFileName = null;
private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;
private PlayButton mPlayButton = null;
private MediaPlayer mPlayer = null;
// Requesting permission to RECORD_AUDIO
private boolean permissionToRecordAccepted = false;
private String [] permissions = {Manifest.permission.RECORD_AUDIO};
//region permissionMethod
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case REQUEST_RECORD_AUDIO_PERMISSION:
permissionToRecordAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
break;
}
if (!permissionToRecordAccepted ) finish();
}
//endregion
private void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
private void onPlay(boolean start) {
if (start) {
startPlaying();
} else {
stopPlaying();
}
}
private void startPlaying() {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
private void stopPlaying() {
mPlayer.release();
mPlayer = null;
}
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
//region RecordButton
public class RecordButton extends AppCompatButton {
boolean mStartRecording = true;
OnClickListener clicker = new OnClickListener() {
public void onClick( View v) {
onRecord(mStartRecording);
if (mStartRecording) {
setText("Stop recording");
} else {
setText("Start recording");
}
mStartRecording = !mStartRecording;
}
};
public RecordButton(Context ctx) {
super(ctx);
setText("Start recording");
setOnClickListener(clicker);
}
}
//endregion
//region PlayButton
public class PlayButton extends AppCompatButton {
boolean mStartPlaying = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onPlay(mStartPlaying);
if (mStartPlaying) {
setText("Stop playing");
} else {
setText("Start playing");
}
mStartPlaying = !mStartPlaying;
}
};
public PlayButton(Context ctx) {
super(ctx);
setText("Start playing");
setOnClickListener(clicker);
}
}
//endregion
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Record to the external cache directory for visibility
mFileName = getExternalCacheDir().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION);
LinearLayout ll = new LinearLayout(this);
mRecordButton = (RecordButton) findViewById(R.id.record_button);
mPlayButton = (PlayButton) findViewById(R.id.play_button);
/*ll.addView(mRecordButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));*/
//mPlayButton = new PlayButton(this);
/*ll.addView(mPlayButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
setContentView(ll);*/
}
#Override
public void onStop() {
super.onStop();
if (mRecorder != null) {
mRecorder.release();
mRecorder = null;
}
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
}
This is my full logcat:
09-04 12:36:45.898 6198-6198/? I/art: Late-enabling -Xcheck:jni
Reinit property: dalvik.vm.checkjni= false
09-04 12:36:46.143 6198-6198/com.example.ruben.fileapp W/System: ClassLoader referenced unknown path: /data/app/com.example.ruben.fileapp-1/lib/arm64
09-04 12:36:46.156 6198-6198/com.example.ruben.fileapp I/InstantRun: starting instant run server: is main process
09-04 12:36:46.185 6198-6198/com.example.ruben.fileapp I/HwCust: Constructor found for class android.app.HwCustActivityImpl
09-04 12:36:46.210 6198-6198/com.example.ruben.fileapp I/HwCust: Constructor found for class android.app.HwCustHwWallpaperManagerImpl
09-04 12:36:46.235 6198-6198/com.example.ruben.fileapp W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
09-04 12:36:46.318 6198-6198/com.example.ruben.fileapp E/HW-JPEG-DEC: [HME_JPEG_DEC_Delete](3321): HME_JPEG_DEC_Delete: decoder_ctx=null
09-04 12:36:46.344 6198-6198/com.example.ruben.fileapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ruben.fileapp, PID: 6198
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ruben.fileapp/com.example.ruben.fileapp.MainActivity}: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class com.example.ruben.fileapp.MainActivity.PlayButton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
Caused by: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class com.example.ruben.fileapp.MainActivity.PlayButton
Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class com.example.ruben.fileapp.MainActivity.PlayButton
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.ruben.fileapp.MainActivity.PlayButton" on path: DexPathList[[zip file "/data/app/com.example.ruben.fileapp-1/base.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.ruben.fileapp-1/lib/arm64, /system/lib64, /vendor/lib64, /system/vendor/lib64, /product/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:616)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:798)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:738)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:869)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:832)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:872)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:832)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.ruben.fileapp.MainActivity.onCreate(MainActivity.java:158)
at android.app.Activity.performCreate(Activity.java:6910)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
09-04 12:36:46.353 6198-6198/com.example.ruben.fileapp I/Process: Sending signal. PID: 6198 SIG: 9
I would appreciate any kind of help.
Thank you
P.S.: This is my first question posted on stackoverflow, if something is bad in the way I'm asking, feel free to tell me.
You're calling MainActivity.RecordButton but the RecordButton isn't a subclass of MainActivity.
Your RecordButton and PlayButton are public classes, which means they are in their own files, probaly at the same level as the MainActivity.
try
<com.example.ruben.fileapp.PlayButton
android:id="#+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Make separate class for custom button instead of mainactivity
com.example.ruben.fileapp.MainActivity.PlayButton
move your PlayButton class into other location like
com.example.ruben.fileapp.PlayButton
Please make sure that android.enableJetifier=true exists in the gradle.properties it's not there by default anymore and you will face ambiguous exceptions when some library inside your code still uses the support libraries rather than AndroidX's.
Can someone give me a example of a right json file and php, so I can use it in the Android GET method? I think my files are wrong. This are the newest files regarding JSON and PHP
data.json
{
"Heroes":
[
{
"name":"Superman",
"about":"testdesc",
"image":"google.de/image.png"
},
{
"name":"Superman",
"about":"testdesc",
"image":"google.de/image.png"
},
{
"name":"Superman",
"about":"testdesc",
"image":"google.de/image.png"
},
{
"name":"Superman",
"about":"testdesc",
"image":"google.de/image.png"
}
]
}
data.php
<?php
$jsonData = file_get_contents("data.json");
$json = json_encode($jsonData,true);
header('Content-Type: application/json');
echo $json;
?>
I am new to Android App's coding.
May you can help me out, with a stupid problem.
I've tried to make an app from a youtube tutorial,
there it workes fine but I got several problems now I really need to fix.
The app is basicly an App that shows me a RecyclerViewCard with an Image, HeaderText and About text, the content is in a php file on my localhost.
<?php
$json2 = array(
"error" => "false",
"Heroes" => array(
'name' => 'Spiderman',
'about' => 'Spiderman ist ein Spinnenheld mit einem leckeren Schwanz',
'image' => 'www.google.de/info.png'
)
);
?>
So this is basicly the Json code to get it in my app, I hope this is right. If not please help me out with the right json code.
The error I get is following:
06-29 18:03:47.292 5249-5249/? I/art: Not late-enabling -Xcheck:jni (already on)
06-29 18:03:47.293 5249-5249/? W/art: Unexpected CPU variant for X86 using defaults: x86
06-29 18:03:47.714 5249-5249/de.example.kevin.superheroes W/System: ClassLoader referenced unknown path: /data/app/de.example.kevin.superheroes-2/lib/x86
06-29 18:03:47.764 5249-5249/de.example.kevin.superheroes I/InstantRun: starting instant run server: is main process
06-29 18:03:47.954 5249-5249/de.example.kevin.superheroes W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-29 18:03:48.574 5249-5287/de.example.kevin.superheroes D/NetworkSecurityConfig: No Network Security Config specified, using platform default
06-29 18:03:48.639 5249-5285/de.example.kevin.superheroes I/OpenGLRenderer: Initialized EGL, version 1.4
06-29 18:03:48.639 5249-5285/de.example.kevin.superheroes D/OpenGLRenderer: Swap behavior 1
06-29 18:03:48.647 5249-5285/de.example.kevin.superheroes W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
06-29 18:03:48.647 5249-5285/de.example.kevin.superheroes D/OpenGLRenderer: Swap behavior 0
06-29 18:03:48.949 5249-5249/de.example.kevin.superheroes E/RecyclerView: No adapter attached; skipping layout
06-29 18:03:49.168 5249-5249/de.example.kevin.superheroes W/System.err: org.json.JSONException: End of input at character 0 of
06-29 18:03:49.168 5249-5249/de.example.kevin.superheroes W/System.err: at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
06-29 18:03:49.168 5249-5249/de.example.kevin.superheroes W/System.err: at org.json.JSONTokener.nextValue(JSONTokener.java:97)
06-29 18:03:49.168 5249-5249/de.example.kevin.superheroes W/System.err: at org.json.JSONObject.<init>(JSONObject.java:156)
06-29 18:03:49.168 5249-5249/de.example.kevin.superheroes W/System.err: at org.json.JSONObject.<init>(JSONObject.java:173)
06-29 18:03:49.168 5249-5249/de.example.kevin.superheroes W/System.err: at de.example.kevin.superheroes.MainActivity$1.onResponse(MainActivity.java:60)
06-29 18:03:49.168 5249-5249/de.example.kevin.superheroes W/System.err: at de.example.kevin.superheroes.MainActivity$1.onResponse(MainActivity.java:55)
06-29 18:03:49.168 5249-5249/de.example.kevin.superheroes W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
06-29 18:03:49.168 5249-5249/de.example.kevin.superheroes W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
06-29 18:03:49.168 5249-5249/de.example.kevin.superheroes W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
06-29 18:03:49.169 5249-5249/de.example.kevin.superheroes W/System.err: at android.os.Handler.handleCallback(Handler.java:751)
06-29 18:03:49.169 5249-5249/de.example.kevin.superheroes W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
06-29 18:03:49.169 5249-5249/de.example.kevin.superheroes W/System.err: at android.os.Looper.loop(Looper.java:154)
06-29 18:03:49.169 5249-5249/de.example.kevin.superheroes W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6119)
06-29 18:03:49.169 5249-5249/de.example.kevin.superheroes W/System.err: at java.lang.reflect.Method.invoke(Native Method)
06-29 18:03:49.169 5249-5249/de.example.kevin.superheroes W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
06-29 18:03:49.169 5249-5249/de.example.kevin.superheroes W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
06-29 18:03:49.240 5249-5249/de.example.kevin.superheroes W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
The MainActivity:
package de.example.kevin.superheroes;
import android.app.LauncherActivity;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView = null;
private RecyclerView.Adapter adapter = null;
private List<ListItem> listItems = null;
private static final String URL_DATA = "http://192.168.178.29/android/data.php";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_layout);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
listItems = new ArrayList<>();
loadRecyclerViewData();
}
private void loadRecyclerViewData(){
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Content is loading ...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_DATA, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("Heroes");
for(int i = 0; i<array.length(); i++){
JSONObject o = array.getJSONObject(i);
ListItem item = new ListItem(
o.getString("name"),
o.getString("about"),
o.getString("image")
);
listItems.add(item);
}
adapter = new MyAdapter(listItems,getApplicationContext());
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
ListItem.java
package de.example.kevin.superheroes;
import android.widget.ImageView;
public class ListItem {
public ListItem(String img_url, String head, String desc) {
this.img_url = img_url;
this.head = head;
this.desc = desc;
}
private String img_url = null;
private String head = null;
private String desc = null;
public String getImg_url() {
return img_url;
}
public String getHead() {
return head;
}
public String getDesc() {
return desc;
}
}
MyAdapter
package de.example.kevin.superheroes;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<ListItem> listItems = null;
private Context context = null;
public MyAdapter(List<ListItem> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item,parent,false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
ListItem listItem = listItems.get(position);
holder.textViewHead.setText(listItem.getHead());
holder.textViewDesc.setText(listItem.getDesc());
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView textViewHead = null;
public TextView textViewDesc = null;
public ViewHolder(View itemView) {
super(itemView);
textViewHead = (TextView) itemView.findViewById(R.id.textViewHead);
textViewDesc = (TextView) itemView.findViewById(R.id.textViewDesc);
}
}
}
I would be so glad if you can help me !
If you have any question please ask.
Thanks
After editing my json code I get following error:
PHP
<?php
$json2 = array(
"error" => "false",
"Heroes" => array(
'name' => 'Spiderman',
'about' => 'Spiderman ist ein Spinnenheld mit einem ',
'image' => 'www.google.de/info.png'
)
);
echo json_encode($json2);
?>
No I get this huge errors :
I have postet it on pastebin because its to big for here:
https://pastebin.com/RAuNfZGm
Since it says, "org.json.JSONException: End of input at character 0". It essentially means that you are not returning anything from your php. Like Vaibhav said, you need to encode your array in json using json_encode() and echo the result.
Further Answer
You would need,
<?php
class Hero {
// Creating some properties
public $name;
public $about;
public $image;
// Assigning the values
public function __construct($name, $about, $image) {
$this->name = $name;
$this->about = $about;
$this->image = $image;
}
}
$array_heros[0] = new Hero('Spiderman', 'Spiderman-about', 'image_link');
$array_heros[1] = new Hero('Batman', 'Batman-about', 'image_link');
$json2 = array(
"error" => "false",
"Heroes" => $array_heros
);
echo json_encode($json2);
?>
In you php file its just creating array not json
to create json and return it use last line.
`
<?php
$json2 = array(
"error" => "false",
"Heroes" => array(
'name' => 'Spiderman',
'about' => 'Spiderman ist ein Spinnenheld mit einem leckeren Schwanz',
'image' => 'www.google.de/info.png'
)
);
echo json_encode($json2);
?>`