How to generate external logfile in android app? - android

may i know is there any way to generate external log file?

This can be done e.g. with java.util.logging classes:
import android.os.Environment;
import android.util.Log;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;
FileHandler fh=null;
String name;
if ( 0 == Environment.getExternalStorageState().compareTo(Environment.MEDIA_MOUNTED))
name = Environment.getExternalStorageDirectory().getAbsolutePath();
else
name = Environment.getDataDirectory().getAbsolutePath();
name += "/mylogfile.log";
try {
fh = new FileHandler(name, 256*1024, 1, true);
fh.setFormatter(new SimpleFormatter());
fh.publish(new LogRecord(level, tag+": "+msg));
} catch (Exception e)
{
Log.e("MyLog", "FileHandler exception", e);
return;
}finally{
if (fh != null)
fh.close();
}

Related

Bitmap saved as black image sometimes for same bitmap

I know this was a common issue, but I have tried different suggestion, and still no solution.
My issue is that the bitmap is being displayed correctly, however SOMETIMES it saves as a black bitmap, while other times it saves correctly.
Can anyone see what I have done and tell me where I can be going wrong?
I have looked at most of the other StackOverflow questions.
The following is my code, which encodes the clip board text into a QR code and attempts to save the qr code generated.
Thank you!
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.text.ClipboardManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
//import android.content.ClipboardManager;
public class BarcodeWriter extends AppCompatActivity {
ImageLoader imgLoader;
ImageView qrImg;
String copiedStr;
TextView qrTxt;
ClipboardManager clipboard;
String BASE_QR_URL = "http://chart.apis.google.com/chart?cht=qr&chs=400x400&chld=M&choe=UTF-8&chl=";
String fullUrl = BASE_QR_URL;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.barcode_writer);
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.build();
imgLoader = ImageLoader.getInstance(); // Do it on Application start
imgLoader.init(config);
qrImg = (ImageView)findViewById(R.id.qrImg);
qrTxt = (TextView)findViewById(R.id.qrTxt);
clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
/*
* clipboard.getText() is now deprecated. But I am going to use it here
* because the new way of doing the same thing only works on API lvl 11+
* Since I want this application to support API lvl 4+ we have to use
* the old method.
*/
CharSequence clipTxt = clipboard.getText();
//This is the new, non-deprecated way of getting text from the Clipboard.
//CharSequence clipTxt = clipboard.getPrimaryClip().getItemAt(0).getText();
//If the clipboard has text, and it is more than 0 characters.
if((null != clipTxt) && (clipTxt.length() > 0)){
try {
qrTxt.setText(clipTxt);
copiedStr = clipTxt.toString();
fullUrl += URLEncoder.encode(copiedStr, "UTF-8");
//imgLoader.displayImage(fullUrl, qrImg);
ImageLoader.getInstance().displayImage(fullUrl, qrImg, defaultOptions); // Incoming options will be used
qrImg.setDrawingCacheEnabled(true);
qrImg.measure(View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY));
qrImg.layout(0, 0, qrImg.getMeasuredWidth(), qrImg.getMeasuredHeight());
qrImg.buildDrawingCache();
Bitmap bm = Bitmap.createBitmap(qrImg.getDrawingCache());
qrImg.setDrawingCacheEnabled(false); // clear drawing cache
try {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/DCIM/QR codes");
myDir.mkdirs();
String fname = clipTxt+".png";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, out);
Toast.makeText(BarcodeWriter.this, "Image Saved", Toast.LENGTH_SHORT).show();
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
getApplicationContext().sendBroadcast(mediaScanIntent);
Toast.makeText(this,"QR Code showing "+clipTxt,Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "Error occurred. Please try again later.",
Toast.LENGTH_SHORT).show();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{ //If no text display a dialog.
Toast.makeText(this,"No text in clipboard",Toast.LENGTH_SHORT).show();
}
}
}
Your code seems to be fine, the only thing that I saw is that you are getting you Bitmap from and external URL with Universal Image Loader instantly after calling displayImage. So is possible that for some delay in the network sometimes your ImageView still doesn't have the entire image and can make your bitmap saved as black.
Please try moving your all code bellow ImageLoader.getInstance().displayImage inside the onLoadingComplete like this:
ImageLoader.getInstance().displayImage(fullUrl, qrImg, defaultOptions, new SimpleImageLoadingListener()
{
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
ImageLoader.getInstance().displayImage(fullUrl, qrImg, defaultOptions,null);
qrImg.setDrawingCacheEnabled(true);
...
catch (Exception e) {
Toast.makeText(this, "Error occurred. Please try again later.",
Toast.LENGTH_SHORT).show();
}
}
});
Hope this help!!

how to send data (a string) to a paired device in android?

I am working on bluetooth for the first time. I got the list of paired devices. Now my requirement is I need to send some data (a string) to the device. how can I do that? I tried searching but didn't find anything useful. Could anyone help out this?
Something like this might suffice:
DataOutputStream os;
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
BroadcastReceiver discoveryResult = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String remoteDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
BluetoothDevice remoteDevice;
remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText(getApplicationContext(), "Discovered: " + remoteDeviceName + " address " + remoteDevice.getAddress(), Toast.LENGTH_SHORT).show();
try{
BluetoothDevice device = bluetooth.getRemoteDevice(remoteDevice.getAddress());
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
BluetoothSocket clientSocket = (BluetoothSocket) m.invoke(device, 1);
clientSocket.connect();
os = new DataOutputStream(clientSocket.getOutputStream());
new clientSock().start();
} catch (Exception e) {
e.printStackTrace();
Log.e("BLUETOOTH", e.getMessage());
}
}
};
registerReceiver(discoveryResult, new IntentFilter(BluetoothDevice.ACTION_FOUND));
bluetooth.enable();
if (!bluetooth.isDiscovering()) {
bluetooth.startDiscovery();
}
public class clientSock extends Thread {
public void run () {
try {
os.writeBytes("anything you want"); // anything you want
os.flush();
} catch (Exception e1) {
e1.printStackTrace();
return;
}
}
}
You will also need a lot of imports such as these:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;
import java.util.UUID;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;
note that not all imports are necessary for this example code, your IDE might help you to sort them out for you.
Pass data on the os.writeBytes("anything you want"); // anything you want line.
You will also need Permissions

Taking screenshot crashes app

I tried creating a screenshot app, but somehow I can not get the root content.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Canvas;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.MeasureSpec;
public class Screenshot extends Activity {
public final String TAG = "Screen";
String output;
public String takeScreen(String savepathname) {
Log.e(TAG, "TAKESCREEN 01");
//View content = findViewById(R.id.button1);
//View content = findViewById(R.id.layoutroot);
//View content = getWindow().getDecorView().findViewById(android.R.id.content);
Log.e(TAG, "TAKESCREEN 01.5");
content.setDrawingCacheEnabled(true);
Log.e(TAG, "TAKESCREEN 02");
//View content = findViewById(R.id.layoutroot);
Bitmap bitmap = content.getDrawingCache();
File file = new File( Environment.getExternalStorageDirectory() + savepathname);
Log.e(TAG, "TAKESCREEN 03");
try{
Log.e(TAG, "TAKESCREEN 04");
file.createNewFile();
Log.e(TAG, "TAKESCREEN 05");
FileOutputStream ostream = new FileOutputStream(file);
Log.e(TAG, "TAKESCREEN 06");
bitmap.compress(CompressFormat.PNG, 100, ostream);
Log.e(TAG, "TAKESCREEN 07");
ostream.close();
output = "Successfully saved -> "+savepathname;
}catch (Exception e) {
e.printStackTrace();
output = "Screenshot.java -> "+e;
}
return output;
}
}
If I use findViewbyId, I only get this error: "layoutroot cannot be resolved or is not a field".
While using "getWindow().getDecorView().findViewById(android.R.id.content);" works, but then the app crashes in that part.
Could someone help me with that, please?
Did you implement this part too? If you didn't, place it inside your Screen class. By the way, I strongly believe that going over this activity lifecycle tutorial will help you.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Assuming that button1 and layoutroot are properly described within activity_main.xml

Turn FTP command into ASync Task?

First off, don't flame me for not searching, I've looked for answers to this and while there are answers out there, I can't understand any of them.
Now, with that aside, I'm trying to put my ftp command into an ASync task for Android.
Code:
package com.dronnoc.ftp;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.io.CopyStreamEvent;
import org.apache.commons.net.io.CopyStreamListener;
import org.apache.commons.net.io.Util;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FTPTransfer ftp = new FTPTransfer("hostname", "user", "pass");
String data = Environment.getDataDirectory().toString();
data += "/data/com.dronnoc.ftp/databases/test.db";
boolean result = ftp.upload(data, "/ftp_dir/test.db");
if(result)
{
Log.d("message", "Upload Successful.");
}
else
{
Log.e("message", ftp.error());
}
ftp.close();
}
}
FTPTransfer.java
package com.dronnoc.ftp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;
public class FTPTransfer {
final FTPClient ftp = new FTPClient();
private String error = null;
private boolean connected = false;
public FTPTransfer(String host, String username, String pass) {
// TODO Auto-generated constructor stub
try {
ftp.connect(host);
if(ftp.login(username, pass))
{
ftp.enterLocalPassiveMode();
connected = true;
}
}
catch (FTPConnectionClosedException e) { error = e.getMessage(); }
catch (SocketException e) { error = e.getMessage(); }
catch (IOException e) { error = e.getMessage(); }
}
public boolean upload(String localName, String remoteName)
{
if(ftp.isConnected() && connected)
{
try {
FileInputStream file = new FileInputStream(localName);
boolean result = ftp.storeFile(remoteName, file);
if(result) { return true; }
else { return false; }
}
catch (FileNotFoundException e) { error = e.getMessage(); return false; }
catch (IOException e) { error = e.getMessage(); return false; }
}
return false;
}
public boolean upload(File localName, String remoteName)
{
if(ftp.isConnected() && connected)
{
try {
FileInputStream file = new FileInputStream(localName.getAbsolutePath());
boolean result = ftp.storeFile(remoteName, file);
if(result) { return true; }
else { return false; }
}
catch (FileNotFoundException e) { error = e.getMessage(); return false; }
catch (IOException e) { error = e.getMessage(); return false; }
}
return false;
}
public boolean download(String remote, String local)
{
//TODO Put appropriate code here
return false;
}
public boolean close()
{
try {
ftp.disconnect();
return true;
} catch (IOException e) {
error = e.getMessage();
return false;
}
}
public String error()
{
return error;
}
}
What I want to know is how I can put my FTP function into an ASync task so that I can run it in a background activity and update a progress bar, and also indicate how many bytes its uploaded so far?
Cheers
EDIT
The code itself works at the moment, I just need to know how to make it into an Async task
Not sure if you're asking how to use the AsyncTask but in case you are here's a tutorial about using AsyncTask to make requests to a web service. This can be extended easily to perform FTP in the background. AsyncTask can also support Progress though I don't think it's mentioned in the tutorial.
Basically your upload function needs to move to doInBackground and so does the connection code. See http://geekjamboree.wordpress.com/2011/11/22/asynctask-call-web-services-in-android/

JmDNS Error - The method getInetAddresses() is undefined for the type ServiceInfo

For some reason it doesn't like the getInetAddresses() method in the lines:
Enumeration additions = (Enumeration) ev.getInfo().getInetAddresses();
if (ev.getInfo().getInetAddresses() != null && ev.getInfo().getInetAddresses().length > 0) {
additions = ev.getInfo().getInetAddresses()[0].getHostAddress();
}
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import javax.jmdns.ServiceInfo;
private String type = "_workstation._tcp.local.";
private JmDNS jmdns = null;
private ServiceListener listener = null;
private ServiceInfo serviceInfo;
private void setUp() {
android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) getSystemService(android.content.Context.WIFI_SERVICE);
lock = wifi.createMulticastLock("mylockthereturn");
lock.setReferenceCounted(true);
lock.acquire();
try {
jmdns = JmDNS.create();
jmdns.addServiceListener(type, listener = new ServiceListener() {
#Override
public void serviceResolved(ServiceEvent ev) {
Enumeration additions = (Enumeration) ev.getInfo().getInetAddresses();
if (ev.getInfo().getInetAddresses() != null && ev.getInfo().getInetAddresses().length > 0) {
additions = ev.getInfo().getInetAddresses()[0].getHostAddress();
}
notifyUser("Service resolved: " + ev.getInfo().getQualifiedName() + " port:" + ev.getInfo().getPort() + additions);
}
#Override
public void serviceRemoved(ServiceEvent ev) {
notifyUser("Service removed: " + ev.getName());
}
#Override
public void serviceAdded(ServiceEvent event) {
// Required to force serviceResolved to be called again (after the first search)
jmdns.requestServiceInfo(event.getType(), event.getName(), 1);
}
});
serviceInfo = ServiceInfo.create("_test._tcp.local.", "AndroidTest", 0, "plain test service from android");
jmdns.registerService(serviceInfo);
} catch (IOException e) {
e.printStackTrace();
return;
}
}
Try download and use latest version of JmDNS from Maven Central Repository, apparently there are some issues related to the jar file downloaded from their SourceForge home page. jar file from SourceForge doesn't play quite nice with Android.
Check out this SO question for more details. Hope this help.

Categories

Resources