I have an android app, that should use google fusion tables. I'm using a google service account and have to get the path of my xxxxxxxxxxxprivatekey.p12.
public class CredentialProvider {
...
private static String PRIVATE_KEY = "xxxxxxxxxxxprivatekey.p12";
...
public static GoogleCredential getCredential() throws IOException, GeneralSecurityException {
return getCredential(Arrays.asList(SCOPES), SERVICE_ACCOUNT_EMAIL, new File(PRIVATE_KEY), HTTP_TRANSPORT, JSON_FACTORY);
}
...
}
The code wants to make a new File out of the PRIVATE_KEY path. I've tried various paths but every time I'm getting a FileNotFoundException and open failed: ENOENT (No such file or directory).
I have read something about the assets folder, but I don't know how to get that work with the getCredential method.
Where I have to put my private key in my android project and
how has the PRIVATE_KEY path to look like and
how I get "new File(PRIVATE_KEY)" work?
Thanks ;)
EDIT:
now I'm overriding GoogleCredential.Builder to create my own setServiceAccountPrivateKeyFromP12File(InputStream p12File) like in your link and it seems to work fine. But in getCredential() refreshToken() is called and crashes in a NetworkOnMainThreadException.
I've read, that I should use AsyncTask for it. Can you give me a hint, where I have to put that AsyncTask and what should be inside doInBackground() and what inside onPostExecute() or any method?
Here is the code of getCredential(). It crashes in refreshToken() with a NetworkOnMainThreadException:
public static GoogleCredential getCredential(List<String> SCOPE, String SERVICE_ACCOUNT_EMAIL,
InputStream inputStreamFromP12File, HttpTransport HTTP_TRANSPORT, JsonFactory JSON_FACTORY)
throws IOException, GeneralSecurityException {
// Build service account credential.
MyGoogleCredentialBuilder builder = new MyGoogleCredentialBuilder();
builder.setTransport(HTTP_TRANSPORT);
builder.setJsonFactory(JSON_FACTORY);
builder.setServiceAccountId(SERVICE_ACCOUNT_EMAIL);
builder.setServiceAccountScopes(SCOPE);
builder.setServiceAccountPrivateKeyFromP12File(inputStreamFromP12File);
GoogleCredential credential = builder.build();
credential.refreshToken();
return credential;
}
EDIT2:
Finally, I solved it that way:
private static class RefreshTokenTask extends AsyncTask<GoogleCredential, Void, Void> {
#Override
protected Void doInBackground(GoogleCredential... params) {
try {
params[0].refreshToken();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
and in my getCredential method:
new RefreshTokenTask().execute(credential);
You can't access assets as you would access regular files, since these files are bundled with the application.
That's why new File(PRIVATE_KEY) doesn't work, and there is no path you can give that would make it work.
What you could do is get an InputStream for that file :
AssetManager assetManager = context.getAssets();
InputStream input = assetManager.open(PRIVATE_KEY);
If you need to access it as a File, you could copy it to the internal storage of your app the first time your application is launched. I'm not sure that's the best solution (perhaps you don't want to store the private key on the device's internal storage for security reasons), but it should work. Then you can access it from the context.getFilesDir () folder.
InputStream fis = assetManager.open(PRIVATE_KEY);
FileOutputStream fos = openFileOutput(PRIVATE_KEY, Context.MODE_PRIVATE);
byte buf[] = new byte[1024];
int len;
while ((len = fis.read(buf)) > 0) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
Related
I am basically trying to read a long list of numbers(doubles)from a text file and save them into an array. I have these lines of code but it doesn't work when I load into my android smartphone. The readfile() does work completely when I use debug mode to check if my code reads the ExamScore, it does read and store the values as expected in my laptop. When it loads into smartphone, it just doesn't work. I save my ExamScore.txt in the root directory of android studio, for example, Users->AndroidStudioProjects->Project A. The main concern I have is that:
How do I know if this ExamScore.txt is saved into my smartphone as well when I build the app? Do I have to save the text file into my smartphone separately or something?The error I get is
java.io.FileNotFoundException: ExamScore.txt: open failed: ENOENT (No such file or directory)
static double[] readfile() throws FileNotFoundException{
Scanner scorefile = new Scanner(new File("ExamScore.txt"));
int count = -1;
double[] score = new double[8641];
while (scorefile.hasNext()) {
count = count + 1;
score[count] = Double.parseDouble(scorefile.nextLine());
}
scorefile.close();
return score;
}
In my main code,
double []score=readfile();
I save my ExamScore.txt in the root directory of android studio, for example, Users->AndroidStudioProjects->Project A... How do I know if this ExamScore.txt is saved into my smartphone as well when I build the app?
It isn't.
You need to create an assets folder.
Refer: Where do I place the 'assets' folder in Android Studio?
And you would use getAssets() to read from that folder.
public class MainActivity extends Activity {
private double[] readfile() throws FileNotFoundException{
InputStream fileStream = getAssets().open("ExamScore.txt");
// TODO: read an InputStream
}
}
Note: that is a read-only location of your app.
Or you can use the internal SD card.
How do I read the file content from the Internal storage - Android App
EDIT With refactored code in other answer
public static List<Double> readScore(Context context, String filename) {
List<Double> scores = new ArrayList<>();
AssetManager mgr = context.getAssets();
try (
BufferedReader reader = new BufferedReader(
new InputStreamReader(mgr.open(fileName)));
) {
String mLine;
while ((mLine = reader.readLine()) != null) {
scores.add(Double.parseDouble(mLine));
}
} catch (NumberFormatException e) {
Log.e("ERROR: readScore", e.getMessage());
}
return scores;
}
And then
List<Double> scores = readScore(MainActivity.this, "score.txt");
For those who are wondering, this is my solution! Thank you all for your help!!!! The issue I had was I didn't write it in the main activity but wrote the code in other java file. After writing this in the main activity file and putting my text file inside the assets folder. The issue is resolved :
public static LinkedList<Double> score=new LinkedList<Double>();
public void readScore() throws java.io.IOException {
BufferedReader reader = new BufferedReader(
new InputStreamReader(getAssets().open("score.txt")));
String mLine;
while ((mLine = reader.readLine()) != null) {
score.add(Double.parseDouble(mLine));
}
reader.close();
}
Hi Iam having serious issues try to persist some serializable objects to a file on the local android file system. Iam getting a Bad file descriptor error and I think it is to do with my methods for creating the file. the file and checking if the file exists. i create a private file object in the class. Then, on write or read. I check file existance with the following code.
#Override
public boolean fileExists() {
File file = context.getFileStreamPath(filename);
return file.exists();
}
this doesnt instantiate my file object called "objectfile"!! but does check the "filename" exists.
to create the file I call this method if "filename" doesnt exist.
public void createFile()
{
objectfile = new File(context.getFilesDir(), filename);
objectfile.setReadable(true);
objectfile.setWritable(true);
}
Iam not sure if this will give me back my previously created file which would be ideally what I want to do. Is there a way i can just get the old file or create a new one and pass it to "objectfile" variable in the constructor??
Iam also wondering what the best way to do this is??
Or should i just use the mysqlite db? using object file persistance doesn't seem to be working out for me right now and iam working to a deadline. Also this method is mention in the gooogle docs so I thought it would be legit was to do it.
http://developer.android.com/training/basics/data-storage/files.html
here is my method for reading the serializable objects
public synchronized ArrayList<RoomItem> readObjects() {
final ArrayList<RoomItem> readlist = new ArrayList<>();
if(!fileExists())
return readlist;
if(objectfile == null)
createFile();
try {
finputstream = new FileInputStream(objectfile);
instream = new ObjectInputStream(finputstream);
readwritethread = new Thread(new Runnable() {
#Override
public void run() {
try {
final ArrayList<RoomItem> readitems = (ArrayList<RoomItem>) instream.readObject();
instream.close();
finputstream.close();
handler.post(new Runnable() {
#Override
public void run() {
listener.updateList(readitems);
}
});
} catch(IOException e)
{
e.printStackTrace();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
Log.d("read failed", "file read failed");
}
}
});
}
catch(Exception e)
{
e.printStackTrace();
}
timeOutReadWrite(readwritethread);
readwritethread.start();
try {
readwritethread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("read from file", "file read");
return readlist;
if anyone could suggest any improvements id really appreciate it. I use a handler to pass back to my activity and implement a listener interface on my activity thats call the activity when all the obj are read. Thanks again!
1#: Yes, it will return the original file you created.
2#: Depends on the thing you want to store, seems File is more flex from description
hope helpful.
We have used
FileOutputStream fos = context.openFileOutput("file.ser", Context.MODE_PRIVATE);
to write our serialized files.This will carete files in /data/data/app.package.name/files/. In fact, this path is returned by getFilesDir().
And while deserializing, use
//make sure you pass the same file that was passed to openFileOutput()..
FileInputStream fis = context.openFileInput("file.ser");
Also, to avoid confusing between file names you can use name of class that is being serialized.
Ex:
public static <T> void serialize(final Context context, final T objectToSerialize) {
....
....
Strin fileName = objectToSerialize.getClass().getSimpleName();
...
}
Do this and keep the method in util so it can be used for any type of objects (T type) to serialize.
I implemented a JSON interface for getting model data over http in one of my android projects.
this works so far and I would like to write some tests. I created a test project as suggested in the android documentation. for testing the JSON interface I need some test data which I would like to put in a file.
my research showed up that it's best to put these files in the assets folder of the android test project. to access files in the assets folder one should extend the test class by InstrumentationTestCase. then it should be possible to access the files by calling getAssets().open() on a resources object. so I came up with the following code:
public class ModelTest extends InstrumentationTestCase {
public void testModel() throws Exception {
String fileName = "models.json";
Resources res = getInstrumentation().getContext().getResources();
InputStream in = res.getAssets().open(fileName);
...
}
}
unfortunately I'm getting an "no such file or directory (2)" error when trying to access "models.json" file. (/assets/models.json)
when getting a list of the available files by
String[] list = res.getAssets().list("");
"models.json" is listed in there.
I'm running these tests on Android 4.2.2 api level 17.
public static String readFileFromAssets(String fileName, Context c) {
try {
InputStream is = c.getAssets().open(fileName);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String text = new String(buffer);
return text;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Then use the following code:
JSONObject json = new JSONObject(Util.readFileFromAssets("abc.txt", getApplicationContext()));
please use below code:
AssetManager assetManager = getResources().getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open("foo.txt");
if ( inputStream != null)
Log.d(TAG, "It worked!");
} catch (IOException e) {
e.printStackTrace();
}
I started developing an android app that have to interact with MMS attachements, in particular, get attachements such as text, bitmaps, audio, video etc. and store them on the phone in a specific folder.
So i started reading some books and some post on the web but it isn't a very common argument, and i didn't find an official way to do what i want to do.
I found a fairly good article here on stack-overflow here: How to Read MMS Data in Android?... it works very well for me, but there are 2 problems:
The article shows you how to get MMS data by querying over the "hidden" SMS-MMS content provider, and as far as i know, Google doesn't guarantee that they'll keep the current structure in every android's future relase.
The article only explains how to get Text data and Bitmap data from MMS...what about video/audio? I tried to get a video/audio stream from an InputStream such as the example did with Bitmaps, unfortunately with no luck...
I'm very disappointed about the absence of official tutorial or "How-To" over this argument because SMS and MMS management is a very common need in mobile developement.
I hope someone can help me....
Thanks in advance!!
I found a fairly simple way to read Video/Audio data from MMS, so i decided to publish this part of my class that provides MMS attachements, for all users that need this.
private static final int RAW_DATA_BLOCK_SIZE = 16384; //Set the block size used to write a ByteArrayOutputStream to byte[]
public static final int ERROR_IO_EXCEPTION = 1;
public static final int ERROR_FILE_NOT_FOUND = 2;
public static byte[] LoadRaw(Context context, Uri uri, int Error){
InputStream inputStream = null;
byte[] ret = new byte[0];
//Open inputStream from the specified URI
try {
inputStream = context.getContentResolver().openInputStream(uri);
//Try read from the InputStream
if(inputStream!=null)
ret = InputStreamToByteArray(inputStream);
}
catch (FileNotFoundException e1) {
Error = ERROR_FILE_NOT_FOUND;
}
catch (IOException e) {
Error = ERROR_IO_EXCEPTION;
}
finally{
if (inputStream != null) {
try {
inputStream.close();
}
catch (IOException e) {
//Problem on closing stream.
//The return state does not change.
Error = ERROR_IO_EXCEPTION;
}
}
}
//Return
return ret;
}
//Create a byte array from an open inputStream. Read blocks of RAW_DATA_BLOCK_SIZE byte
private static byte[] InputStreamToByteArray(InputStream inputStream) throws IOException{
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[RAW_DATA_BLOCK_SIZE];
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
return buffer.toByteArray();
}
In this way you can extract "Raw" data such as Audio/Video/Images from MMS by passing:
the context where you need to use this function
the URI of the MMS part that contains data you want to extract (for ex. "content://mms/part/2")
the byref param that returns an eventual error code thrown by the procedure.
Once you have your byte[], you can create an empty File and then use a FileOutputStream to write the byte[] into it. If the file path\extension is correct and if your app has all the right
permissions, you'll be able to store your data.
PS. This procedure has been tested a few times and it worked, but i don't exclude can be some unmanaged exception cases that may produce error states. IMHO it can be improoved too...
Is there any unique id on Android Mobile?? If it is then how many digit it has?
How can I access that through my program??
Thanks
Deepak
There are several problems that occur when using IMEI, IMSI... that are described here:
http://android-developers.blogspot.pt/2011/03/identifying-app-installations.html
The recommended approach is to use:
http://developer.android.com/reference/android/provider/Settings.Secure.html#ANDROID_ID
String unique_id = android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
check IMEI.
http://www.anddev.org/tinytut_-_getting_the_imsi_-_imei_sim-device_unique_ids-t446.html
For detailed instructions on how to get a Unique Identifier for each Android device your application is installed from, see this official Android Developers Blog posting:
http://android-developers.blogspot.com/2011/03/identifying-app-installations.html
It seems the best way is for you to generate one your self upon installation and subsequently read it when the application is re-launched.
I personally find this acceptable but not ideal. No one identifier provided by Android works in all instances as most are dependent on the phone's radio states (wifi on/off, cellular on/off, bluetooth on/off). The others like Settings.Secure.ANDROID_ID must be implemented by the manufacturer and are not guaranteed to be unique.
The following is an example of writing data to an INSTALLATION file that would be stored along with any other data the application saves locally.
public class Installation {
private static String sID = null;
private static final String INSTALLATION = "INSTALLATION";
public synchronized static String id(Context context) {
if (sID == null) {
File installation = new File(context.getFilesDir(), INSTALLATION);
try {
if (!installation.exists())
writeInstallationFile(installation);
sID = readInstallationFile(installation);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return sID;
}
private static String readInstallationFile(File installation) throws IOException {
RandomAccessFile f = new RandomAccessFile(installation, "r");
byte[] bytes = new byte[(int) f.length()];
f.readFully(bytes);
f.close();
return new String(bytes);
}
private static void writeInstallationFile(File installation) throws IOException {
FileOutputStream out = new FileOutputStream(installation);
String id = UUID.randomUUID().toString();
out.write(id.getBytes());
out.close();
}
}