So I have this app, but its to big for the play store because it has lots of pdfs, so I decided to use play asset delivery to retrieve the pdfs. I have done everything like it says in google docs, I have created an asset-package, changed everything in the manifest, build.gradle and build a bundle! But the play asset delivery just isn't working! I can't get my pdf files that are on the "pdfs->src->main->assets" !! Can someone please help me? Am I choosing the right package name?
My source code to retrieve the pdf is the following :
mPDFView = (PDFView) findViewById(R.id.pdf);
Context context = null;
try {
context = createPackageContext(getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
AssetManager assetManager = context.getAssets();
try {
InputStream is = assetManager.open("A abóboda.pdf");
mPDFView.fromStream(is).load();
} catch (IOException e) {
e.printStackTrace();
}
Related
I'm just playing around with Android Studio and I'm trying to figure out how to Download a file into /system.
I understand I need root for this and I already got that part working, the only trouble I'm having is with
request.setDestinationInExternalPublicDir("/system/", "test.jpg");
The goal is to download a file and save it to /system with the file name being test.jpg.
So the end is the file is located at /system/test.jpg.
The issue with this is that DownloadManager is saving it to internal storage and is creating a new folder named 'system'.
I can tell it has something to do with setDestinationInExternalPublicDir but I'm just not sure what to change it to.
Thanks again
What I did is that:
(It's a part in one of my projects)
/*****DOWNLOAD FILE*****/
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("https://github.com/pelya/android-keyboard-gadget/blob/master/hid-gadget-test/hid-gadget-test?raw=true"));
request.setDescription("hid-gadget-test");
request.setTitle("hid-gadget-test");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "hid-gadget-test"); /*****SAVE TO DOWNLOAD FOLDER*****/
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
File mvfile = new File("/sdcard/"+Environment.DIRECTORY_DOWNLOADS+"/hid-gadget-test");
while (!mvfile.exists()) {} /*****WAIT UNTIL DOWNLOAD COMPLETE*****/
try {
Thread.sleep(5000);
} catch (InterruptedException ignored) {}
try { /*****RUN MV-COMMAND TO MOVE TO ROOT DIR*****/
Process su = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
outputStream.writeBytes("mv /sdcard/"+Environment.DIRECTORY_DOWNLOADS+"/hid-gadget-test /data/local/tmp/hid-gadget-test\n");
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
su.waitFor();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "IOException", Toast.LENGTH_SHORT).show();
} catch (InterruptedException e) {
Toast.makeText(getApplicationContext(), "InterruptedException", Toast.LENGTH_SHORT).show();
}
The wait-until-download-complete-thingy is a bit hacky, I found it here. It will maybe not work with files that take more than 5 seconds to download
I'm developing an Android app which allows users to upload/download images to and from a database (powered by Amazon AWS). The problem I'm facing is that I can successfully download the files to a directory
/storage/emulated/0/data/myfile.jpg
But I cannot display them as a new ImageView.
Here are my methods that deal with displaying the methods. Note that RefreshFeedTask.downloadedFiles is a List of Bitmaps as shown here:
do {
objectListing = s3Client.listObjects(listObjectsRequest);
for (S3ObjectSummary objectSummary :
objectListing.getObjectSummaries()) {
keys.add(objectSummary.getKey());
}
listObjectsRequest.setMarker(objectListing.getNextMarker());
} while (objectListing.isTruncated());
Iterator<String> o = keys.iterator();
while(o.hasNext())
{
String n = o.next();
File file = new File(Environment.getExternalStorageDirectory()+"/data/", n);
if(!file.exists())
{
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
TransferObserver observer = transferUtility.download(
existingBucketName,
n,
file);
Bitmap m = BitmapFactory.decodeFile(file.getAbsolutePath());
private void refreshFeed()
{
new RefreshFeedTask(this).start();
for(Bitmap f : RefreshFeedTask.downloadedFiles)
{
displayImage(f);
}
}
private void displayImage(Bitmap f){
ImageView myImage = new ImageView(this);
myImage.setImageBitmap(f);
myImage.setVisibility(View.VISIBLE);
Log.i("Background","Displaying file");
}
Any help is appreciated, as I am somewhat new to Android development, but not Java development.
Looks like you have not added you new ImageView to any view, try adding your new ImageView to any container to display it.
parentLayout.addView(theNewImageView);
Another tip: You can try Glide if you want to display many images more efficiently.
One issue that you have here is Ownership.
Advise: That file you just downloaded to that location, your app may not own that location. If this is a closed ecosystem where you are the sole user and owner of that file, you should probably create an application specific directory upon running the application.
The best way to display something that you do not own in the Sdcard is to use the MediaStore API in Android to access them. You need a URI to the right path and just doing an absolute path is not generally advised, especially for image assets.
A good example of this is here:
https://dzone.com/articles/displaying-images-sd-card
Hey Guys I did a little App, where I type into a textbox a specific value (height, weight) and save it into a file.
I did this but I do not know, which path I have to use for Android.
Hope you can help :)
public void SaveList(View view) {
//Pf`enter code here`ad, im privaten Speicherbereich
File file = new File("I need this path :)");
try {
OutputStreamWriter fdg = new OutputStreamWriter(new FileOutputStream(file));
fdg.write(""+this.weight);
fdg.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
You can use Environment.getDataDirectory() to get the root directory, if you dont have an SD card.
If you have an SD card, use Environment.getExternalStorageState()
Read more about them in the docs
Thus, change your code as follows
File file = new File(Environment.getDataDirectory()+"/your_folder_name/your_file_name");
This will create a file with name your_file_name in the folder your_folder_name in your internal storage.
Is it possible to change default sms application on Android 4.4 without user knowledge? I'm trying to do it on a ROOTED Galaxy S5 using reflection and invoking system methods, so here is what I done so far:
Created small app that implements all things needed for app to be a SMS manager (as per http://android-developers.blogspot.in/2013/10/getting-your-sms-apps-ready-for-kitkat.html) and I can make it default sms app but Android asks me to confirm that in dialog. Now I'm exploring android source code and I found something in this class:
https://github.com/android/platform_packages_apps_settings/blob/2abbacb7d46657e5863eb2ef0035521ffc41a0a8/src/com/android/settings/SmsDefaultDialog.java
So I'm trying to invoke internal method:
SmsApplication.setDefaultApplication(mNewSmsApplicationData.mPackageName, this);
via reflection and here is how I done it so far:
Class[] params = new Class[2];
params[0]=String.class;
params[1]=Context.class;
Class cls = null;
try {
cls = Class.forName("com.android.internal.telephony.SmsApplication");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Method method = null;
try {
method = cls.getDeclaredMethod("getSmsApplicationData", params);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
try {
Object[] params2 = new Object[2];
params2[0]=getPackageName();
params2[1]=getApplicationContext();
Log.d("Current package", getPackageName());
method.invoke(null, params2);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
And I made this app system app and put it into folder /system/priv-app, rebooted phone and started it successfully with adb, but when I click on the button that executes code above, nothing happens. No errors, no log output, but default app is still Messaging (com.android.mms).
Is there any way to do this?
This information contained in file /data/system/users/0/settings_secure.xml. For example, default application for SMS stored by key sms_default_application.
<setting id="85" name="sms_default_application" value="com.google.android.talk" package="com.android.settings" />
More info you can find in this class: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/provider/Settings.java#6067
Accordingly, if you have ROOT permissions, you can edit this file.
Sound does not play when I run the JAR, but it does when I run it in eclipse.
Here is where I load the clips:
public void init(){
System.out.println("grabbing Music");
String currentDir = new File("").getAbsolutePath();
name=new File(currentDir+"\\music\\").list();
clip=new Clip[name.length];
soundFile=new File[name.length];
for(int x=0;x<name.length;x++){
System.out.println(currentDir+"\\music\\"+name[x]);
try {
soundFile[x]= new File(currentDir+"\\music\\"+name[x]);
AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile[x]);
DataLine.Info info= new DataLine.Info(Clip.class, sound.getFormat());
clip[x] = (Clip) AudioSystem.getLine(info);
clip[x].open(sound);
clip[x].addLineListener(new LineListener(){
public void update(LineEvent event) {
if (event.getType() == LineEvent.Type.STOP) {
event.getLine().close();
}
}
});
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
}
}
}
I do not get any errors when running it in Eclipse. There should be no possibility of an invalid directory error, so what is wrong?
-When the jar is run in CMD i get no errors.
edit: I feel like I am loading the audio wrong, hence why I pasted the code I used to load the files in. In my searches I haven't seen anyone use File to load in a sound file. Wonder if that is the problem?
First thing that goes into my mind is that you didn't attached your sound library classes into your jar.
In order to run your current code, the folder music should be in the same folder the jar file is located in.
Another solution is to package your music folder inside the jar file and then change your code to:
InputStream is = getClass().getResourceAsStream("/music/" + name[x]);
AudioInputStream sound = AudioSystem.getAudioInputStream(is);
How about-
Right click on your project in Eclipse. Then New -> Source Folder.
Name the source folder anything. e.g. music_src.
Copy or drag the entire music directory in music_src. Then make the jar.
File systems have a hard time looking into jars.
Try using URL instead. A URL can locate a location within a jar. This happens a lot with folks trying to access resources in jars for the first time.
Otherwise things look fine.