Posts

Showing posts with the label File

Android: Getting A File URI From A Content URI?

Answer : Just use getContentResolver().openInputStream(uri) to get an InputStream from a URI. http://developer.android.com/reference/android/content/ContentResolver.html#openInputStream(android.net.Uri) This is an old answer with deprecated and hacky way of overcoming some specific content resolver pain points. Take it with some huge grains of salt and use the proper openInputStream API if at all possible. You can use the Content Resolver to get a file:// path from the content:// URI: String filePath = null; Uri _uri = data.getData(); Log.d("","URI = "+ _uri); if (_uri != null && "content".equals(_uri.getScheme())) { Cursor cursor = this.getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null); cursor.moveToFirst(); filePath = cursor.getString(0); cursor.close(); } else { filePath = _uri.getPath(); } Log.d(...

Android Studio 3 (All Versions) - Device File Explorer Nothing To Show

Answer : After struggling with this for a day, the answer for me was associated with disabling SuperUser permissions for the ADB shell on my Android Device. If you have rooted your device and are using an app like SuperSU, try disabling the SU permission for ADB shell in the apps view list. First disconnect your Android device from the computer running Android Studio On your Android device open SuperSU and select the apps tab. Click on ADB shell and select Access:'deny' Reconnect your device to your dev computer using a USB cable In Android Studio open the "Device File Explorer". You should now see a list of files on the device, including files in /data/data/ and its subfolders You would think that having SU permissions would be a good thing: but in this case, no. Disable ADB shell's SU permissions in SuperSU AS file explorer will execute su 0 sh -c 'ls -l /' to list the files. For unknown reason, SuperSu remove the single quotes and...

Angular: How To Download A File From HttpClient?

Answer : Blobs are returned with file type from backend. The following function will accept any file type and popup download window: downloadFile(route: string, filename: string = null): void{ const baseUrl = 'http://myserver/index.php/api'; const token = 'my JWT'; const headers = new HttpHeaders().set('authorization','Bearer '+token); this.http.get(baseUrl + route,{headers, responseType: 'blob' as 'json'}).subscribe( (response: any) =>{ let dataType = response.type; let binaryData = []; binaryData.push(response); let downloadLink = document.createElement('a'); downloadLink.href = window.URL.createObjectURL(new Blob(binaryData, {type: dataType})); if (filename) downloadLink.setAttribute('download', filename); document.body.appendChild(downloadLink); downloadLink.click(); }...

C# - How To Get Program Files (x86) On Windows 64 Bit

Answer : The function below will return the x86 Program Files directory in all of these three Windows configurations: 32 bit Windows 32 bit program running on 64 bit Windows 64 bit program running on 64 bit windows   static string ProgramFilesx86() { if( 8 == IntPtr.Size || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432")))) { return Environment.GetEnvironmentVariable("ProgramFiles(x86)"); } return Environment.GetEnvironmentVariable("ProgramFiles"); } If you're using .NET 4, there is a special folder enumeration ProgramFilesX86: Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) Environment.GetEnvironmentVariable("PROGRAMFILES(X86)") ?? Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)

C Puts() Without Newline

Answer : Typically one would use fputs() instead of puts() to omit the newline. In your code, the puts(input); would become: fputs(input, stdout); puts() adds the newline character by the library specification. You can use printf instead, where you can control what gets printed with a format string: printf("%s", input); You can also write a custom puts function: #include <stdio.h> int my_puts(char const s[static 1]) { for (size_t i = 0; s[i]; ++i) if (putchar(s[i]) == EOF) return EOF; return 0; } int main() { my_puts("testing "); my_puts("C puts() without "); my_puts("newline"); return 0; } Output: testing C puts() without newline

Can't Delete File From External Storage In Android Programmatically

Answer : Using ContentResolver to delete media files is wrong and provides many problems for the user. You can not delete a file on the sd-card simply by deleting its information from the ContentResolver on Android versions greater than Jelly Bean(4.3) . It works only on Android versions prior to KitKat(4.4) . That's why the Android team provided DocumentProvider. Why contentResolver.delete(...) is wrong? 1. Fills up the sd-card When you try to delete a media file on the sd-card by the ContentResolver on Android versions greater than 4.3, the actual media file will remain untouched because the contentResolver.delete(...) approach only removes the information (name, date, path ...) of the media and you will end up having unregistered media files on your sd-card which ContentResolver has no idea about their existence anymore and that's why you couldn't see them in your gallery and you think they've been deleted with this approach while they're stil...

Creating A Pdf File In Android Programmatically And Writing In It

Answer : So apparently the code I was using wasn't compatible with android, hence the error I was getting. Below you'll find the correct code that actually works right (for creating a pdf file, putting some content in it, saving in and the opening the newly created file): PS: For this you'll need to add the jar of iTextG to your project: // Method for creating a pdf file from text, saving it then opening it for display public void createandDisplayPdf(String text) { Document doc = new Document(); try { String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Dir"; File dir = new File(path); if(!dir.exists()) dir.mkdirs(); File file = new File(dir, "newFile.pdf"); FileOutputStream fOut = new FileOutputStream(file); PdfWriter.getInstance(doc, fOut); //open the document doc.open(); Paragraph p...