I have and activity where on click of menu item save I want to save the image of the screen to my device inside a a specific folder. how can I do it. ?
The image which is displayed in the background is a ImageView and the text is textview. I have to merge them and save them as a single image.
Try with below code on menu item click event....
View v = view.getRootView();
v.setDrawingCacheEnabled(true);
Bitmap b = v.getDrawingCache();
// give path of external directory to save image
String extr = Environment.getExternalStorageDirectory().toString();
File myPath = new File(extr, "test.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
where view v is root layout...
Related
I'm trying to create a login form which loads the captcha from a specific url "http://evarsity.srmuniv.ac.in/srmsip/" into image view. But I'm unable to do so... What would be the best way to achieve this?
This is the code of the Java Class File below:
Document document = null;
try {
document = Jsoup.connect("http://evarsity.srmuniv.ac.in/srmsip/").ignoreContentType(true).get();
} catch (IOException e) {
e.printStackTrace();
}
Element captchaElement = document.select("#divmain img").first();
String captchaImgSrc = captchaElement.attr("src");
InputStream inputStream = null;
try {
inputStream = new URL("http://evarsity.srmuniv.ac.in/srmsip/" + captchaImgSrc).openStream();
} catch (IOException e) {
e.printStackTrace();
}
Bitmap captcha = BitmapFactory.decodeStream(inputStream);
ImageView captchaImage = (ImageView) findViewById(R.id.image);
captchaImage.setImageBitmap(captcha);
The image view either crashes showing nullPointer or with white colour background and in the last second line of the code the (ImageView) shows Casting is redundant.
I've got folder in assets called images1 with 114 images in it. I need to set them in listView with 2 textViews and 1 imageView. I haven't problems with textViews, but i don't know how to set images from assets to listView.
I tried:
int ids[] = new int[114];
for (int i = 0; i <ids.length; i++) {//<-------- taking ids of all pictures from images1 in assets-folder
try {
String[] images =getAssets().list("images1");
ArrayList<String> listImages = new ArrayList<String>(Arrays.asList(images));
int imgId = getResourceId(this, listImages.get(i),"images1", getPackageName());
ids[i] = imgId;
}
catch(IOException ex) {}
}
ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>(
questionTexts.length);//<--------filling listView's textViews and imageView
Map<String, Object> m;
for (int i = 0; i < questionTexts.length; i++) {
m = new HashMap<String, Object>();
m.put(ATTRIBUTE_QUESTION_TEXT, questionTexts[i]);//<-------- textView
m.put(ATTRIBUTE_ANSWER_TEXT, answerTexts[i]);//<-------- textView
m.put(ATTRIBUTE_NAME_IMAGE, ids[i]);//<-------- imageView
data.add(m);
String[] from = { ATTRIBUTE_QUESTION_TEXT, ATTRIBUTE_ANSWER_TEXT,
ATTRIBUTE_NAME_IMAGE };
int[] to = { R.id.listView_item_title, R.id.listView_item_short_description, R.id.listView_image };
SimpleAdapter sAdapter = new SimpleAdapter(this, data, R.layout.item,
from, to);
lvSimple = (ListView) findViewById(R.id.lvSimple);
lvSimple.setAdapter(sAdapter);
}
public static int getResourceId(Context context,String variableName, String resourceName,
String packageName) throws RuntimeException{//<----- this method helps me to get IDs of images from assets/images1
try{
return context.getResources().getIdentifier(variableName,resourceName,packageName);
}catch (Exception e){
throw new RuntimeException("Error getting resource id");
}
}
But finally i've got white fields instead my pictures.
I know how to solve this problem when your pictures are in R.drawable, but how to do it when they are in assets subfolder?
You can use this.
try
{
// get input stream
InputStream ims = getAssets().open("avatar.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
mImage.setImageDrawable(d);
ims .close();
}
catch(IOException ex)
{
return;
}
Are you sure that this images should be inside assets/ folder? Why not in res/drawables/?
Of course you can load it from assets ;)
To get the list of all files inside asset folder use below code:
list = getAssets().list("images1")
To load the image from asset you can use below code:
fun setImageFromAsset(ImageView view, String filename) {
try {
InputStream is = getAssets().open(filename);
Drawable drawable = Drawable.createFromStream(is, null);
view.setImageDrawable(drawable);
}
catch(IOException ex) {
return;
}
}
You can get a bitmap by using below code
private Bitmap getBitmapFromAssets(String fileName){
AssetManager am = getAssets();
InputStream is = null;
try{
is = am.open(fileName);
}catch(IOException e){
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(is);
return bitmap;
}
You can show bitmap in imageview by using "Glide" Here is sample code
Glide.with(context)
.load(Uri.parse("file:///android_asset/fileName"))
.into(imageView);
Simple Adapter can work only with IDs in drawable-folder or with Uri. But you can use ViewBinder to set image using setImageBitmap method.
I have a Adobe Air application that intend to take a screenshot with Native Extension on Android device, but the java code returns a black image.
public FREObject call(FREContext context, FREObject[] params) {
View view = context.getActivity().getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap image = view.getDrawingCache();
}
I don't know much about Adobe Air. My java code runs exactly on Android Java Application, but returns black image on Adobe Air Android Application with Native Extension.
Is there any solution or any way to take a screenshot using Java in NativeExtension?
Thanks much!
Could be that you are not getting the correct view. Try this to get the topmost root view.
public FREObject call(FREContext context, FREObject[] params)
{
View view = findViewById(android.R.id.content).getRootView();
view.setDrawingCacheEnabled(true);
Bitmap image = view.getDrawingCache();
if(image == null)
{
System.out.println("Image returned was null!");
}
}
I also removed the buildDrawingCache() line; that can sometimes cause issues, and from what I've read it's not completely necessary.
Finally you'll want to check if the bitmap being returned is null. If so that could be why it's all black.
You can take a screenshot like this and save it to the SD card:
View content = findViewById(R.id.layoutroot);
content.setDrawingCacheEnabled(true);
Function to get the rendered view:
private void getScreen()
{
View content = findViewById(R.id.layoutroot);
Bitmap bitmap = content.getDrawingCache();
File file = new File( Environment.getExternalStorageDirectory() + "/asdf.png");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
You have to add this permission to your AndroidManifest (if you want to save it):
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I'm trying a way to save a picture when I'll click in button save using upload component of vaadin7.
The upload component has one button to send image but I wanna that save when I click button save of my Window with name that I define.
I'm trying this.
//upload image
Upload upload = new Upload("Choose your picture");
upload.setButtonCaption(null);
mainLayout.addComponent(upload);
Button btnSave = new Button("Save");
btnSave.addClickListener(new Button.ClickListener() {
#Override
public void buttonClick(ClickEvent event) {
//click to save all fields and picture choosed in upload
}
});
/** upload image picture */
public class ImageUpload implements Receiver{
private File file;
private String cpf; // image's name example 222.333.444-55
/** save image picture */
#Override
public OutputStream receiveUpload(String filename, String mimeType) {
FileOutputStream fos = null;
try{
if(new File(filename).getName().endsWith("jpg")){
String cpfFormato = this.cpf.replaceAll("\\.", "").replace("-", "");
String[] imagem = filename.split("\\.");
String novaImagem = cpfFormato + ".jpg"; //22233344455.jpg
file = new File(novaImagem);
fos = new FileOutputStream("/tmp/" + file);
}else{
new Notification("Erro de arquivo \n",
"Only jpg",
Notification.Type.ERROR_MESSAGE)
.show(Page.getCurrent());
}
}catch(FileNotFoundException ex){
new Notification("File not found \n",
ex.getLocalizedMessage(),
Notification.Type.ERROR_MESSAGE)
.show(Page.getCurrent());
return null;
}
return fos;
}
}
Any idea ?
Use the upload.submitUpload(); method.
Hint: As you can see in the API description you can hide the upload internal submit button by setting upload.setButtonCaption(null);
Link to the API: https://vaadin.com/api/com/vaadin/ui/Upload.html#submitUpload()
I have a business need to create the NinePatchDrawable objects at runtime, this is, an exterior .png image is received from a server and it has to be applied in a button's background (for example) as a nine patch.
I have tried to create the NinePatchDrawable object, but the constructor asks me for a "byte[] chunck" that describes the patch. The thing is, I have no idea on how to build this chunk from a bitmap that does not have the 9patch information in it.
Any ideas on this topic? Am I seeing the problem from a wrong perspective?
See my answer for Create a NinePatch/NinePatchDrawable in runtime
I develop a tool to create NinePatchDrawable from (uncompiled) NinePatch bitmap.
See https://gist.github.com/knight9999/86bec38071a9e0a781ee .
The method
NinePatchDrawable createNinePatchDrawable(Resources res, Bitmap bitmap)
helps you.
For example,
ImageView imageView = (ImageView) findViewById(R.id.imageview);
Bitmap bitmap = loadBitmapAsset("my_nine_patch_image.9.png", this);
NinePatchDrawable drawable = NinePatchBitmapFactory.createNinePatchDrawable(getResources(), bitmap);
imageView.setBackground( drawable );
where
public static final Bitmap loadBitmapAsset(String fileName,Context context) {
final AssetManager assetManager = context.getAssets();
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(assetManager.open(fileName));
return BitmapFactory.decodeStream(bis);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bis.close();
} catch (Exception e) {
}
}
return null;
}
In this case, the my_nine_patch_image.9.png is under the assets directory.
I've answered this over at question 5519768. Keep in mind the differences between "source" and "compiled" ninepatch images.