Write File in Java Android, Read the File with Phonegap - android

I'm trying to write a json file using android like this:
String jsonString = broadcastDataArray.toString();
Writer output = null;
File fileAnnouncement = new File("announcement.json");
try {
output = new BufferedWriter(new FileWriter(fileAnnouncement));
output.write(jsonString);
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
and now i'm trying to load/read the file using phonegap. Using this method shown here, i am able to read a file that was included in my application source. But i want to read some file that was generated by the android code.
Is there anyway to do this? Is there any specific directory where the file could be accessed from Java android and Phonegap/Sencha ?
Any kind of help or pointer is appreciated. Thanks.

You can do this in following way.
Write this function in your Android activity to write data in text file.
public void WriteDataToFile(String sFileName, String sBody){
try
{
FileWriter f = new FileWriter("/sdcard/data.txt");
f.append(sBody);
f.flush();
f.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
And, now you can read that file from your Sencha/Phonegap app by calling this function.
var user_data = function(){
var request = new XMLHttpRequest();
request.open("GET", "/sdcard/data.txt");
request.onreadystatechange = function() {
if (request.readyState == 4) {
// alert("*" + request.responseText + "*");
data = request.responseText;
}
}
request.send();
}

Related

new BlobStoreManager read write on Android 11

I previously used external storage to store specific data that I would like to share between my applications (without having any contentprovider "host")
File folder = new File(Environment.getExternalStorageDirectory(), "FOLDER_NAME");
File file = new File(folder, "FILE_NAME.dat");
FileOutputStream outputStream = new FileOutputStream(file);
That is why I am trying to use BlobStoreManager, as suggested in google's recommendation for targeting 30 (https://developer.android.com/training/data-storage/shared/datasets)
The read & write are based on a BlobHandle with 4 parameters, one being MessageDigest based on a "content". BlobHandle must use the same 4 parameters, or read will fail (SecurityException).
I managed to write data, and to read it, but it makes no sense:
It seems that in order to write, I need to use the data I want to write to generate the BlobHandle.
Then, to read, as BlobHandle must use the same 4 parameters, I also need the data I wrote to be able to read.
Totally illogic, as I wanted to read this data, I don't have it!
I must miss something or just do not understand how it work. If someone can help :)
Here are my sample:
If I set the following:
createBlobHandle: content = "mydata"
write: data = "mydata"
Then write will success, and read will success too. But it I can not know the value before reading it in a normal usecase :(
If I set the following (which would be logic, at least to me):
createBlobHandle: content = "somekey"
write: data = "mydata"
Then write will fail :(
#RequiresApi(api = Build.VERSION_CODES.R)
private BlobHandle createBlobHandle() {
//Transfer object
String content = "SomeContentToWrite";
String label = "label123";
String tag = "test";
//Sha256 summary of the transmission object
try {
byte[] contentByte = content.getBytes("utf-8");
MessageDigest md = MessageDigest.getInstance("sha256");
byte[] contentHash = md.digest(contentByte);
return BlobHandle.createWithSha256(contentHash, label,0, tag);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
private void write() {
String data = "SomeContentToWrite";
#SuppressLint("WrongConstant") final BlobStoreManager blobStoreManager = ((BlobStoreManager) applicationContext.getSystemService(Context.BLOB_STORE_SERVICE));
//Generate the session of this operation
try {
BlobHandle blobHandle = createBlobHandle();
if (blobHandle == null)
return;
long sessionId = blobStoreManager.createSession(blobHandle);
try (BlobStoreManager.Session session = blobStoreManager.openSession(sessionId)) {
try (OutputStream pfd = new ParcelFileDescriptor.AutoCloseOutputStream(session.openWrite(0, data.getBytes().length))) {
//The abstract of the written object must be consistent with the above, otherwise it will report SecurityException
Log.d(TAG, "writeFile: >>>>>>>>>>text = " + data);
pfd.write(data.getBytes());
pfd.flush();
//Allow public access
session.allowPublicAccess();
session.commit(applicationContext.getMainExecutor(), new Consumer<Integer>() {
#Override
public void accept(Integer integer) {
//0 success 1 failure
Log.d(TAG, "accept: >>>>>>>>" + integer);
}
});
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private String read() {
String data = "";
#SuppressLint("WrongConstant") final BlobStoreManager blobStoreManager = ((BlobStoreManager) applicationContext.getSystemService(Context.BLOB_STORE_SERVICE));
BlobHandle blobHandle = createBlobHandle();
if (blobHandle != null) {
try (InputStream pfd = new ParcelFileDescriptor.AutoCloseInputStream(blobStoreManager.openBlob(createBlobHandle()))) {
//Read data
byte[] buffer = new byte[pfd.available()];
pfd.read(buffer);
String text = new String(buffer, Charset.forName("UTF-8"));
Log.d(TAG, "readFile: >>>>>>>>>>>>>>>>>>>>" + text);
} catch (IOException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
}
return data;
}
According to the official training documentation linked in the question, the missing piece of information, at the time of the question having been asked, is that the four pieces of data contained in the BlobHandler need to be uploaded to a server owned by the client application then subsequently downloaded by which ever other application wants to access the blob via the BlobStorageManager.
So it would seem that on-device blob discovery is not supported. There could also be a solution possible using a Content Provider which could offer up the four required pieces of data, thus circumventing the need for the server infrastructure.

CallLog.Calls Call's logs Android

Yesterday I was looking into a way to acquire the call log of an android device.
My idea was to acquire everything posible and then parse it and get only what I really needed.
Following the documentation See CallLog.Calls Documentation I saw the different fields there are but when trying to get them I got erros caused by differences in the documentation.
Finally I got it to work so I wanted to share the code in case anyone else needs it.
In the code I use JSON objects and save them in the Documents folder.
private void getCallLog(Context context) {
#SuppressLint("MissingPermission") Cursor cursor = getApplicationContext().getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null,
null, CallLog.Calls.DATE + " DESC");
if (cursor.getCount() > 0) {
try{
// Get the directory for the user's public pictures directory.
final File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
// Make sure the path directory exists.
if (!path.exists()) {
// Make it, if it doesn't exit
path.mkdirs();
}
final File file = new File(path, "call.txt");
file.createNewFile();
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
while (cursor.moveToNext()) {
try {
JSONObject object = new JSONObject();
object.put("ANSWERED_EXTERNALLY_TYPE", CallLog.Calls.ANSWERED_EXTERNALLY_TYPE);
object.put("BLOCKED_TYPE", CallLog.Calls.BLOCKED_TYPE);
object.put("CACHED_FORMATTED_NUMBER", cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_FORMATTED_NUMBER)));
object.put("CACHED_LOOKUP_URI", cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_LOOKUP_URI)));
object.put("CACHED_MATCHED_NUMBER", cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_MATCHED_NUMBER)));
object.put("CACHED_NAME", cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME)));
object.put("CACHED_NORMALIZED_NUMBER", cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NORMALIZED_NUMBER)));
object.put("CACHED_NUMBER_LABEL", cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NUMBER_LABEL)));
object.put("CACHED_NUMBER_TYPE", cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NUMBER_TYPE)));
object.put("CACHED_PHOTO_ID", cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_PHOTO_ID)));
object.put("CACHED_PHOTO_URI", CallLog.Calls.CACHED_PHOTO_URI); // Wrong docs
object.put("CONTENT_ITEM_TYPE", CallLog.Calls.CONTENT_ITEM_TYPE); // Wrong docs
object.put("CONTENT_TYPE", CallLog.Calls.CONTENT_TYPE); // Wrong docs
object.put("COUNTRY_ISO", cursor.getString(cursor.getColumnIndex(CallLog.Calls.COUNTRY_ISO)));
object.put("DATA_USAGE", cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATA_USAGE)));
object.put("DATE", cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE)));
object.put("DEFAULT_SORT_ORDER", CallLog.Calls.DEFAULT_SORT_ORDER); // Wrong docs
object.put("DURATION", cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION)));
object.put("EXTRA_CALL_TYPE_FILTER", CallLog.Calls.EXTRA_CALL_TYPE_FILTER); // Wrong docs
object.put("FEATURES", cursor.getString(cursor.getColumnIndex(CallLog.Calls.FEATURES)));
object.put("FEATURES_HD_CALL", CallLog.Calls.FEATURES_HD_CALL);
object.put("FEATURES_PULLED_EXTERNALLY", CallLog.Calls.FEATURES_PULLED_EXTERNALLY);
object.put("FEATURES_VIDEO", CallLog.Calls.FEATURES_VIDEO);
object.put("FEATURES_WIFI", CallLog.Calls.FEATURES_WIFI);
object.put("GEOCODED_LOCATION", cursor.getString(cursor.getColumnIndex(CallLog.Calls.GEOCODED_LOCATION)));
object.put("INCOMING_TYPE", CallLog.Calls.INCOMING_TYPE);
object.put("IS_READ", cursor.getString(cursor.getColumnIndex(CallLog.Calls.IS_READ)));
object.put("LAST_MODIFIED", CallLog.Calls.LAST_MODIFIED); // Wrong docs
object.put("LIMIT_PARAM_KEY", CallLog.Calls.LIMIT_PARAM_KEY); // Wrong docs
object.put("MISSED_TYPE", CallLog.Calls.MISSED_TYPE);
object.put("NEW", cursor.getString(cursor.getColumnIndex(CallLog.Calls.NEW)));
object.put("NUMBER", cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)));
object.put("NUMBER_PRESENTATION", cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER_PRESENTATION)));
object.put("OFFSET_PARAM_KEY", CallLog.Calls.OFFSET_PARAM_KEY); // Wrong docs
object.put("OUTGOING_TYPE", CallLog.Calls.OUTGOING_TYPE);
object.put("PHONE_ACCOUNT_COMPONENT_NAME", cursor.getString(cursor.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_COMPONENT_NAME)));
object.put("PHONE_ACCOUNT_ID", cursor.getString(cursor.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_ID)));
object.put("POST_DIAL_DIGITS", CallLog.Calls.POST_DIAL_DIGITS); // Wrong docs
object.put("PRESENTATION_ALLOWED", CallLog.Calls.PRESENTATION_ALLOWED);
object.put("PRESENTATION_PAYPHONE", CallLog.Calls.PRESENTATION_PAYPHONE);
object.put("PRESENTATION_RESTRICTED", CallLog.Calls.PRESENTATION_RESTRICTED);
object.put("PRESENTATION_UNKNOWN", CallLog.Calls.PRESENTATION_UNKNOWN);
object.put("REJECTED_TYPE", CallLog.Calls.REJECTED_TYPE);
object.put("TRANSCRIPTION", cursor.getString(cursor.getColumnIndex(CallLog.Calls.TRANSCRIPTION)));
object.put("TYPE", cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE)));
object.put("VIA_NUMBER", CallLog.Calls.VIA_NUMBER); // Wrong docs
object.put("VOICEMAIL_TYPE", CallLog.Calls.VOICEMAIL_TYPE); // Wrong docs
object.put("VOICEMAIL_URI", cursor.getString(cursor.getColumnIndex(CallLog.Calls.VOICEMAIL_URI)));
myOutWriter.append(object.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
myOutWriter.close();
fOut.flush();
fOut.close();
} catch (Exception e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
}

How to use getClassLoader() correctly?

Here I try to use getClassLoader().getResources() to get my .model file, however it returns null. I'm not sure where goes wrong!
And when I try to print out the urls, it gives me java.lang.TwoEnumerationsInOne#5fd1900, what does this means?
public Activity(MainActivity activity) {
MainActivity activity = new MainActivity();
try {
// Open stream to read trained model from file
InputStream is = null;
// this .model file is save under my /project/app/src/main/res/
Enumeration<URL> urls = Activity.class.getClassLoader().getResources("file.model");
// System.out.println("url:"+urls);
if (urls.hasMoreElements()) {
URL element = urls.nextElement();
is = element.openStream();
}
// deserialize the model
classifier = (J48) SerializationHelper.read(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
In Android, resources put under src/main/res are not visible in the class path and can only be accessed via the android resources API. Try to put the file into src/main/resources.

Android OS - How to track Azure upload progress

I've been working with Azure on the Android OS and I managed to upload my video file (.mp4) to a Container I had already prepared for it.
I did this by getting a Shared Access Signature (SAS) first, which provided me with:
a temporary key
the name of the container to where I want to send the files
the server URI
Then, I started an AsyncTask to send the file to the container using the "upload".
I checked the container, and the file gets uploaded perfectly, no problems on that end.
My question is regarding the progress of the upload. Is it possible to track it? I would like to have an upload bar to give a better UX.
P.S - I'm using the Azure Mobile SDK
Here's my code:
private void uploadFile(String filename){
mFileTransferInProgress = true;
try {
Log.d("Funky Stuff", "Blob Azure Config");
final String gFilename = filename;
File file = new File(filename); // File path
String blobUri = blobServerURL + sharedAccessSignature.replaceAll("\"", "");
StorageUri storage = new StorageUri(URI.create(blobUri));
CloudBlobClient blobCLient = new CloudBlobClient(storage);
//Container name here
CloudBlobContainer container = blobCLient.getContainerReference(blobContainer);
blob = container.getBlockBlobReference(file.getName());
//fileToByteConverter is a method to convert files to a byte[]
byte[] buffer = fileToByteConverter(file);
ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer);
if (blob != null) {
new UploadFileToAzure().execute(inputStream);
}
} catch (StorageException e) {
Log.d("Funky Stuff", "StorageException: " + e.toString());
e.printStackTrace();
} catch (IOException e) {
Log.d("Funky Stuff", "IOException: " + e.toString());
e.printStackTrace();
} catch (Exception e) {
Log.d("Funky Stuff", "Exception: " + e.toString());
e.printStackTrace();
}
mFileTransferInProgress = false;
//TODO: Missing ProgressChanged method from AWS
}
private class UploadFileToAzure extends
AsyncTask <ByteArrayInputStream, Void, Void>
{
#Override
protected Void doInBackground(ByteArrayInputStream... params) {
try {
Log.d("Funky Stuff", "Entered UploadFileToAzure Async" + uploadEvent.mFilename);
//Method to upload, takes an InputStream and a size
blob.upload(params[0], params[0].available());
params[0].close();
} catch (StorageException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Thanks!
You can split your file and send its part using Block, there is a good example of your case in this link but it used C# so you should find the corresponding function in the android library reference.
Basically instead of sending you file as one big file, you split it to multiple files (bytes) and send it to azure so you can track the progress on how many bytes that already sent to azure

Trying to read a text file in res/raw with scanner

Hello,
I am trying to read a file with a Scanner so I can use the input of the strings to construct other objects. However my scanner is always throwing a NullPointerException when trying to create it. I have a pig.txt text file in the res/raw folder but my scanner can not seem to access it. I do not know what I am doing wrong. I have comment out other code of the method but still get an exception.
public void loadAchievements() {
try {
Scanner s = new Scanner(getResources().openRawResource(R.raw.pig));
/**
* s = s.useDelimiter("."); Scanner StringScanner; StringScanner =
* new Scanner(s.next()); StringScanner =
* StringScanner.useDelimiter(":"); String keep =
* StringScanner.next(); String StringKeeper = StringScanner.next();
* this.achievementBoard.add(new Achievement_Item(keep,
* StringKeeper)); StringScanner.close(); s.close();
**/
} catch (NullPointerException e) {
e.printStackTrace();
System.out.println("NULLPOINTER");
}
}
I had this problem today, and I resolved somehow.
I know that old question, but I would share it if others have stuck.
public class Question {
private int numberOfQuestion;
private String[] myquestion;
public Question(InputStream file_name) {
Scanner scanner = null;
try {
scanner = new Scanner(file_name);
} catch (Exception e) {
Log.d("Question", "Scanner :" + e);
System.exit(1);
}
this.numberOfQuestion = scanner.nextInt();
scanner.nextLine();
myquestion = new String[numberOfQuestion];
for (int i = 0; i < numberOfQuestion; ++i) {
myquestion[i] = scanner.nextLine();
}
scanner.close();
}
---------------------------------------------------------
call:
try {
MyScanner myScanner = new MyScanner(getResources().openRawResource( R.raw.input_question));
} catch (Exception e) {
Log.d("Error", "input_question.txt");
}
openRawResource() method can only be used to open drawable, sound, and raw resources; it will fail on string and color resources. Since your pig.txt is a text file that contains a String, openRawResource() won't be able to open a new stream therefore your stream is null.

Categories

Resources