I am using Picasso to fetch image and I just want to save the image.Below code is not working for me to save the image.
public class DownloadImage extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mindmaps);
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
Picasso.with(this)
.load("http://i.imgur.com/DvpvklR.png")
.into(imageView);
final Button btntakephoto = (Button) findViewById(R.id.save);
btntakephoto.setOnClickListener((View.OnClickListener) this);
}
public void onClick(View v){
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Drawable image = imageView.getDrawable();
if (image != null && image instanceof BitmapDrawable) {
BitmapDrawable drawable = (BitmapDrawable) image;
Bitmap bitmap = drawable.getBitmap();
try {
File file = new File("path where you want to save");
FileOutputStream stream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, stream);
stream.flush();
stream.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
On pressing the save button image should be saved in the gallery.
MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);
try this,
call this method from button click:
private void saveImage() {
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Drawable image = imageView.getDrawable();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), image);
OutputStream out = null;
String fileName = "MyImage.jpg";
String mPath = "";
try {
out = mActivity.openFileOutput(fileName, Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
File f = mActivity.getFileStreamPath(fileName);
mPath = f.getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
}
Logger.logger("URI :" + mPath);
}
Make your DownloadImage class implement View.OnClickListener interface.
Related
In the code below, you can see that I have created an imageview using a bitmap. What I want to know is how I can save an image of that imageview to my camera roll. Thanks!
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.key_code_zoom);
title = (TextView) findViewById(R.id.accountTitleLarge);
imageView = (ImageView) findViewById(R.id.keyCodeLarge);
Intent callingActivity = getIntent();
Bundle callingBundle = callingActivity.getExtras();
if (callingBundle != null) {
String titleText = callingBundle.getString("title");
byte[] bytes = getIntent().getByteArrayExtra("bitmapbytes");
bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
title.setText(titleText);
imageView.setImageBitmap(bmp);
}
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
supportFinishAfterTransition();
}
});
}
To save an image in gallery, you must first get the bitmap and then save it.
private void imageToRoll(){
imageView.buildDrawingCache();
Bitmap image = imageView.getDrawingCache(); // Gets the Bitmap
MediaStore.Images.Media.insertImage(getContentResolver(), imageBitmap, imagTitle , imageDescription); // Saves the image.
}
Also, set the permission in your manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
If you are looking for some code that will help you save the Image from the ImageView to your phone storage then the following code will help you
public String getImageFromView() {
imageview.setDrawingCacheEnabled(true);
// this is the important code :)
// Without it the view will have a dimension of 0,0 and the bitmap will be null
imageview.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
imageview.layout(0, 0, imageview.getMeasuredWidth(), imageview.getMeasuredHeight());
imageview.buildDrawingCache(true);
//Define a bitmap with the same size as the view
Bitmap b = imageview.getDrawingCache();
String imgPath = getImageFilename();
File file = new File(imgPath);
try {
OutputStream os = new FileOutputStream(file);
b.compress(Bitmap.CompressFormat.JPEG, 90, os);
os.flush();
os.close();
ContentValues image = new ContentValues();
image.put(Images.Media.TITLE, "NST");
image.put(Images.Media.DISPLAY_NAME, imgPath.substring(imgPath.lastIndexOf('/')+1));
image.put(Images.Media.DESCRIPTION, "App Image");
image.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
image.put(Images.Media.MIME_TYPE, "image/jpg");
image.put(Images.Media.ORIENTATION, 0);
File parent = file.getParentFile();
image.put(Images.ImageColumns.BUCKET_ID, parent.toString()
.toLowerCase().hashCode());
image.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, parent.getName()
.toLowerCase());
image.put(Images.Media.SIZE, file.length());
image.put(Images.Media.DATA, file.getAbsolutePath());
Uri result = mContext.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return imgPath;
}
you will have to provide a file path getImageFilename();this function provides the path where the file is suppose to be stored. The ContentValues will be responsible to let the images be scanned in gallery.
Hope this helps.
I am using a Glide library for loading remote URLs into ImageView's.
I want to save the image from this ImageView to gallery. (I don't want to make another network call again to download the same image).
How we can achieve this?
Glide.with(yourApplicationContext))
.load(youUrl)
.asBitmap()
.into(new SimpleTarget<Bitmap>(myWidth, myHeight) {
#Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
//set bitmap to imageview and save
}
};
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, false);
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 60, byteArrayOutputStream);
String fileName = "image.jpeg";
File file = new File("your_directory_path/"
+ fileName);
try {
file.createNewFile();
// write the bytes in file
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(byteArrayOutputStream.toByteArray());
// remember close the FileOutput stream
fileOutputStream.close();
ToastHelper.show(getString(R.string.qr_code_save));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ToastHelper.show("Error");
}
Note : If your drawble is not always an instanceof BitmapDrawable
Bitmap bitmap;
if (mImageView.getDrawable() instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
} else {
Drawable d = mImageView.getDrawable();
bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Try this
I haven't try this way. But i think this match your problem. Put this code on onBindViewHolder of your RecyclerView adapter.
Glide.with(yourApplicationContext))
.load(youUrl)
.asBitmap()
.into(new SimpleTarget<Bitmap>(myWidth, myHeight) {
#Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
//Set bitmap to your ImageView
imageView.setImageBitmap(bitmap);
viewHolder.saveButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//Save bitmap to gallery
saveToGallery(bitmap);
}
});
}
};
This might help you
public void saveBitmap(ImageView imageView) {
Bitmap bitmap = ((GlideBitmapDrawable) imageView.getDrawable()).getBitmap();
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/My Images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception ex) {
//ignore
}
}
I have an application, where you can take a picture about yourself (the app saves the image in a specified folder called "MyAppImage"), and I want to display the taken image in a second activity with a code, how to do this? I want to display it in a imageView in my SecondActivity, but I need a code that can get the last captured camera image from this folder, is there any way to do this?
Hope someone can guide me, how to do this, thanks!
After take photo:
MainActivity
String filePath;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
}
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
String fileName = "yourPhotoName" + ".jpg"
filePath = "pathOfMyAppImageFolder" + fileName;
File destination = new File(filePath);
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), AnotherActivity.class);
intent.putExtra("filePath", filePath)
startActivity(intent);
}
});
AnotherActivity
String filePath;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = this.getIntent();
filePath = intent.getStringExtra("filePath");
File imgFile = new File(filePath);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
}
Take the last photo of the folder:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<File> files = getListFiles(new File("MyAppImageFolderPath"));
File imgFile = files.get(files.size());
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
}
private List<File> getListFiles(File parentDir) {
ArrayList<File> inFiles = new ArrayList<File>();
File[] files = parentDir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
inFiles.addAll(getListFiles(file));
} else {
if(file.getName().endsWith(".jpg")){ //change to your image extension
inFiles.add(file);
}
}
}
return inFiles;
}
Once you takePhoto, save it into file.
Then call intent to start second activity and putExtraString with your image file path.
That is all.
I want to export an image to csv, but my application crashes whenever trying to do this. In my application I have a question "which brands do you recognize ?", and wanted to let the user answer by selecting a checkbox along with the corresponding ImageView. The objective is to save both question(a string) and answer(an image) to csv or doc by clicking on a button, but the application crashes and only the string is printed in the generated csv.
public class Page3 extends Activity implements OnClickListener {
Intent myIntent;
TextView question4;
CheckBox one, two, three;
ImageView one1, two2, three3;
Bitmap bmp1, bmp2, bmp3;
String ques4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page3);
initialize();
}
private void initialize() {
Button prev = (Button) findViewById(R.id.page3previous);
Button next = (Button) findViewById(R.id.page3next);
next.setOnClickListener(this);
prev.setOnClickListener(this);
question4 = (TextView) findViewById(R.id.question4tv);
one1 = (ImageView) findViewById(R.id.imageView1);
two2 = (ImageView) findViewById(R.id.imageView2);
three3 = (ImageView) findViewById(R.id.imageView3);
one = (CheckBox) findViewById(R.id.checkBox1);
two = (CheckBox) findViewById(R.id.checkBox2);
three = (CheckBox) findViewById(R.id.checkBox3);
}
#Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.page3previous:
myIntent = new Intent(Page3.this, Page2.class);
startActivity(myIntent);
break;
case R.id.page3next:
ques4 = question4.getText().toString();
if (one.isChecked()) {
one1.buildDrawingCache();
bmp1 = one1.getDrawingCache();
setBitMapOneTwoThree(bmp1, bmp3, bmp2, ques4);
} else if (two.isChecked()) {
two2.buildDrawingCache();
bmp2 = two.getDrawingCache();
setBitMapOneTwoThree(bmp1, bmp3, bmp2, ques4);
} else if (three.isChecked()) {
three3.buildDrawingCache();
bmp3 = three3.getDrawingCache();
setBitMapOneTwoThree(bmp1, bmp3, bmp2, ques4);
} else if (two.isChecked() && one.isChecked()) {
one1.buildDrawingCache();
bmp1 = one1.getDrawingCache();
two2.buildDrawingCache();
bmp2 = two.getDrawingCache();
setBitMapOneTwoThree(bmp1, bmp3, bmp2, ques4);
} else if (two.isChecked() && three.isChecked()) {
two2.buildDrawingCache();
bmp2 = two.getDrawingCache();
three3.buildDrawingCache();
bmp3 = three3.getDrawingCache();
setBitMapOneTwoThree(bmp1, bmp3, bmp2, ques4);
} else if (one.isChecked() && three.isChecked()) {
three3.buildDrawingCache();
bmp3 = three3.getDrawingCache();
one1.buildDrawingCache();
bmp1 = one1.getDrawingCache();
setBitMapOneTwoThree(bmp1, bmp3, bmp2, ques4);
} else if (one.isChecked() && three.isChecked() && two.isChecked()) {
three3.buildDrawingCache();
bmp3 = three3.getDrawingCache();
two2.buildDrawingCache();
bmp2 = two.getDrawingCache();
one1.buildDrawingCache();
bmp1 = one1.getDrawingCache();
setBitMapOneTwoThree(bmp1, bmp3, bmp2, ques4);
}
myIntent = new Intent(Page3.this, Page4.class);
startActivity(myIntent);
break;
}
}
private void setBitMapOneTwoThree(Bitmap bmp12, Bitmap bmp32, Bitmap bmp22,
String ques42) {
bmp12 = this.bmp1;
bmp32 = this.bmp3;
bmp22 = this.bmp2;
ques42 = this.ques4;
generateCsvFile("Image.csv", bmp12, bmp32, bmp22, ques42);
}
private void generateCsvFile(String string, Bitmap bmp12, Bitmap bmp32,
Bitmap bmp22, String ques42) {
try {
File root = Environment.getExternalStorageDirectory();
File gpxfile = new File(root, string);
FileWriter writer = new FileWriter(gpxfile);
FileOutputStream fOut = new FileOutputStream(gpxfile);
writer.append(ques42);
writer.flush();
writer.close();
bmp12.compress(Bitmap.CompressFormat.PNG, 85, fOut);
bmp32.compress(Bitmap.CompressFormat.PNG, 85, fOut);
bmp22.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Since csv is a text format, you have to encode each bitmap into its own base64 string before you can insert them into a csv file.
Here is an online converter for a quick example of what I'm talking about.
Here is a code example for encoding an image into a base64 string. And once you need to deserialize the images from the csv file, here is the code for decoding a base64 string back into its original binary format.
I am able to take 'screenshot of image' in my app but i need to take screenshot of 'particular part' in that image. Please anyone help me to do that... Thank you...
Here is the code I am using to take screenshot of an image....
L1 = (LinearLayout) findViewById(R.id.LinearLayout01);
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View v1 = L1.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
image = (ImageView) findViewById(R.id.ImageView01);
image.setBackgroundDrawable(bitmapDrawable);
}
});
`
after getting the bitmap do this way
Bitmap new_bmp = Bitmap.createBitmap(bm, 0, 0, linear.getWidth(), linear.getHeight(), matrix, false);
image.draw(new Canvas(new_bmp));
String s = Environment.getExternalStorageDirectory().toString();
linear.setBackgroundColor(Color.BLACK);
OutputStream outStream = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_hhmmss");
String file_name = sdf.format(new Date())+".PNG";
File file = new File(s, file_name);
try {
outStream = new FileOutputStream(file);
new_bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(CaptureImage.this, e.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(CaptureImage.this, e.toString(), Toast.LENGTH_LONG).show();
}