Categories
discuss

Is createTempFile thread-safe?

I’m using Java 6.

Is it possible that two threads calling createTempFile (of the class java.io.File) get the same temp file?

Answer

Best way to get your answer is to look at the source code. At first there isn’t any synchronization in createTempFile, but to generate the temp file name, it is using SecureRandom which is ThreadSafe.
Then unless you are really unlucky, your file will always get a different name.

On top of that, createTempFile implementation is looping, generating new file name, until the file has been created. The file creation of course is delegated to the native file system operation which we may assume is threadsafe..

Categories
discuss

IllegalArgumentException: Invalid column

Here is the logcat:

01-15 16:06:03.622: ERROR/AndroidRuntime(22300): Uncaught handler: thread main exiting due to uncaught exception
01-15 16:06:03.657: ERROR/AndroidRuntime(22300): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mohit.geo2do/com.mohit.geo2do.activities.TaskEdit}: java.lang.IllegalArgumentException: Invalid column due_date
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at android.os.Looper.loop(Looper.java:123)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at android.app.ActivityThread.main(ActivityThread.java:4363)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at java.lang.reflect.Method.invokeNative(Native Method)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at java.lang.reflect.Method.invoke(Method.java:521)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at dalvik.system.NativeStart.main(Native Method)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300): Caused by: java.lang.IllegalArgumentException: Invalid column due_date
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at android.database.sqlite.SQLiteQueryBuilder.computeProjection(SQLiteQueryBuilder.java:508)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at android.database.sqlite.SQLiteQueryBuilder.buildQuery(SQLiteQueryBuilder.java:356)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:309)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:266)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at com.mohit.geo2do.provider.TasksProvider.query(TasksProvider.java:174)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at android.content.ContentProvider$Transport.query(ContentProvider.java:130)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at android.content.ContentResolver.query(ContentResolver.java:202)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at com.mohit.geo2do.activities.TaskEdit.onCreate(TaskEdit.java:105)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-15 16:06:03.657: ERROR/AndroidRuntime(22300):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)

The line that’s associated with it is:

private Cursor task;
private Uri uri;
private String[] PROJECTION { 
    Tasks._ID, Tasks.TITLE, Tasks.COMPLETED, Tasks.DUE_DATE, Tasks.IMPORTANCE, Tasks.NOTES
};
...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_task);
    ...
    uri = getIntent().getData();
    task = getContentResolver().query(uri, PROJECTION, null, null, null);
} 
...

What could be the problem? The database is created just fine. Is there any other code you’d need to see?

UPDATE:
I am VERY sure this column exists. I queried the database with this:

Cursor c = db.rawQuery("SELECT * FROM tasks LIMIT 1", null);
for (int i = 0; i < c.getColumnNames().length; i++) {
    Log.v(TAG, c.getColumnNames()[i]);
}

And in LogCat:

01-15 16:52:07.857: VERBOSE/TasksProvider(24325): Creating database...
01-15 16:52:07.862: VERBOSE/TasksProvider(24325): _id
01-15 16:52:07.862: VERBOSE/TasksProvider(24325): title
01-15 16:52:07.862: VERBOSE/TasksProvider(24325): completed
01-15 16:52:07.862: VERBOSE/TasksProvider(24325): due_date
01-15 16:52:07.862: VERBOSE/TasksProvider(24325): notes
01-15 16:52:07.862: VERBOSE/TasksProvider(24325): importance

So the column DOES exist.

Answer

I found a weird fix to this. In String[] PROJECTION. You have to do:

private String[] PROJECTION { 
    Tasks._ID, 
    Tasks.TITLE, 
    Tasks.COMPLETED,
    Tasks.DUE_DATE + " as " + Tasks.DUE_DATE, 
    Tasks.IMPORTANCE + " as " + Tasks.DUE_DATE, 
    Tasks.NOTES + " as " + Tasks.NOTES
};
Categories
discuss

Union of two or more (hash)maps

I have two Maps that contain the same type of Objects:

Map<String, TaskJSO> a = new HashMap<String, TaskJSO>();
Map<String, TaskJSO> b = new HashMap<String, TaskJSO>();

public class TaskJSO { String id; }

The map keys are the “id” properties.

a.put(taskJSO.getId(), taskJSO);

I want to obtain a list with: all values in “Map b” + all values in “Map a” that are not in “Map b”.

What is the fastest way of doing this operation?

Thanks

EDIT: The comparaison is done by id. So, two TaskJSOs are considered as equal if they have the same id (equals method is overrided).

My intention is to know which is the fastest way of doing this operation from a performance point of view. For instance, is there any difference if I do the “comparaison” in a map (as suggested by Peter):

Map<String, TaskJSO> ab = new HashMap<String, TaskJSO>(a);
ab.putAll(b);
ab.values()

or if instead I use a set (as suggested by Nishant):

Set s = new Hashset();
s.addAll(a.values());
s.addAll(b.values());

Answer

Method 1:

 Set s = new HashSet();
 s.addAll(a.values());
 s.addAll(b.values());

Set is collection of unique objects. Refer: http://download.oracle.com/javase/1.4.2/docs/api/java/util/HashSet.html


Method 2:

This will compare keys, and if same keys are found — the value will be overwritten by the later Map’s value.

Map<String, TaskJSO> ab = new HashMap<String, TaskJSO>(a);
ab.putAll(b);
ab.values()

Now, no matter what’s the case… the comparison will take place using equals. So, Method-1 will call equals on all the values and Method2 will call it on all the keys. Depending on how complex the comparison is the performance will vary.

In method 1, you need to create a new Set but it ensures that different values with same keys are not overwitten. But Method-2 is smart, if you have unique IDs.

Edit#1 updates as the question was updated

Categories
discuss

Youtube data API : Get access to media stream and play (JAVA)

I want to access a youtube video and play using my own media player. I am able to get the video properties (title, url etc..) using the youtube data API. Can I get access to the stream of the video and use my own Media Player (like the Android Media Player) to play it.
I am trying all of these in JAVA.

Thanks in advance.. 🙂

Answer

You can’t. Look here for further reading on what the API could handle:
YoutubeAPI

If you could get an InputStream on that, Google won’t get any money for advertisement at all.

But you could parse the page of the video-url you got from the API and look for the real video-link inside the flash tags.

Categories
discuss

How do I set the android text view to cut any letters that don’t fit in a layout

I have several TextViews in a row and I want the 1st TextView to take up all spare space but if the combined length of the TextViews is too long to fit on one line, I want the 1st TextView to be cut off.

As for the spare space I have set the 1st TextView’s layout_width to match parent and layout_weight to 1 which works. I just can’t get it to cut off any extra letters so it fits on one row.

What I want is something like the following with ‘Text’ and ‘Long Text’

'Text   :data'
'Long T :data'

So ‘Long Text’ gets cut off to ‘Long T’ in order to fit.

What I get is

'Text   :data'
'Long Text :d'

Thanks!

Answer

Give the first (left) TextView a set size (set in relative terms, dp), or use maxWidth for it, and also use ellipsize.

http://developer.android.com/reference/android/widget/TextView.html#attr_android:ellipsize

This is a good answer to your problem: TextView cuts off text when it is long enough

Also, seems similar to this question which may help: Two TextViews side by side, only one to ellipsize?

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..