I have implemented an FTP Client on my app and trying to connect to an Existing FTP Server but don't know why it throws an error.
I am using this library in the build.gradle
implementationfiles('libs/apache-commons-net.jar')
Here is my code for FTP Client connection part
private void configFTPClient(String user, String password)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
FTPClient ftpClient = new FTPClient();
//final String serverFinal=server;
BufferedInputStream buffIn = null;
String fileNameDirectory="BPL_CARDIART";
File file =new File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath(),fileNameDirectory);
String firstRemoteFile =file+"/"+ "Cardiart__LPP1_ECG.pdf";
try {
buffIn = new BufferedInputStream(new FileInputStream(firstRemoteFile));
ftpClient.connect(server,port);
ftpClient.login(user, password);
// ftpClient.changeWorkingDirectory(serverRoad);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.storeFile("test.txt", buffIn);
buffIn.close();
ftpClient.logout();
ftpClient.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
And my maifest.xml file is code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cardiact.bpl.pkg.com.bplcardiactconnect"
>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
/>
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.usb.host" />
<uses-feature android:name="android.hardware.camera2" android:required="false"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:allowBackup="false"
android:icon="#mipmap/ic_launcher"
android:fullBackupContent="false"
android:label="#string/app_name"
android:hardwareAccelerated="true"
android:name="application.BaseApplicationClass"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SplashScreenActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".BaseLoginActivityClass"
android:launchMode="singleInstance"
android:configChanges="orientation|keyboardHidden|screenSize"
/>
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
</application>
</manifest>
As I already checked, connection with my wifi and internet on my phones, its working fine, but still it throws an error
java.net.UnknownHostException: Unable to resolve host "ftp://14.141.84.241/": No address associated with hostname
2018-10-15 11:58:40.669 2407-2407/cardiact.bpl.com.bplcardiactconnect W/System.err: at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:141)
2018-10-15 11:58:40.669 2407-2407/cardiact.bpl.com.bplcardiactconnect W/System.err: at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90)
2018-10-15 11:58:40.670 2407-2407/cardiact.bpl.com.bplcardiactconnect W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:787)
2018-10-15 11:58:40.670 2407-2407/cardiact.bpl.com.bplcardiactconnect W/System.err: at java.net.Socket.<init>(Socket.java:217)
2018-10-15 11:58:40.670 2407-2407/cardiact.bpl.com.bplcardiactconnect W/System.err: at org.apache.commons.net.DefaultSocketFactory.createSocket(DefaultSocketFactory.java:53)
2018-10-15 11:58:40.670 2407-2407/cardiact.bpl.com.bplcardiactconnect W/System.err: at org.apache.commons.net.SocketClient.connect(SocketClient.java:162)
Related
I'm appending two audio file using mp4parser. Appending is done but it is very slow don't know what is the issue.
On this line debugger stuck for big audio files for example approx 30 minutes audio file.
Container out = new DefaultMp4Builder().build(result);
Here is code.
Movie[] inMovies = null;
inMovies = new Movie[]{
MovieCreator.build(fileOne),
MovieCreator.build(fileTwo)};
List<Track> audioTracks = new LinkedList<Track>();
for (Movie m : inMovies) {
for (Track t : m.getTracks()) {
if (t.getHandler().equals("soun")) {
audioTracks.add(t);
}
}
}
Movie result = new Movie();
if (audioTracks.size() > 0) {
result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
}
Container out = new DefaultMp4Builder().build(result);
String fileName = getFilename();
FileChannel fc = new RandomAccessFile(String.format(fileName), "rw").getChannel();
out.writeContainer(fc);
fc.close();
Manifestfile
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ehrlite"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ehr_icon"
android:label="#string/app_name"
android:name="com.webservice.myApplication"
android:theme="#style/AppTheme" >
<activity
android:name="com.ehrlite.LoginActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateVisible|adjustResize"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.ehrlite.MasterActivity"
android:screenOrientation="portrait">
</activity>
<activity
android:name="com.ehrlite.MainActivity"
android:screenOrientation="portrait"
>
</activity>
<activity
android:name="com.ehrlite.PrevMemo"
android:screenOrientation="portrait">
</activity>
<activity
android:name="com.webviewcall.WebViewActivity"
android:screenOrientation="portrait">
</activity>`enter code here`
<activity
android:name="com.ehrlite.TranscribedActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="adjustPan">
</activity>
</application>
</manifest>
Can anyone help me out to improve performance,
thanks in advance.
I found the solution for your answer.
I was faced issue previously.
You are using RandomAccessfile. You need to use FileOutputStream
Container out = new DefaultMp4Builder().build(result);
String outputFilePath = getfilename();
FileOutputStream fos = new FileOutputStream(new File(outputFilePath));
out.writeContainer(fos.getChannel());
fos.close();
Reference:
why are java RandomAccessFile so much slower than FileOutputStream?
My onReceive() doesn't get invoked at all even when application receives sms message. It doesn't get invoked when the message arrives. Shouldn't it be working in background and only gets invoked when the SMS arrives?
It works fine on a seperate project but not when I'm integraing to my own app.
My Code:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
public class SmsBroadcastReciever extends BroadcastReceiver {
private static final String LOG_TAG = "SMSBroadRec";
public static final String SMS_BUNDLE = "pdus";
String SenderNo = "+92----------";
String SenderNo2 = "+92----------";
#Override
public void onReceive(Context context, Intent intent) {
Log.d(LOG_TAG, "In onReceive()");
Bundle bundle = intent.getExtras();
String smsBody;
String address;
try {
if ( !bundle.isEmpty() ){
Log.d(LOG_TAG, "Sms received");
String verificationCode = null;
Object[] sms = (Object[]) bundle.get(SMS_BUNDLE);
for (Object sm : sms) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sm);
smsBody = smsMessage.getMessageBody();
address = smsMessage.getOriginatingAddress();
Log.d(LOG_TAG, address);
if (SenderNo.equals(address) || SenderNo2.equals(address)) {
verificationCode = getVerificationCode(smsBody);
Log.e(LOG_TAG, "OTP received: " + verificationCode);
break;
} else {
Log.d(LOG_TAG, "wrong sender");
break;
}
}
SharedPreferences prefs = context.getSharedPreferences(AppConfig.APP_SCRATCH, Context.MODE_PRIVATE);
SharedPreferences.Editor ed = prefs.edit();
ed.putString(AppConfig.APP_SCRATCH, verificationCode);
if (ed.commit()) {
Log.d(LOG_TAG, "commit succesful"); //added to the shared preferences
} else {
Log.d(LOG_TAG, "commit unsuccessful");
}
} else {
Log.d(LOG_TAG, "Intent must be empty!");
}
}
catch (Exception e){
e.printStackTrace();
}
}
private String getVerificationCode(String message) {
String code = null;
int index = message.indexOf(AppConfig.OTP_DELIMITER);
if (index != -1) {
int start = index + 2;
int length = 8;
code = message.substring(start, start + length);
return code;
}
return code;
}
}
This is the part of the manifest file and i have added permissions too which are READ_SMS, RECEIVE_SMS, WRITE_SMS.
manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ubroadkast.nayatel"
android:versionCode="4"
android:versionName="1.3">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
<uses-feature
android:name="android.hardware.telephony"
android:required="true" />
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<uses-permission
android:name="android.permission.INTERNET"
android:required="true" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:required="true" />
<uses-permission
android:name="android.permission.RECORD_AUDIO"
android:required="true" />
<uses-permission
android:name="android.permission.CAMERA"
android:required="true" />
<uses-permission
android:name="android.permission.READ_PHONE_STATE"
android:required="true" />
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"
android:required="true" />
<uses-permission
android:name="android.permission.SEND_SMS"
android:required="true" />
<uses-permission
android:name="android.permission.ACCESS_WIFI_STATE"
android:required="true" />
<uses-permission
android:name="android.permission.CHANGE_WIFI_STATE"
android:required="true" />
<uses-permission
android:name="android.permission.READ_CONTACTS"
android:required="true" />
<uses-permission
android:name="android.permission.WRITE_CONTACTS"
android:required="true" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:required="true" />
<uses-permission android:name="android.permission.WRITE_SMS"
android:required="true"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"
android:required="true"/>
<uses-permission android:name="android.permission.READ_SMS"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.Ubroadkast">
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/facebook_app_id" />
<provider
android:name="com.facebook.FacebookContentProvider"
android:authorities="com.facebook.app.FacebookContentProvider912845522101212"
android:exported="true" />
<activity
android:name=".MainActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="#string/app_name"
android:screenOrientation="landscape" />
<activity
android:name=".Home"
android:label="#string/app_name"
android:screenOrientation="portrait" />
<activity
android:name=".Settings"
android:label="Settings"
android:screenOrientation="portrait" />
<activity
android:name=".UserStatus"
android:label="#string/title_activity_user_status" />
<activity
android:name=".login"
android:label="#string/app_name"
android:screenOrientation="portrait" />
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="#string/app_name"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
<activity
android:name=".facebook"
android:label="UBroadkast Facebook Sign In"
android:screenOrientation="portrait" />
<activity
android:name=".Terms"
android:label="Terms And Conditions"
android:screenOrientation="portrait" />
<activity
android:name=".Signup"
android:label="Sign Up"
android:screenOrientation="portrait" />
<activity
android:name=".rating"
android:label="Ubroadkast Feedback"
android:screenOrientation="portrait" />
<activity
android:name=".change_password"
android:label="Reset Password" />
<activity android:name=".HomeScreen" />
<activity android:name=".Videoview" />
<activity android:name=".URLExtractor" />
<receiver android:name=".SmsBroadcastReciever" android:enabled="true" android:exported="false">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECIEVED"/>
</intent-filter>
</receiver>
</application>
</manifest>
change following line in manifest from
<action android:name="android.provider.Telephony.SMS_RECIEVED"/>
to
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
There's a spelling mistake of word "received".
This question already has answers here:
Error message 'java.net.SocketException: socket failed: EACCES (Permission denied)'
(10 answers)
Closed 7 years ago.
i get the following Exception
I/System.out: java.net.SocketException: socket failed: EACCES (Permission denied)
I have a closed network without internet access, only router, android device and raspberry pi.
My AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk android:minSdkVersion="16" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
i have only one Activity
MainActivity:
public class MainActivity extends Activity {
private static final int PORT= 8888;
private static final int TIMEOUT_MS = 500;
#Override
protected void onCreate(Bundle savedInstanceState) {
final TextView tView;
Button buttonRed, buttonGreen, buttonBlue, buttonWhite;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tView = (TextView) findViewById(R.id.showroom_welcome);
buttonRed = (Button) findViewById(R.id.buttonred);
buttonRed.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
/*send message xyz*/
try {
String message = "\"xyz\"";
DatagramSocket socket = new DatagramSocket(PORT);
socket.setBroadcast(true);
socket.setSoTimeout(TIMEOUT_MS);
DatagramPacket packet = new DatagramPacket(message.getBytes(), message.length(), getBroadcastAddress(), PORT); //InetAddress.getByName("192.168.100.255"), PORT );
socket.send(packet);
}catch(UnsupportedEncodingException uee){
tView.setText(uee.getMessage());
System.out.println(uee.getMessage());
}catch (SocketException se){
tView.setText(se.getMessage() );
System.out.println(se+"\n");
}catch (IOException ioe){
tView.setText(ioe.getMessage());
System.out.println(ioe + "\n");
}
}
});
}
It is a UDP client that sends only xyz to raspi.
Do you have an idea what is the problem?
You're only setting up the permissions for the State of your network. You only need the state if you want an information in which network you are or if you want to change the network programmatically.
<uses-permission android:name="android.permission.CHANGE_WIFI_STATET" />
Is wrong change it to:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
You also need the the Internet Permission:
<uses-permission android:name="android.permission.INTERNET"/>
Add the INTERNET permission:
<uses-permission android:name="android.permission.INTERNET" />
This is an issue with your permissions.
Your missing the Internet-permission:
<uses-permission android:name="android.permission.INTERNET"/>
Adding this to your code should fix your problem.
Hope that helps!
I am trying use AsyncTask to extract an image over the internet and display on the RecyclerViewer but I am getting the following error message, which's captured from my Exception in my doInBackground from my RecyclerViewAdapter class:
08-02 00:11:25.608: E/ImageDownload(5753): Download failed: Permission denied (missing INTERNET permission?)
See full version of AsyncTask sub-class below:
class GetImageFromNet extends AsyncTask<String, Void, Bitmap> {
private RVAdapter.PersonViewHolder personViewHolder;
private String url = "http://ia.media-imdb.com/images/M/MV5BNDMyODU3ODk3Ml5BMl5BanBnXkFtZTgwNDc1ODkwNjE#._V1_SX300.jpg";
public GetImageFromNet(RVAdapter.PersonViewHolder personViewHolder){
this.personViewHolder = personViewHolder;
}
protected Bitmap doInBackground(String... params) {
try {
InputStream in = new java.net.URL(url).openStream();
Bitmap bitmap = BitmapFactory.decodeStream(in);
return bitmap;
//NOTE: it is not thread-safe to set the ImageView from inside this method. It must be done in onPostExecute()
} catch (Exception e) {
Log.e("ImageDownload", "Download failed: " + e.getMessage());
}
return null;
}
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
personViewHolder.personPhoto.setImageBitmap(bitmap);
}
I have no idea why I am getting above error since I have already added the following permission access in my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Please help!
Move your <uses-permission> elements to be immediate children of <manifest>, above your <application> element.
Update your manifest as:-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
This function is used to save image on sdcard (I have tested it on the emulator only):
public String SaveImage(String URL,String imagename){
Bitmap bitmap = null;
InputStream in = null;
String path = Environment.getExternalStorageDirectory().toString();
File dir = new File(path + "/bh");
if(!dir.exists())
new File(path + "/bh").mkdir();
Log.i("in save()", "after file");
File mImageFile = new File(path+"/bh/"+imagename);
if(mImageFile.exists())
Toast.makeText(mContext, "Saved", Toast.LENGTH_LONG).show();
try{
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
FileOutputStream out = new FileOutputStream(mImageFile);
bitmap.compress(CompressFormat.JPEG, 100,out);
out.flush();
out.close();
in.close();
return imagename+".jpg";
}catch(IOException ex){
Log.e("==== Error in saving image ====",ex.getMessage());
return "";
}
}
I have added the required permissions like below :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mobile.bh"
android:versionCode="4"
android:versionName="1.0.3" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<activity
android:name="mobile.bh.activities.BHActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="mobile.bh.activities.RecipesListActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="mobile.bh.activities.RecipeInfoActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="mobile.bh.activities.IngredientsActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="mobile.bh.activities.MethodActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="mobile.bh.activities.SpicesListActivity"
android:screenOrientation="portrait" >
</activity>
<activity android:name=".activities.SpicesCategoriesActivity" >
</activity>
<activity android:name=".activities.CategoryActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You also need to use the Internet permission since you're connecting to the internet.
<uses-permission android:name="android.permission.INTERNET" />
Does your emulator support SDCard ?
Have a look at this great answer, it might be the issue https://stackoverflow.com/a/11468278/1635817