imageview from URI catch file not found - android

imageView = (ImageView) v.findViewById(R.id.lbl2);
try {
Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(Uri.parse(c.getPhotoUrl())));
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// handle errors
Toast.makeText(Chat.this, "File not found", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// handle errors
Toast.makeText(Chat.this, "Error : "+e, Toast.LENGTH_SHORT).show();
}
the URI is jpeg file I'm trying decode it into bitmap
and it keeps catching file not found

Related

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?

Save custom imageview bitmap on buttonclik()

I have created image view with custom shape using
siyamed/android-shape-imageview
Now i want to save the image on SD card with shape.
How can i achieve this?
As the custom view is just an ImageView, try adding something like this to your buttons on click event to save the image as a bitmap;
ImageView imageViewCustom = (ImageView) findViewById(R.id.image_view_to_save);
try {
imageViewCustom.setDrawingCacheEnabled(true);
FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+"/ACustomShapeFolder/file1234.jpg"));
Bitmap bitmap = imageViewCustom.getDrawingCache();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Toast.makeText(this, "Error occured", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(this, "Error occured", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
Hope this helps!

File not found when uploading database file into dropbox

I have completed one android application and integrated DropBox to my application to upload a database.When i am uploading a single file it will be upload correctly.My problem is when i get the db file from my application and uploading this to dropbox it show file not found exeception.I am also using this link but not get solution.
Link
FileInputStream inputStream = null;
try {
String databasePath=getDatabasePath("databaseTaskApps.db").getPath();
Log.i(TAG,"DatabasePath:"+databasePath);
File file = new File(databasePath+ "/databaseTaskApps");
inputStream = new FileInputStream(file);
com.dropbox.client2.DropboxAPI.Entry newEntry = mApi.putFileOverwrite("/databaseTaskApps", inputStream,
file.length(), null);
Log.i("DbExampleLog", "The uploaded file's rev is: " + newEntry.rev);
} catch (DropboxUnlinkedException e) {
// User has unlinked, ask them to link again here.
Toast.makeText(getApplicationContext(), "Not Uploading", Toast.LENGTH_SHORT).show();
} catch (DropboxException e) {
Log.e("DbExampleLog", "Something went wrong while uploading.");
Toast.makeText(getApplicationContext(), "Not Uploading", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
Log.e("DbExampleLog", "File not found.");
Toast.makeText(getApplicationContext(), "Not Uploading", Toast.LENGTH_SHORT).show();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
}
}
}
Hai i found answer for my question
File[] files = new File("/data/data/com.dropbox.android.sample/databases/").listFiles();
for (File f:files) {
if (f.getName().equals("databaseTaskApps"))
{
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(f);
com.dropbox.client2.DropboxAPI.Entry newEntry = mApi.putFileOverwrite("/databaseTaskApps", inputStream,
f.length(), null);
Log.i("DbExampleLog", "The uploaded file's rev is: " + newEntry.rev);
} catch (DropboxUnlinkedException e) {
// User has unlinked, ask them to link again here.
Toast.makeText(getApplicationContext(), "Not Uploading", Toast.LENGTH_SHORT).show();
} catch (DropboxException e) {
Log.e("DbExampleLog", "Something went wrong while uploading.");
Toast.makeText(getApplicationContext(), "Not Uploading", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
Log.e("DbExampleLog", "File not found.");
Toast.makeText(getApplicationContext(), "Not Uploading", Toast.LENGTH_SHORT).show();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
}
}
}
}
}

Taking screenshot then trying to save it in emulator gallery but it is not saving

Here is my code to take screen shot and save it in gallery.tried by debugging here screen is captured but not saving to gallery.
#Override
public void onClick(View v) {
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
}
making screenshot
protected Bitmap takeScreenshot() {
// TODO Auto-generated method stub
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
Toast.makeText(getApplicationContext(), "Screen captured", Toast.LENGTH_LONG).show();
return rootView.getDrawingCache();
}
Saving screen here
protected void saveBitmap(Bitmap bitmap) {
// TODO Auto-generated method stub
File imagePath = new File(Environment.getExternalStorageDirectory()
+ "/screenshot.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
Toast.makeText(getApplicationContext(), "Screen saved", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
Run your code in device please, it may be the problem of emulator, because emulator dont have physical SD Card.

Setting the Wallpaper from an Image on the SDCard

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();
}

Categories

Resources