I will store a data in a file.and i will display it.But my problem is how can i control the HTMLVewr or other reader to go a newline.
in this code when i display the file "file1" there are no newline.
try {
FileOutputStream ops=openFileOutput("file1",Context.MODE_PRIVATE);
DataOutputStream dos=new DataOutputStream(ops);
for(int i=0;i<w*h;i++){
//int alpa=Color.alpha(pixels[i]);
int red=Color.red(pixels[i]);
int green=Color.green(pixels[i]);
int blue=Color.blue(pixels[i]);
String k;
if(i==0)
k=red+" "+green+" "+blue+" ";
else if((i%(w-1)==0)&&(i>0)){
k="**"+red+" "+green+" "+blue+" ";
k=k+"/n";}
else
k="**"+red+" "+green+" "+blue+" ";
dos.write(k.getBytes());
}
ops.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Related
I am getting a duration of my audio file , convert it to int , then convert int to string and then string to something like this 00:32 . Converted string is correct, but then is a problem with convert that string to data
Here is my code:
File file = new File(Environment.getExternalStorageDirectory()+"/MyImages/.audio1.wav");
MediaPlayer mp = new MediaPlayer();
FileInputStream fs = null;
FileDescriptor fd = null;
try {
fs = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fd = fs.getFD();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.setDataSource(fd);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int length = mp.getDuration()/1000;
mp.release();
String audiotime = String.valueOf(length);
SimpleDateFormat sdf = new SimpleDateFormat("mmss");
try {
Date d = sdf.parse(audiotime);
textView1.setText(audiotime);
} catch (ParseException ex) {
}
Two things, most important, you use the original String as text for your TextView. You should do something like textView1.setText(d.toString());
Secondly, you should use "mm:ss" as your format, instead of "mmss".
Note you could also use the format function of SimpleDateFormat. This will return a StringBuffer, which might be more appropriate since you only need the String representation
source: android documentation
I am trying to send a file using Android bluetooth sockets. I am getting the pairing requests but unfortunately the data is not being transferred
my Server side and client side is as follows:
Server: (for my application server is receiving the data)
public void run() {
super.run();
BluetoothAdapter mBtadapter = BluetoothAdapter.getDefaultAdapter();
Log.i("bluetooth Adapter", "got adapter "+ mBtadapter.getAddress());
try {
Ssocket = mBtadapter.listenUsingRfcommWithServiceRecord("CardXchange", uuid);
Log.i("Socket", "Socket Acquired "+ Ssocket.toString());
} catch (IOException e) {
e.printStackTrace();
}
while(true){
if(read_flag()==1){
try {
Toast.makeText(c, "flag is eventually 1", Toast.LENGTH_LONG).show();
Ssocket.close();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
try {
client_socket = Ssocket.accept();
if(client_socket!=null){
//this function is used to handle the connection when sockets get connected
manageConnection1(client_socket);
Ssocket.close();
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
Log.i("breaked ", "oh god!! out of the loop ");
}
//implementation of the function
private void manageConnection1(BluetoothSocket client_socket2) {
// TODO Auto-generated method stub
int bytes;
Log.i("serevr chya manage connection madhe gela", "in servers manage connection ");
File file = new File("/sdcard/myfolder/received.Xcard");
try {
FileOutputStream fos = new FileOutputStream(file);
try {
InputStream is = client_socket2.getInputStream();
while((bytes = is.read(buffer, 0, 1024))>0){
fos.write(buffer, 0, 1024);
}
is.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
client_socket2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
and the client side (the sending side is like this):
public void run(){
try {
Method m = send_dev.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
Csocket = (BluetoothSocket) m.invoke(send_dev, 1);
Csocket.connect();
manageSendConnection(Csocket);
}catch(Exception e){
e.printStackTrace();
}
}
//function to handle after connection is established.
private void manageSendConnection(BluetoothSocket csocket) {
// TODO Auto-generated method stub
File f = new File("/sdcard/myfolder/card3.Xcard");
byte[] buffer = new byte[(int)f.length()];
try {
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
try {
bis.read(buffer, 0, (int)f.length());
OutputStream os = csocket.getOutputStream();
os.write(buffer);
os.close();
fis.close();
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
csocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I am getting the pairing request but the data is not being sent.
can anyone please help me to find the problem and give a hint on solution??
I would like to make a quiz program. The questions are in a text file in asset folder. The answers are also in the asset folder called the number of the question (for example: the first question answers are in the text file called 1). I would like to give the questions and answers randomly (answers to a button). Until this everything is all right (maybe not the shortest solution, but works well). Then the user can answer the question clicking the correct button. And here is the problem. I get the text of the button and the first row of the answer file (always the first row is the right answer in the answer text file). It should be the same, and then I sign, this is the correct answer. But it's not the same, and I don't know why. A put text to the button from the answer file and get the first row from the answer file, so it should be the same. I print it out to log cat, and look like they are the same. I don't know what could be went wrong.
Can anybody help me out.
This is where I set the text of the button (randomly) and compare the first rows and the text of the button:
BufferedReader br2 = new BufferedReader(is2, 8192);
for(int k2=0; k2<3; k2++){
try {
kerdes2[k2] = br2.readLine();
final ArrayList <Integer> kerdesno2 = new ArrayList<Integer>();
for(int j=0;j<3;j++) kerdesno2.add(j);
Collections.shuffle(kerdesno2);
System.out.println(kerdesno2);
answ.setText(kerdes2[kerdesno2.get(0)]);
answ2.setText(kerdes2[kerdesno2.get(1)]);
answ3.setText(kerdes2[kerdesno2.get(2)]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
answ.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
InputStreamReader is3 = null;
try {
is3 = new InputStreamReader(am.open(i3), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br3 = new BufferedReader(is3, 8192);
try {
String helyes = br3.readLine();
System.out.println(helyes);
String gomb = answ.getText().toString();
System.out.println(gomb);
for(int f=0; f<helyes.length(); f++)
{
char c = helyes.charAt(f);
char d = gomb.charAt(f);
if(c != d){
System.out.println(c);
System.out.println(((String) gomb).indexOf(c));
}
}
if(gomb == helyes)
{
x++;
TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
eredmeny.setText("Eredményed: " + Math.round(x*100/i2) + "%");
}
else
{
TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
eredmeny.setText(gomb + " = " + helyes);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
answ2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
InputStreamReader is3 = null;
try {
is3 = new InputStreamReader(am.open(i3), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br3 = new BufferedReader(is3, 8192);
try {
String helyes = br3.readLine();
System.out.println(helyes);
String gomb = answ2.getText().toString();
System.out.println(gomb);
if(gomb == helyes)
{
x++;
TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
eredmeny.setText("Eredményed: " + Math.round(x*100/i2) + "%");
}
else
{
TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
eredmeny.setText(gomb + " = " + helyes);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
answ3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
InputStreamReader is3 = null;
try {
is3 = new InputStreamReader(am.open(i3), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br3 = new BufferedReader(is3, 8192);
try {
String gomb = answ3.getText().toString();
String helyes = br3.readLine();
System.out.println(gomb);
System.out.println(helyes);
if(gomb == helyes){
x++;
TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
eredmeny.setText("Eredményed: " + Math.round(x*100/i2) + "%");
}
else
{
TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
eredmeny.setText(gomb + " = " + helyes);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
As You can see I try to iterate over the two strings to realize where the problem is, but I couldn't manage to find...
String is an object. When comparing objects, use .equals(), not ==.
Your code:
if(gomb == helyes)
Should be:
if(gomb.equals(helyes))
By using == you're comparing memory, not the actual String objects. Sometimes you'll get the expected result, but other times you won't. .equals() will always test the Strings themselves.
I can see you are comparing by
if(gomb == helyes){
while it should be
if(gomb.equals(helyes)){
How to read the particular data from the internal storage file.
For eg., I have stored
1. Device 2. Time(epoch format) 3. button text
CharSequence cs =((Button) v).getText();
t = System.currentTimeMillis()/1000;
s = cs.toString();
buf = (t+"\n").getBytes();
buf1 = (s+"\n").getBytes();
try {
FileOutputStream fos = openFileOutput(Filename, Context.MODE_APPEND);
fos.write("DVD".getBytes());
fos.write(tab.getBytes());
fos.write(buf);
fos.write(tab.getBytes());
fos.write(buf1);
//fos.write(tab.getBytes());
//fos.write((R.id.bSix+"\n").getBytes());
fos.write(newline.getBytes());
//fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
then while reading, how can we read only price from the file? (using fos.read())
Thanks
I would suggest writing the file in a more structured way, like this:
long t = System.currentTimeMillis()/1000;
String s = ((Button) v).getText();
DataOutputStream dos = null;
try {
dos = new DataOutputStream(openFileOutput(Filename, Context.MODE_APPEND));
dos.writeUTF("DVD");
dos.writeLong(t); // Write time
dos.writeUTF(s); // Write button text
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
// Ignore
}
}
}
To read it back, something like this:
DataInputStream dis = null;
try {
dis = new DataInputStream(openFileInput(Filename));
String dvd = dis.readUTF();
long time = dis.readLong();
String buttonText = dis.readUTF();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
// Ignore
}
}
}
m trying fetch all photos from perticular album from facebook in android , i am using facebook android sdk to do the task but the problem is , i don't know what url request is required to access the photos inside album ?
https://graph.facebook.com/ALBUM_ID/photos
If it's for a particular person then:
https://graph.facebook.com/me/albums/
And then choose the album id and then use the first call
EDIT
You will also need to give permission while creating Facebook object in the Permission String array also need to add user_photos to be able to load photos
Code Worked for Me.
I have two function - one is to get Album id and anther one is just retrieving all images from that particular album.
First use test() function and then use downloadpic().
public void test()
{
facebook.getAccessToken();
JSONArray albumss=null;
String response = null;
try {
response = facebook.request("me/albums");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject json = null;
try {
json = Util.parseJson(response);
} catch (FacebookError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONArray albums = null;
try {
albums = json.getJSONArray("data");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i =0; i < albums.length(); i++) {
JSONObject album = null;
try {
album = albums.getJSONObject(i);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
//Here I have selected Profile pic album.
if (album.getString("type").equalsIgnoreCase("profile")) {
// JSONArray al=null;
wallAlbumID = album.getString("id");
// Now you have album id in wallAlbumID(global varible).
Log.d("JSON", wallAlbumID);
break;
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Now use downloadpic():
void downloadpic( )
{
facebook.getAccessToken();
String response = null;
try {
response = facebook.request(wallAlbumID+"/photos");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject json = null;
try {
json = Util.parseJson(response);
} catch (FacebookError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONArray photos = null;
try {
photos = json.getJSONArray("data");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i =0; i < photos.length(); i++) {
JSONObject a = null;
try {
a = photos.getJSONObject(i);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String testing = null;
try {
testing = a.getString("source");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
\
URL value=null;
try {
value=new URL(testing);
//Now you have URL of that particular image use it in your way
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}