Amanni
Amanni

Reputation: 1934

Converting iPhone code to Android

I put up a problem on Android and didn't get any answer but I've found a similar problem on the iPhone platform with the answer but I don't know how to translate this into Java code. Please can anyone who is well versed in both languages give this a try.

NSString* userAgent = @"Mozilla/5.0";

NSURL *url = [NSURL URLWithString:[@"http://www.translate.google.com/translate_tts?tl=el&q=Καλημέρα" 
                        stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];

[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];

NSURLResponse* response = nil;
NSError* error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request
                                 returningResponse:&response
                                             error:&error];


[data writeToFile:@"/var/tmp/tts.mp3" atomically:YES];

Upvotes: 0

Views: 424

Answers (2)

user370305
user370305

Reputation: 109257

Ok, I figured out, (As I understand)

He is trying to download a file form web and saved it to local storage in i-phone,

In android try this code,

try {
        URL url = new URL("http://www.translate.google.com/translate_tts?tl=el&q=Καλημέρα");
        URLConnection connection = url.openConnection();
        connection.connect();
        // this will be useful so that you can show a typical 0-100% progress bar
        int fileLength = connection.getContentLength();

        // download the file
        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream("/sdcard/tts.mp3");

        byte data[] = new byte[1024];
       int count;
        while ((count = input.read(data)) != -1) {
            output.write(data, 0, count);
        }
       output.flush();
        output.close();
        input.close();
    } catch (Exception e) {
    }

Also mention Use-Permission in your Android Manifest.xml file like,

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Now, this will store your downloaded mp3 file in external storage.

Upvotes: 1

FabianCook
FabianCook

Reputation: 20587

HttpClient httpclient = new DefaultHttpClient();

HttpGet httpget = new HttpGet("http://www.translate.google.com/translate_tts?tl=el&q=Καλημέρα");

HttpResponse response = httpclient.execute(httpget);

Then get the file some how here from the response? Sorry no IDE with me atm.

Maybe something like this?

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(response.getEntity().getContent());

I have no idea how to parse mp3 files tho?

What about something like

InputStream is = response.getEnitity().getContent();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
   baf.append((byte) current);
}

FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();

Upvotes: 0

Related Questions