Wednesday, 7 August 2013

What is the Synced App Images Real/Physical Path?

What is the Synced App Images Real/Physical Path?

I'm trying to send some Images from Android to a RESTFul WCF.
By now I'm being able to select the Images from the Gallery and sending
them to the Server.
But I'm having problems with the Synced Images that get stored in the
Tablet Like the Facebook or G+ photos.
I'm using this function to get the path of the Image
public static String getRealPathFromURI(Context context, Uri contentUri) {
String path = null;
if (contentUri.getScheme().toString().compareTo("content")==0)
{
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(contentUri,
proj, null, null, null);
int column_index =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
path = cursor.getString(column_index);
}
else
{
path = contentUri.getPath();
}
Log.i(TAG, path);
return path;
}
With that kind of images I get the internet path like:
https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-ash4/s2048x2048/432098_10151223392360790_398885469_n.jpg
Just for clarity and to remark. I get a "content" scheme.. so I get the
path from the "if " something like:
content://com.sec.android.gallery3d.provider/picasa/item/5703464571893262194
So When I was using it in the MultipartEntity
((MultipartEntity) oInputEntity).addPart(
"fileContents",
new FileBody(new
File(Utilities.getRealPathFromURI(context,
imageUri)),
"image/jpeg"));
I was getting a FileNotFoundEception I think it's because the image is
over the Internet,
So I changed my method to download the image and now is working with this
code
public static File getFileFromURI(Context context, Uri contentUri) {
String path = IntUtilities.getRealPathFromURI(context,
contentUri);
Log.i(TAG, path);
final File file;
if (path.startsWith("http") || path.startsWith("/http") )
{
//if its an image form internet lets download it and save
it in our directory and return that file
// Determine Uri of camera image to save.
final String fname = "BIR" + UUID.randomUUID() + ".jpg";
final File root = new
File(Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator + "BIR");
root.mkdirs();
file = new File(root, fname);
try {
final URL url = new URL(path);
final HttpURLConnection urlConnection =
(HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(false);
urlConnection.connect();
final FileOutputStream fileOutput = new
FileOutputStream(file);
final InputStream inputStream =
urlConnection.getInputStream();
@SuppressWarnings("unused")
int downloadedSize = 0;
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
}
// close the output stream when done
fileOutput.close();
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
file = new File(path);
}
return file;
}



((MultipartEntity) oInputEntity).addPart(
"fileContents",
new
FileBody(Utilities.getRealPathFromURI(context,
imageUri),
"image/jpeg"));
But I'm not comfortable with this solution, seems like double effort, I
turned off my Wifi and 3g in the tablet, also turned off and on the tablet
iself and I still see those images, so I'm guessing they got copied
locally on the tablet when they were synced for the first time. I looked
for them when attached to my computer to see if they were there, but I
dont see them, maybe I'm doing something wrong or dont know the storage
folder.
The main reason that I dont like this solution is that if you don't have
Internet on the moment, obviously the image will not be downloaded, and
the app I'm making is supposed to work offline, and well.. the Image is
there, there shouldn't be a request to internet to guess a local image.
Being said this, is there a way to find the real/physical path of this
Photos that were synced, that have an http or https schem?
I really appreciate your help

No comments:

Post a Comment