Setting Google Glass Card image with file URI fails - android

I'm having issues using the Cards from the recently released GDK. Basically, Card.addImage() can only take two arguments, a resource id or a URI.
For my use case, I need to open an image that exists as a file not directly as a resource. So for testing purposes I'm including the images in the assets folder. Trying to access them directly from the asset folder fails, so I'm copying them from there to internal storage. Once they're copied, I generate a URI from the file and assign it to the card. The resulting card shows a grey block where the image should be.
String fileName = step.attachment; //of the form, "folder1/images/image1.jpg"
File outFile = new File(getFilesDir()+File.separator+fileName);
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
//check to see if the file has already been cached in internal storage before copying
if(!outFile.exists()) {
FileInputStream inputStream = new FileInputStream(getAssets().openFd(fileName).getFileDescriptor());
FileOutputStream outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
inputChannel = inputStream.getChannel();
outputChannel = outputStream.getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(inputChannel!=null)
inputChannel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(outputChannel!=null)
outputChannel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
card.addImage(Uri.fromFile(outFile));
It's hard to diagnose because I have no clue what the Card is doing internally.

Instead of writing
new FileInputStream(getAssets().openFd(fileName).getFileDescriptor());
can you try
getAssets().openFd(fileName).createInputStream();
and see if it works?
To answer your original question, the addImage method supports resource: and file: URIs.

This is very strange, but I managed to solve my problem. I replaced the file copy code with the following and it appears to have solved my issues
InputStream in = null;
OutputStream out = null;
try {
in = getAssets().open(step.attachment);
out = new FileOutputStream(outFile);
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + step.attachment, e);
}
It's not clear to me why/how I was copying my entire apk, but I'm guessing it's the call to
getAssets().openFd(fileName).getFileDescriptor()
Perhaps it was returning the file descriptor of the apk. It's odd because I've seen some claim that the previous method works.

Related

Google Drive Android api - Downloading db file from drive

I am using the below code for downloading an already uploaded sqlite db file from google drive to the data/data/packagename/databases folder, but when the method completes, I am seeing a db corruption warning message logged in logcat and also all the data on the device for the app is overwritten and shows up blank, upon opening the app.
mfile = Drive.DriveApi.getFile(mGoogleApiClient, mResultsAdapter.getItem(0).getDriveId());
mfile.openContents(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null).setResultCallback(contentsOpenedCallback);
--mfile is an instance of DriveFile
final private ResultCallback<ContentsResult> contentsOpenedCallback = new ResultCallback<ContentsResult>()
{
#Override
public void onResult(ContentsResult result)
{
if (!result.getStatus().isSuccess())
{
FileUtils.appendLog(getApplicationContext(), Tag + "-onResult", "Error opening file");
return;
}
try
{
if (GetFileFromDrive(result))
{
//FileUtils.Restore(getApplicationContext());
SharedPrefHelper.EditSharedPreference(getApplicationContext(), Constants.PREFS_DO_RESTORE, false);
}
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
private boolean GetFileFromDrive(ContentsResult result)
{
Contents contents = result.getContents();
//InputStreamReader rda = new InputStreamReader(contents.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(contents.getInputStream()));
FileOutputStream outStream;
String currLine;
boolean restoreSuccess = false;
File sourceDbFile = BackupDBBeforeDeletion();
if(sourceDbFile != null)
sourceDbFile.delete();
try
{
outStream = new FileOutputStream(getApplicationContext().getDatabasePath(Constants.DB_NAME));
while ((currLine = reader.readLine()) != null)
{
outStream.write(currLine.getBytes());
}
outStream.flush();
reader.close();
outStream.close();
restoreSuccess = true;
}
catch (FileNotFoundException e)
{
// TODO: Log exception
}
catch (IOException e)
{
// TODO: Log Exception
}
return restoreSuccess;
}
When the method GetFileFromDrive completes, a db corruption shows up on LogCat and all the existing data on the app's datanase file (sqlite db) is gone.
Please help, as I have verified that the drive uploaded sqlite db file is correct and well formed, by downloading the same and opening it up in Sqlite Browser. It's the download from drive that is not working.
This solution is not working for me. The sqlite files I got from google drive is more longer than I saved in google drive. That, probably, happen because you are using a Buffereader class and the method readline that reads data in char.
In my project and experience this solutions does not work, I got a corrupted sqlite db.
So... I save sqlite db in google drive in byte so **I read from google drive the same sqlite db in byte, using BufferedInputStream.
This works perfectly for me:
DriveContents contents = result.getDriveContents();
BufferedInputStream bis = new BufferedInputStream(contents.getInputStream());
byte[] buffer = new byte[1024];
int bytesread = 0;
FileOutputStream outStream;
/*if(currentDB != null)
currentDB.delete();*/
try
{
outStream = new FileOutputStream(currentDB);
while( (bytesread = bis.read(buffer)) != -1)
{
outStream.write(buffer,0,bytesread);
}
outStream.flush();
bis.close();
outStream.close();
}
catch (FileNotFoundException e)
{
Log.i(TAG,e.getMessage());
}
catch (IOException e)
{
Log.i(TAG,e.getMessage());
}
finally {
Toast.makeText(getActivity().getBaseContext(),"Data from Google Drive restored successfully." , Toast.LENGTH_LONG).show();
}
contents.discard(mGoogleApiClient);
I was able to finally fix my own issue, by replacing the file.openContents with file.open and setting a result call back with DriveContents result back rather than ContentsResult.
Also, used the DriveContents to set an InputStream on the same (initially opened in Mode_Read_Only in open method) and then writing it out into a physical ".db" file on the data base path location.
Now, the database doesn't get corrupted and restores data successfully.

Downloaded image shows in computer but not in mobiles folder

I am working on an image wallpaper application in android when i download the image from url sometime it shows images on mobile but most of time it does't show but when i connect my mobile to computer its right in the specified folder.
Thanks advance looking forward for answer.
Here is my code to download image.
protected Void doInBackground(Void... arg0) {
FileOutputStream fileOutput = null;
try {
// set the path where we want to save the file
// in this case, going to save it on the root directory of the
// sd card.
File dir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
String filename = _imagePaths[FullScreenImage.position]
.substring(_imagePaths[FullScreenImage.position]
.lastIndexOf("/"));
if (!dir.exists())
dir.mkdirs();
File file = new File(dir, filename);
Log.i("Local filename:", "" + filename);
// this will be used to write the downloaded data into the file
// we created
fileOutput = new FileOutputStream(file);
Bitmap mybitmap = imageLoader.getBitmap(
_imagePaths[FullScreenImage.position], 800, 480);
mybitmap.compress(CompressFormat.JPEG, 100, fileOutput);
// close the output stream when done
// catch some possible errors...
} catch (IOException e) {
Log.e("Download Image catch > ", e.getMessage());
} finally {
if (fileOutput != null) {
try {
fileOutput.flush();
fileOutput.close();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("Download Image catch > ", e.getMessage());
}
}
}
return null;
}
Use MediaScannerConnection.scanFile() to notify the system about new media files.

Saving a file to the external temp dir from R.raw.some_file.m4a

I need to be able to save a file to the external storgage temp dir. The file I am saving though is the R.raw directory of my app.
I have used this example here.
Move Raw file to SD card in Android
The issue is
1. The app seems to read the .m4a file I want (possible reads the bytes wrong here).
2. When the file is saved to the /tmp dir the file size is totally wrong.
eg one file goes from 30kb to 300kb, another goes from 25kb, to .25kb.
Any suggestions
public String saveAs(int ressound, String whipName){
byte[] buffer=null;
InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
int size=0;
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i("saveas", "did not save1");
//return false;
}
String path= Environment.getExternalStorageDirectory().getAbsolutePath()+"/tmp/.pw2";
String filename="/"+whipName+".m4a";
Log.i("path", "file path is " + path);
boolean exists = (new File(path)).exists();
if (!exists){new File(path).mkdirs();}
FileOutputStream save;
try {
save = new FileOutputStream(path+filename);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.i("saveas", "did not save2");
//return false;
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i("saveas", "did not save3");
//return false;
}
File k = new File(path, filename);
return k.getAbsolutePath();
}
You CAN read a file in one full buffer like you're doing, but this is generally bad practice, unless you know the files are small, and the InputStream will know the full size in advance and be able to load all data at once.
If you aren't absolutely sure of max file size, especially on mobile, don't try to load the full thing in memory.
See IOUtils code for the classic example:
http://grepcode.com/file/repo1.maven.org/maven2/commons-io/commons-io/1.4/org/apache/commons/io/IOUtils.java#IOUtils.copyLarge%28java.io.InputStream%2Cjava.io.OutputStream%29
public static long copyLarge(InputStream input, OutputStream output)
throws IOException {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
long count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
Also, make sure to close your buffers explicitly.

RandomAccessFile in Android raw resource file

I tried to create a RandomAccessFile object from a raw resource file in android resource directory without success.
I'm only able to get a inputstream object from raw resource file.
getResources().openRawResource(R.raw.file);
Is it possible to create a RandomAccessFile object from raw asset file or Do I need to stick with inputstream?
It's simply not possible to seek forward and back in an input stream without buffering everything in between into memory. That can be extremely costly, and isn't a scalable solution for reading a (binary) file of some arbitrary size.
You're right: ideally, one would use a RandomAccessFile, but reading from the resources provides an input stream instead. The suggestion mentioned in the comments above is to use the input stream to write the file to the SD card, and randomly access the file from there. You could consider writing the file to a temporary directory, reading it, and deleting it after use:
String file = "your_binary_file.bin";
AssetFileDescriptor afd = null;
FileInputStream fis = null;
File tmpFile = null;
RandomAccessFile raf = null;
try {
afd = context.getAssets().openFd(file);
long len = afd.getLength();
fis = afd.createInputStream();
// We'll create a file in the application's cache directory
File dir = context.getCacheDir();
dir.mkdirs();
tmpFile = new File(dir, file);
if (tmpFile.exists()) {
// Delete the temporary file if it already exists
tmpFile.delete();
}
FileOutputStream fos = null;
try {
// Write the asset file to the temporary location
fos = new FileOutputStream(tmpFile);
byte[] buffer = new byte[1024];
int bufferLen;
while ((bufferLen = fis.read(buffer)) != -1) {
fos.write(buffer, 0, bufferLen);
}
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {}
}
}
// Read the newly created file
raf = new RandomAccessFile(tmpFile, "r");
// Read your file here
} catch (IOException e) {
Log.e(TAG, "Failed reading asset", e);
} finally {
if (raf != null) {
try {
raf.close();
} catch (IOException e) {}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {}
}
if (afd != null) {
try {
afd.close();
} catch (IOException e) {}
}
// Clean up
if (tmpFile != null) {
tmpFile.delete();
}
}
Why not get a new AssetFileDescriptor each time you need a seek? It seems not to be a cpu cycles intensive task (or is it?)
//seek to your first start position
InputStream ins = getAssets().openFd("your_file_name").createInputStream();
isChunk.skip(start);
//read some bytes
ins.read(toThisBuffer, 0, length);
//later on
//seek to a different position, need to openFd again!
//because createInputStream can be called on asset file descriptor only once.
//This resets the new stream to file offset 0,
//so need to seek (skip()) to a new position relative to file beginning.
ins = getAssets().openFd("your_file_name").createInputStream();
ins.skip(start2);
//read some bytes
ins.read(toThatBuffer, 0, length);
I've used this method in my app that needs random access to a 20Mb resource file hundreds of times per second.

Downloading an audio file from raw folder to SD card

I need to download a file from an application. I have 5 audio files in a raw folder. On the onclick event of a button I need to select one audio file from 5 files and download it to an SD card.
How can I acheive this?
this is so simple but mistakefull ... try this code :
File directoryTest = new File(
Environment.getExternalStorageDirectory(), "raw2sd");
try {
//coping sound file to sd
//defining specific directory
File soundDir = new File(directoryTest, "ORG");
//making directories
soundDir.mkdirs();
FileOutputStream sound = new FileOutputStream(
soundDir.getPath() + "/soundName.mp3");
InputStream is = getResources().openRawResource(R.raw.soundFile);
int a = is.available();
byte[] buf = new byte[a];
is.read(buf, 0, a);
sound.write(buf);
sound.flush();
sound.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
this is 100% tested.

Categories

Resources