/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */

package org.appcelerator.titanium.io;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.appcelerator.titanium.TiApplication;

import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

public class TitaniumBlob extends TiBaseFile
{
	protected String url;
	protected String name;
	protected String path;

	public TitaniumBlob(String url) {
		super(TYPE_BLOB);
		this.url = url;
		if (url != null) {
			init();
		}
	}

	protected void init() {
		String [] projection = {
			MediaStore.Images.ImageColumns.DISPLAY_NAME,
			MediaStore.Images.ImageColumns.DATA
		};
		Cursor c = null;
		try {
			c = TiApplication.getInstance().getContentResolver().query(Uri.parse(url), projection, null, null, null);

			if (c.moveToNext()) {
				name = c.getString(0);
				path = c.getString(1);
			}
		} finally {
			if (c != null) {
				c.close();
			}
		}
	}

	public void setUrl(String url) {
		this.url = url;
		if (url != null) {
			init();
		}
	}

	@Override
	public String nativePath() {
		return url;
	}

	public String toURL() {
		return url;
	}

	@Override
	public String name() {
		return name;
	}

	public File getFile() {
		return new File(path);
	}

	public String getContentType() {
		return TiApplication.getInstance().getContentResolver().getType(Uri.parse(url));
	}

	public InputStream getInputStream()
		throws IOException
	{
		return TiApplication.getInstance().getContentResolver().openInputStream(Uri.parse(url));
	}

	@Override
	public OutputStream getOutputStream() throws IOException {
		return null;
	}

	@Override
	public File getNativeFile() {
		return new File(path);
	}
}

@@ -785,17 +785,16 @@
 %09%09String
- 
 %5B%5D proje
@@ -918,16 +918,93 @@
 ll;%0A
+%0A%09%09if (url.startsWith(%22content://com.android.providers.media.documents%22)) %7B%0A%09
 %09%09try %7B%0A
 %09%09%09c
@@ -999,16 +999,17 @@
 %09%09try %7B%0A
+%09
 %09%09%09c = T
@@ -1078,16 +1078,622 @@
 e(url), 
+null, null, null, null);%0A%09%09%09%09c.moveToFirst();%0A%09%09%09%09String id = c.getString(0);%0A%09%09%09%09id = id.substring(id.lastIndexOf(%22:%22) + 1);%0A%09%09%09%09c.close();%0A%0A%09%09%09%09c = TiApplication.getInstance().getContentResolver().query(%0A%09%09%09%09%09android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,%0A%09%09%09%09%09projection, MediaStore.Images.Media._ID + %22 = ? %22, new String%5B%5D%7Bid%7D, null);%0A%0A%09%09%09%09if (c.moveToNext()) %7B%0A%09%09%09%09%09name = c.getString(0);%0A%09%09%09%09%09path = c.getString(1);%0A%09%09%09%09%7D%0A%09%09%09%7D finally %7B%0A%09%09%09%09if (c != null) %7B%0A%09%09%09%09%09c.close();%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09try %7B%0A%09%09%09%09c = TiApplication.getInstance().getContentResolver().query(Uri.parse(url), 
 projecti
@@ -1694,29 +1694,29 @@
 ojection
-, null
 , null,
+ null,
  null);%0A
@@ -1716,16 +1716,17 @@
 null);%0A%0A
+%09
 %09%09%09if (c
@@ -1742,24 +1742,25 @@
 xt()) %7B%0A%09%09%09%09
+%09
 name = c.get
@@ -1766,24 +1766,25 @@
 tString(0);%0A
+%09
 %09%09%09%09path = c
@@ -1797,26 +1797,28 @@
 ring(1);%0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%7D finally 
@@ -1819,16 +1819,17 @@
 nally %7B%0A
+%09
 %09%09%09if (c
@@ -1840,24 +1840,25 @@
 null) %7B%0A%09%09%09%09
+%09
 c.close();%0A%09
@@ -1856,16 +1856,22 @@
 lose();%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

Add support for com.android.providers.media.documents in TitaniumBlob