Setting the Wallpaper from an Image on the SDCard - android

How would one go about setting the homescreen wallpaper from an image on the SDcard?
i.e.:
try {
wallpaperManager.setResource("/sdcard/wallpaper/olive.jpg");
finish();
} catch(IOException e) {
e.printStackTrace();
}
hasn't worked, returned an error: 'The method setResource(int) in the type WallpaperManager is not applicable for the arguments (String)'

Bitmap o = BitmapFactory.decodeFile("/sdcard/wallpapers/olive.jpg");
try {
wallpaperManager.setBitmap(o);
finish();
}
catch (IOException e) {
e.printStackTrace();
}

Related

SecurityException with PdfRenderer, comes with password protected pdfs, and than repeats even with normal pdfs

If trying to open Password Protected PDF with PdfRenderer API, gives SecurityException and handled accordingly, inside catch block and thenonDestroy basic clean up is done, and comes back to home activity and then navigating a simple non-protected PDF than again the same exception occurs.
Please note, this happens only once any protected file got opened.
Refer below code:
#Override
protected PdfRenderer doInBackground(Uri... uri) {
Uri uriToProcess = uri[0];
try {
contentResolver=getContentResolver();
parcelFileDescriptor = contentResolver.openFileDescriptor(uriToProcess, "r");
if(parcelFileDescriptor!=null && mPdfRenderer==null) {
mPdfRenderer = new PdfRenderer(parcelFileDescriptor);
}
} catch (FileNotFoundException e) {
exceptionMsg="Sorry! No such file or directory found";
handleExceptionInUI(exceptionMsg, progressDialog);
Log.e("$$$$ FNFException", e.toString());
} catch (IOException e) {
exceptionMsg="Sorry! Something went wrong with IO";
handleExceptionInUI(exceptionMsg, progressDialog);
Log.e("$$$$ IOException", e.toString());
} catch (SecurityException e) {
if (parcelFileDescriptor!=null) {
try {
parcelFileDescriptor.close();
parcelFileDescriptor = null;
contentResolver=null;
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (mPdfRenderer!=null){
mPdfRenderer.close();
mPdfRenderer=null;
}
exceptionMsg="Password protected file, This can't be opened";
handleExceptionInUI(exceptionMsg, progressDialog);
Log.e("$$$$ SecurityException", e.toString());
} catch (Exception e) {
exceptionMsg="Sorry! Something went wrong.";
handleExceptionInUI(exceptionMsg, progressDialog);
Log.e("$$$$ EXCEPTION", e.toString());
}
return mPdfRenderer;
}
Any help cordially appreciated.
I had the same problem with my app. The way I solved it was using an https://github.com/TomRoush/PdfBox-Android and load the document and check for password protection. After no InvalidPasswordException has been thrown the file can be safely loaded with the PdfRenderer.

Not able to set Bitmap to wallpaper using wallpaperManager

I have a string which consists an URL of the image, in the same activity I am viewing the image through URL. But to set the same image as my wallpaper, I am converting the string to Uri and then to Bitmap to use setBitmap.But I am still getting error telling No image was chosen.
Code is below:
newString has the URL of the image.
final String myUrlStr = newString;
URL url;
Uri uri=null;
try {
url = new URL(myUrlStr);
uri = Uri.parse( url.toURI().toString() );
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
try {
image = MediaStore.Images.Media.getBitmap(this.getContentResolver(),uri);
} catch (IOException e) {
e.printStackTrace();
}
setButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
WallpaperManager wallpaperManager=WallpaperManager.getInstance(getApplicationContext());
try {
// Set the image as wallpaper
if(image!=null)
wallpaperManager.setBitmap(image);
else
Toast.makeText(getApplicationContext(), "No image was chosen.", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
It seems like I can't comment. So i answer here.
I am not sure about your url being on internet or local. I don't find anything wrong with your code. So, my deduction is your onclicklistener is set before it can fetch image.(might need to use asyntask to save image) Are you displaying on your imageview from same image resource?

How to catch FileNotFoundException from MediaPlayer?

I am using a MediaPlayer that grabs videos from URLs:
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(this, uri);
}catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
When I try and set a datasource with a url, that for instance, has been taken down, I get the following error in my logcat:
Couldn't open file on client side; trying server side:
java.io.FileNotFoundException: No content provider:
http://example.com/examplevideo.mp4
However, my catch{} does not trap this in the following code above, which I need it to do so I can display an error message etc...
Is there any way to catch this FileNotFoundException in my code?
Thanks in advance
I don't know how to catch this specific exception. But you can use following to catch any type of exception.
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(this, uri);
}catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(this, uri);
}catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
Log.i("your tag",e.toString());
}
and look at the log with the tag you whant
Thanks all for replies.
I forgot to mention that the file is from an external server, but have adopted an approach similar to what Pramod suggested, but altered to suit my needs.
/**
* Checks if an external file on a server exists.
* #param urlString The file URL
* #return true if file exists, false if does not
*/
public boolean checkExternalFileExists(String urlString){
try {
URL url = new URL(urlString);
int response = ((HttpURLConnection)url.openConnection()).getResponseCode();
return response == HttpURLConnection.HTTP_OK;
}
catch (FileNotFoundException e) {
return false;
}catch (MalformedURLException e) {
return false;
} catch (IOException e) {
return false;
}
}
Thanks

Multiple RTSP Streaming not working

I had used 4 VideoView in my Activity.If uri1 is available and other uri's are not available means all video view shows uri1's video.
If other uri's are available but uri1 is not up means all videoview's are not showing anything. Guide me to solve this issue.
String uri1="rtsp://admin:admin#Serverurl1"
String uri2="rtsp://admin:admin#Serverurl2"
String uri3="rtsp://admin:admin#Serverurl3"
String uri4="rtsp://admin:admin#Serverurl4"
videoview1.setVideoURI(Uri.parse(uri1));
videoview2.setVideoURI(Uri.parse(uri2));
videoview3.setVideoURI(Uri.parse(uri3));
videoview4.setVideoURI(Uri.parse(uri4));
Thanks in advance.
You can try this:
vv1=(VideoView)findViewById(R.id.vv1);
vv2=(VideoView)findViewById(R.id.vv2);
vv3=(VideoView)findViewById(R.id.vv3);
vv4=(VideoView)findViewById(R.id.vv4);
try {
vv1.setVideoURI(Uri.parse(url1));
vv1.requestFocus();
vv1.start();
} catch (Exception e) {
// TODO: handle exception
}
try {
vv2.setVideoURI(Uri.parse(url2));
vv2.requestFocus();
vv2.start();
} catch (Exception e) {
// TODO: handle exception
}
try {
vv3.setVideoURI(Uri.parse(url3));
vv3.requestFocus();
vv3.start();
} catch (Exception e) {
// TODO: handle exception
}
try {
vv4.setVideoURI(Uri.parse(url4));
vv4.requestFocus();
vv4.start();
} catch (Exception e) {
// TODO: handle exception
}

Android ENOENT warning

I am new to android trying to make a custom video controller from an opensource snippet, i have added comments where i thought were necessary for understanding the code a little better, hope it will be helpfull
public void playVideo(){
if(videoview.isPlaying()){
videoview.stopPlayback();
}
try {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
File file = new File(path);//path is a string
FileInputStream inputStream = new FileInputStream(file);//log says warning is here
player.setDataSource(inputStream.getFD());
inputStream.close();
player.setOnPreparedListener(this);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Thanks in advance!!
To get an InputStream for a raw resource, use openRawResource(int).
Possibly you could also use MediaPlayer.create(Context,int) to directly create a MediaPlayer with a given resource identifier.

Categories

Resources