send image onclick to zoom activity - android

I'm trying to send an image to zoom activity from my main_activity.
I have a onclick function:
case R.id.imageViewHero:
String image = ViewHolder.this.post.getImageUrl();
Intent intentv = new Intent(context, Zoom.class);
Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", image);
intentv.putExtras(extras);
context.startActivity(intentv);
break;
the problem is the string image, I don't know what to do next to send it to zoom. Any ideas?
my zoom.class if needed:
public class Zoom extends Activity {
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zoom);
Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");
ImageView imgDisplay;
Button btnClose;
imgDisplay = (ImageView) findViewById(R.id.imgDisplay);
btnClose = (Button) findViewById(R.id.btnClose);
btnClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Zoom.this.finish();
}
});
imgDisplay.setImageBitmap(bmp );
}
}

The code is correct for most part except one mistake in making the Bitmap from the image URL.
You cannot make a Bitmap image from an image URL like you have done.
Its already been answered on StackOverflow.
Check this link on how to convert an Image from an Image URL to a Bitmap - How to get bitmap from a url in android?
Basically, you are getting an Image URL from Intent which you are passing from the previous Activity, in the Zoom Activity, save it to a String variable, make Bitmap from it, set the Bitmap to the ImageView.

In onClick method, you seem to send imageUrl. However in Zoom.java you're treating it as Bitmap.
You can use a library like Picasso to populate ImageView using Image URL

Related

Android : Clickable link of sdcard storage

I want to display links which have format such as/sdcard/Pictures/ etc in textview as clickable link with highlight and underline. I often use Linkifyfor display links, however, this does not support storage links (only web, email etc).
Is there any possible way for that?
I would recommend displaying the image in an ImageView in an Activity.
You can put logic in your onClick(View v) to open the contents of the TextView in a new activity:
#Override
public void onClick(View v) {
Intent intent = new Intent(context, ShowImageActivity.class);
intent.putExtra("imageLocation", v.getText());
context.startActivity(intent);
}
Then in your ShowImageActivity's onCreate() :
ImageView jpgView = (ImageView)findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/sample-1.jpg");
jpgView.setImageBitmap(bitmap);
setContentView(R.layout.main);
https://stackoverflow.com/a/9509948/5486718
If you need to you can alternatively add a custom intent to the linkify:
String newActivityURL = "content://com.example.yourapp.yourtargetactivity/";
Pattern pattern = Pattern.compile("[/sdcard/]+[[a-f][0-9]]+[.jpg]");
Linkify.addLinks(hashView, pattern, newActivityURL);

Can't get Image from drawable by code

I have an Imageview that when user clicks on it a dialog box opens and it is suppose to show image larger within it.
the Imageview I have is in a layout and the code is this:
ImageView image_terrain = (ImageView)findViewById(R.id.imageView2);
image_terrain.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Universitymap.class);
intent.putExtra("imageName", "sattelite");
Dialog d = new Dialog(Universitymap.this);
d.setContentView(R.layout.image_dialog);
d.show();
}
});
I use intent for sending which picture clicked by sending it's name.
now in image_dialog layout I have this code:
Intent intent = new Intent();
String fileName = intent.getExtras().getString("imageName");
loadImage(fileName);
and the function that loads image in image_dialog layout class is:
private void loadImage(String fileName){
ImageView img = (ImageView)findViewById(R.id.img_Picture);
int resID = getResources().getIdentifier(fileName, "drawable", "com.neema.smobile.Main");
img.setImageResource(resID);
}
one word = it doesn't work. I would be happy if anyone help.
Try this
int imageResource = getResources().getIdentifier(fileName, null, getPackageName());
imageview = (ImageView)findViewById(R.id.img_Picture);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);
Why are you passing the filename via an Intent? If you are creating the Dialog in the same activity you could create a custom Dialog with a custom Constructor and overgive the filename.
Also i think this line doesn't make sense:
Intent intent = new Intent();
String fileName = intent.getExtras().getString("imageName");
Since you are trying to catch the String from a new Intent, instead of the Intent you sent.
Or did I missunderstood youre way?
The Intent is for starting Activities not for Dialogs :)
Edit:
class CustomDialog extends Dialog{
private String fileName;
public CustomDialog(String fileName){
this.fileName = fileName;
this.setContentView(R.layout.your_dialog_layout);
}
#Override
public void onCreate(Bundle savedInstanceState){
ImageView image = (ImageView) findById(R.id.image);
image.setBackgroundResource(....//set your Image like mentioned from kumaand using your saved fileName
}
}
Should work. For more Information you could look at http://about-android.blogspot.de/2010/02/create-custom-dialog.html

How to load image from one xml file into another xml file?

I'm creating an camera app here. I've managed to click an image and display in an ImageView. I have a ImageButton on whose click takes you to another page. On the other page I want to display that image with a set of scrollable images like on Instagram. But I cant load that image on the other xml file.
Help Please.
Code:
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_select);
// Image clicked...
ImageView ivPic = (ImageView) findViewById (R.id.ivPic);
// Image button to go to next page...
ImageButton ib = (ImageButton) findViewById(R.id.ibNext);
// Not sure if this is right.....
Drawable iv = ivPic.getDrawable();
ib.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.rating.bar.EFFECTS"));
}
});
}}
In the other class I created this classes variable but I cant use iv(which holds the drawable of the image view). It only accepts integers I think. What should I do?
Read the URI/Path of the image in your imagebutton and add to the extras of the intent calling the next activity and read the extras in the next activity.

How to add multiple custom views on an activity on a button click?

I am making an app in which an image is loaded from the gallery, now the user can add re-sizable and dragable rectangles on the image on the click of a button.
Right now i am able to add the image and also add a required rectangle on the image on the click of a button.
But what i want is that if the button is clicked again, a new rectangle is added, and the previous is restored as it is.
My code is as below..
public class ImageActivity extends Activity {
FrameLayout fm; //framelayout to add views on the image
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
Bundle bundle = getIntent().getExtras(); //5 lines
final String value = bundle.getString("key"); //of code
Bitmap bmp = BitmapFactory.decodeFile(value); //to add
ImageView imageView = (ImageView) findViewById(R.id.imgView); //the image
imageView.setImageBitmap(bmp); //from gallery
Button bd = (Button)findViewById(R.id.buttonDraw); //button to add rectangle
fm = (FrameLayout)findViewById(R.id.main_view); //framelayout object
bd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
DrawPanel dview=new DrawPanel(ImageActivity.this);
fm.addView(dview);
}});
}
}
I am creating a new object of mycustom view for the rectangle each time the button is clicked. The problem here is that when the button is clicked for the first time, the rectangle is drawn with each of its functionalities.
But when i click the button again the previous one vanishes and no new rectangle is drawn.
There is no message in the console or in the log cat. What am i doing wrong...

Clear Bitmap data and make it null

I am working in an android application and I want to Clear my Bitmap data. The scenario is that I am taking a screen capture of an Widget(Imageview) and I am storing it in a Bitmap. This action comes in a Button click. SO after some time I get a Memory error. So I want to clear the values in the bitmap. So to do that I have done the following code :
The BitMap variable is mCaptureImageBitmap
public void ButtonClick(View v)
{
mCaptureImageBitmap.recycle();
mCaptureImageBitmap=null;
View ve = findViewById(R.id.mainscreenGlViewRelativeLayout);
ve.setDrawingCacheEnabled(true);
mCaptureImageBitmap = ve.getDrawingCache();
}
But I get an error of NullPoint exception. Please help me
You have most of the right code but in the wrong order. Try doing something like this
public void ButtonClick(View v)
{
Bitmap mCaptureImageBitmap;
final View ve = findViewById(R.id.mainscreenGlViewRelativeLayout);
ve.setDrawingCacheEnabled(true);
mCaptureImageBitmap = ve.getDrawingCache();
// Do something useful with your image here
mCaptureImageBitmap.recycle();
mCaptureImageBitmap = null;
}
Try this code ...
ve.setDrawingCacheEnabled(true);
// Add these lines
ve.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
ve.layout(0, 0, ve.getMeasuredWidth(), ve.getMeasuredHeight());
ve.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(ve.getDrawingCache());
mCaptureImageBitmap = b;
ve.setDrawingCacheEnabled(false); // clear drawing cache

Categories

Resources