I can't find the path where an image was saved - android

Im trying to save a profile photo, but when I use the save image code I got (i got almost the same example everywhere on the internet), I don't really know where the image is getting stored.
public void saveImage(Bitmap image) {
FileOutputStream out = null;
try {
out = new FileOutputStream("BecityAvatar.png");
image.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
The thing is, I want to store the image so when the profile launches and the layout is loaded, if there is an image stored as the avatar, it loads itself onto the imageview from the layout. If there isn't, nothing will happen. I don't really know where the images will get stored when I save them. Any help or tips will be appreciated.

Your problem is with:
out = new FileOutputStream("BecityAvatar.png");
this way you are creating a FileOutputStream, that points to /, and your application has not right to write there. For instance
File file = new File(getFilesDir(), "BecityAvatar.png")
out = new FileOutputStream(file);
to use getFilesDir(), you need a context

Related

to take screenshot and to draw on it (android)

I want to take screenshot of my WebView by clicking on menu item . And then I want to draw on this screenshot and save it on my sdcard.
I've also found the solution of taking screenshot and saving. Here it is:
private void getScreen(View content) {
Bitmap bitmap = content.getDrawingCache();
File file = new File("/sdcard/screen.png");
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
and on onOptionsItemSelected() method:
View content = findViewById(R.id.webview_main);
content.setDrawingCacheEnabled(true);
getScreen(content);
It`s work ok, but I want to implement drawing on it too. If anyone know, how to do this, tell me please,
I will be very grateful.

How can I get a Picasa image folder on my Marshmallow device?

My application allows users to select an image to upload. When users select an image from a picasa album my data intent comes back with dat=content://com.sec.android.gallery3d.provider/picasa/item/....
Apparently when selecting an image from a picasa folder, I must handle getting the image differently as noted in this answer.
But before I implement a fix, I want to be able to reproduce the crash so I can verify my fix actually works. So how can I get a Picasa folder on my new (marshmallow) Android test device since Picasa has been killed by Google?
The most guaranteed way of getting a file send inside an intent, is to open a stream to it and copy it over to a private folder on your app.
This way works for local file, content uri, picasa, all of it.
Something like that:
private File getSharedFile() {
Uri uri = intent.getExtras().getParcelable(Intent.EXTRA_STREAM);
// or using the new compat lib
Uri uri = ShareCompat.IntentReader(this).getStream();
InputStream is = null;
OutputStream os = null;
try {
File f = ... define here a temp file // maybe getCacheDir();
is = getContentResolver().openInputStream(uri);
os = new BufferedOutputStream(new FileOutputStream(f));
int read;
byte[] bytes = new byte[2048];
while ((read = is.read(bytes)) != -1) {
os.write(bytes, 0, read);
}
return f;
} catch (Exception e) {
... handle exceptions, buffer underflow, NPE, etc
} finally {
try { is.close(); } catch (Exception e) { /* u never know */ }
try {
os.flush();
os.close();
} catch (Exception e) { /* seriously can happen */ }
}
return null;
}

Downloading Images Directly From Android Activity

I'm showing images in my app and I want to add download button after every images when user click on it, image will automatically save to folder. Is it possible?
If you already have the file saved in your application, copy it to this public folder
File imagePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
Then use the technique provided here to scan the picture into the Gallery. Now when the user opens the Gallery they'll see the picture.
I tried Universal Image Loader And Picasso before.
you see that you need and witch one is enough to you.
also to have better decision read this one and this one.
it may help you :
you can use Glide for download image and i think it is better then picasso because it extends picasso.and for more information please see https://github.com/bumptech/glide.
for this you just have to include compile 'com.github.bumptech.glide:glide:3.6.1' into dependencies and then simply add this code line
Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView);`
where http://goo.gl/gEgYUd is URL to pass.and after using this you have not to maintain cache.
enjoy your code:)
private static void persistImage(Bitmap bitmap, String name) {
File filesDir = getAppContext().getFilesDir();
File imageFile = new File(filesDir, name + ".jpg");
OutputStream os;
try {
os = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
}
}
You can use Picasso library to display images. Add this below code in your build.gradle dependencies:
compile 'com.squareup.picasso:picasso:2.4.0'
Now use this to display images,You can add this code inside the onClick() method of your button.
File file = new File(imagePath);
if(file.exists()) {
Picasso.with(context).load(file).skipMemoryCache().placeholder(R.drawable.placeholder).into(yourImageView);
}
else {
Picasso.with(context).load(imageUrl).skipMemoryCache().placeholder(R.drawable.placeholder).into(yourImageView, new PicassoCallBack(yourImageView,imagePath));
}
The picassoCallBack class will look like this :
public class PicassoCallBack extends Callback.EmptyCallback {
ImageView imageView;
String filename;
public PicassoCallBack(ImageView imageView, String filename) {
this.imageView = imageView;
this.filename = filename;
}
#Override public void onSuccess() {
// Log.e("picasso", "success");
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
try {
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos1);
// FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
File file = new File(filename);
FileOutputStream outStream = new FileOutputStream(file);
outStream.write(baos1.toByteArray());
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onError() {
Log.e("picasso", "error");
}
}
Hope it will do your job.

Black portion in place of transparent portion while saving the bitmap

I am frustrated searching almost every google page for this problem.
I need to save my bitmap to file.
I have used this method several times with no problem at all. But now I am having a problem.
The Bitmap I am saving is a round image with transparency and having .png format which I have placed in res folder. But I am getting black portion in place of transparent portion.
This is the code I am using for saving the bitmap.
void saveImage(Bitmap bmp) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bytes);
finalImg = new File(Environment.getExternalStorageDirectory(),
"emoticon_temp" + ".png");
finalImg.createNewFile();
FileOutputStream fo = new FileOutputStream(finalImg);
fo.write(bytes.toByteArray());
fo.flush();
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I am saving this bitmap as file because I need to share this image and for sharing the image I need the Stream Uri.
If you got any other Idea for sharing a bitmap, please let me know.

Saving images dynamically from application to SD card

I'm making a gallery in which I load (on run time) images from asset folder. Now I want to save image to SD card on the click event.
For example: When the app starts, the user see images, they can scroll through images and view them (this part is done). The problem is pictures are loading dynamically in my own gallery view. I have not hard coded them.
I want to save it to the SD card. But I don't have the hard coded path of images. There can be any number of images.
private void CopyAssets() {
AssetManager assetManager = getAssets();
InputStream in=null;
String[] files = null;
try {
files = assetManager.list("image");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for(String filename : files) {
try {
in = assetManager.open(filename);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
String dirName = Environment.getExternalStorageDirectory().toString();
File newFile = new File(dirName);
newFile.mkdirs();
OutputStream out = new FileOutputStream(newFile);
System.out.println("in tryyyy");
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(Exception e) {
Log.e("tag", e.getMessage());
}
I tried above method, I don't want to copy all the image to the SD card. But only that one which the user chooses from the gallery that too dynamically. As there will be a number of images. Hard coding each image path will be tough.
Is there any way in Android, by which I can get the current image path or URI in a string? What is View v = this.getCurrentFocus();? What does it return?
a Gallery extends from AdapterView , and just like on adapterView , you can add a listener for when an item is being selected.
one you know which item was selected , use it in order to copy the image you want to the sd-card.
for better understanding of how to implement the adapter for the adapterView , watch "the world of listView" video . you might want to put the path into the viewHolder (depending on your code and design) .
Here's a method I created that will allow you to save an image (Bitmap) to the memory. The parameters requiere a bitmap object and the filename of that object.
public void writeBitmapToMemory(String filename, Bitmap bitmap) {
FileOutputStream fos;
// Use the compress method on the Bitmap object to write image to the OutputStream
try {
fos = this.openFileOutput(filename, Context.MODE_PRIVATE);
// Writing the bitmap to the output stream
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
I hope this helps.

Categories

Resources