Thursday, October 27, 2011

urllib2 POST progress

Innen.

It is possible but you need to do a few things:

  • Fake out the urllib2 subsystem into passing a file handle down to httplib by attaching a __len__attribute which makes len(data) return the correct size, used to populate the Content-Length header.
  • Override the read() method on your file handle: as httplib calls read() your callback will be invoked, letting you calculate the percentage and update your progress bar.

This could work with any file-like object, but I've wrapped file to show how it could work with a really large file streamed from disk:

import os, urllib2
from cStringIO import StringIO

class Progress(object):
def __init__(self):
self._seen = 0.0

def update(self, total, size, name):
self._seen += size
pct
= (self._seen / total) * 100.0
print '%s progress: %.2f' % (name, pct)

class file_with_callback(file):
def __init__(self, path, mode, callback, *args):
file
.__init__(self, path, mode)
self.seek(0, os.SEEK_END)
self._total = self.tell()
self.seek(0)
self._callback = callback
self._args = args

def __len__(self):
return self._total

def read(self, size):
data
= file.read(self, size)
self._callback(self._total, len(data), *self._args)
return data

path
= 'large_file.txt'
progress
= Progress()
stream
= file_with_callback(path, 'rb', progress.update, path)
req
= urllib2.Request(url, stream)
res
= urllib2.urlopen(req)

Output:

large_file.txt progress: 0.68
large_file
.txt progress: 1.36
large_file
.txt progress: 2.04
large_file
.txt progress: 2.72
large_file
.txt progress: 3.40
...
large_file
.txt progress: 99.20
large_file
.txt progress: 99.87
large_file
.txt progress: 100.00

Wednesday, October 26, 2011

poster python lib

poster provides a set of classes and functions to faciliate making HTTP POST (or PUT) requests using the standard multipart/form-data encoding.

Wednesday, August 24, 2011

[Android] resource string-ek kiolvasasa

Innen

-In a Layout XML:
android:text="@string/resource_name"

-In an Activity:
this.getString(R.string.resource_name);

-In an area of the program in which you have access to a Context or Application, such as a ListAdapter:
context.getString(R.string.resource_name);
application.getString(R.string.resource_name);

In case there is not a Context or Activity handy:
final Resources r = Resources.getSystem();
r.getString(android.R.string.resource_name);
vagy:
String r = getResources().getString(R.string.resource_name);

Tuesday, August 23, 2011

[Android] EditText beallitasok

Ha azt szeretnenk megadni, hogy milyen billentyuzet jelenjen meg egy EditText-hez, azt az android:inputType attributum beallitasaval intezhetjuk el.
A beviheto szoveg maximalis karakterszamat pedig az android:maxLength attributumban lehet beallitani.

Erdekes, hogy ha az inputType-ot textEmailAddress-re, phone-ra vagy textURI-ra allitom, akkor nem tordeli tobb sorosra az EditText-et akkor sem, ha kene, hanem vagy a vege vagy az eleje nem latszik a bevitt szovegnek. Illetve a textEmailAddress es a textURI szerkesztes kozben landscape modban tobb sorosra tordelve mutatja a szoveget, de csak szerkesztes kozben (ha latszik a billentyuzet). Ha nem eppen szerkesztes allatt van akkor landscape mode-ban is 1 soros, portrait modban pedig mindig. A phone viszont mindig egysoros. Meg akkor is amikor landscape modban szerkesztem es az EditBox magassaga automatikusan megno tobb sorosra. Ez persze lehet OS meg keszulek fuggo dolog is. En 2.2-es Androiddal (API Level 8) probaltam egy Samsung Galaxy S-en.

Megneztem es ugyanigy mukodik a default Contacts app-ban is. A telefonszamot sose tordeli, akkor se, ha van hely. Az email cimet es URL-t azt landscape szerkesztes kozben igen, de maskulonben nem.

Megneztem ugyanezt egy masik Samsung Galaxy S-en amin mar 2.3.3-as Android van es ott mar a textURI es a textEmailAddress eseteben (mar ha a Contacts app-ban ezek vannak beallitva) sem tordeli landscape modban szerkesztes kozben sem (noha az EditBox magassaga megno).

Megneztem egy HTC Sensation-on is 2.3.3-as Androiddal. Ott a Samsung-os Contacts app helyett egy People nevu app van ami csak portrait modban mukodik, de a browser-ben amikor szerkesztjuk az URL-t akkor ugyanaz a nem tordelos, de magas EditBox jon elo mint a 2.3.3-as Samsung-nal. Ebbol en arra kovetkeztetek, hogy valalohol a 2.2 es a 2.3.3 kozott volt egy valtas ami miatt mar nem csak a phone inputType nem tordelodhet soha, hanem a tobbi amugy sem todelheto (ahol nincs a billentyuzeten sortores gomb) sem.

Wednesday, August 17, 2011

[Android] AlrertDialog, Toast es Notification uzenetek megjelenitesere

The Android SDK offers three main types of notifications – enough to cover all of your needs:

In this tutorial, we will cover all three notification types.

View megjelenitese ures ListView eseten [Android]

Ha az Activity a ListActivity-bol szarmazik akkor kell lennie a layout-jan egy "@android:id/list" id-ju ListView-nak ami megjeleniti a listat, de lehet rajta meg egy "@android:id/empty" id-ju View is ami automatikusan megjlenik, ha a listank ures.

network elerhetoseg ellenorzese Androidon

Lars Vogel-tol:

Android allows to check if the network is available. Here is a code snippet to check this:
public boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
// if no network is available networkInfo will be null, otherwise check if we are connected
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
Your application requires also the uses-permission “android.permission.ACCESS_NETWORK_STATE” to check for the network state.

Tuesday, August 09, 2011

Screenshot keszitese Android device-rol

  • USB Debugging legyen bekapcsolva a device-on (Settings/Application/Developement)
  • USB kabelen legyen radugva a gepre a device.
  • Davlik Debug Monitor Service inditasa (android-sdk\tools\ddms.bat)
  • Device/Screen capture

Wednesday, August 03, 2011

SamFirmware

Ha masik FirmWare kene Samsung telefnra: http://samfirmware.com/

Most tud MPEG_4_SP video encodolast a MediaRecorder vagy nem???

Itt azt jelzik, hogy nem. Be is masolom a tablazotat, nehogy utolag mas legyen:

The table below describes the media format support built into the Android platform. Note that any given mobile device may provide support for additional formats or file types not listed in the table.

Note: Media codecs that are not guaranteed to be available on all Android platform versions are accordingly noted in parentheses—for example "(Android 3.0+)".

Table 1. Core media format and codec support.

TypeFormat / CodecEncoderDecoderDetailsSupported File Type(s) / Container Formats
AudioAAC LC/LTP••Mono/Stereo content in any combination of standard bit rates up to 160 kbps and sampling rates from 8 to 48kHz3GPP (.3gp), and MPEG-4 (.mp4, .m4a). ADTS raw AAC (.aac, decode only, ADIF not supported, Android 3.1+).
HE-AACv1 (AAC+) •
HE-AACv2 (enhanced AAC+) •
AMR-NB••4.75 to 12.2 kbps sampled @ 8kHz3GPP (.3gp)
AMR-WB••9 rates from 6.60 kbit/s to 23.85 kbit/s sampled @ 16kHz3GPP (.3gp)
FLAC •
(Android 3.1+)
Mono/Stereo (no multichannel). Sample rates up to 48 kHz (but up to 44.1 kHz is recommended on devices with 44.1 kHz output, as the 48 to 44.1 kHz downsampler does not include a low-pass filter). 16-bit recommended; no dither applied for 24-bit.FLAC (.flac) only
MP3 •Mono/Stereo 8-320Kbps constant (CBR) or variable bit-rate (VBR)MP3 (.mp3)
MIDI •MIDI Type 0 and 1. DLS Version 1 and 2. XMF and Mobile XMF. Support for ringtone formats RTTTL/RTX, OTA, and iMelodyType 0 and 1 (.mid, .xmf, .mxmf). Also RTTTL/RTX (.rtttl, .rtx), OTA (.ota), and iMelody (.imy)
Ogg Vorbis • Ogg (.ogg)
PCM/WAVE •8- and 16-bit linear PCM (rates up to limit of hardware)WAVE (.wav)
ImageJPEG••Base+progressiveJPEG (.jpg)
GIF • GIF (.gif)
PNG•• PNG (.png)
BMP • BMP (.bmp)
VideoH.263•• 3GPP (.3gp) and MPEG-4 (.mp4)
H.264 AVC•
(Android 3.0+)
•Baseline Profile (BP)3GPP (.3gp) and MPEG-4 (.mp4). MPEG-TS (.ts, AAC audio only, not seekable, Android 3.0+)
MPEG-4 SP • 3GPP (.3gp)
VP8 •
(Android 2.3.3+)
WebM (.webm)


Ugyanakkor a MediaRecorder.VideoEncoder dokumentaciojaban azt irjak, hogy API Level 3 ota tudja. Ezt is idemasolom:

public static final int DEFAULT

Constant Value: 0 (0x00000000)

public static final int H263

Constant Value: 1 (0x00000001)

public static final int H264

Constant Value: 2 (0x00000002)

public static final int MPEG_4_SP

Constant Value: 3 (0x00000003)

A tapasztalatom pedig az volt, hogy 2.2.1-es Firmware-nel ha ezt allitottam be akkor siman mukodott (valoszinuleg fallback-elt h.263-ra), de 2.3.3-as Firmwaren probalva IOException-t dobott a MediaRecorder.prepare(). A 2.2.1-es egy Samsung Galaxy S GT-I9003-ason volt, a 2.3.3-as pedig egy GT-I9000-esen.

Android SDK version ellenorzese kodbol

Build.VERSION.SDK_INT
The user-visible SDK version of the framework; its possible values are defined in Build.VERSION_CODES.

How To: Use Odin To Upgrade Samsung Galaxy S GT-I9000 To Froyo

Itt van.

Tuesday, August 02, 2011

setVideoSize legyen a setVideoEncoder elott

Ha elobb hivom meg a MediaRecorder-en a setVideoEncoder-t es utana a setVideoSize-ot akkor barmit is allitok be 320x240-ben rogzit. Ha elobb allitom be a meretet es csak utana a VideoEncodert, akkor jo lesz.