I followed another StackOverflow tutorial where you can capture a bitmap from the camera and set it to your image view. The issue I am having is that the picture that is returned is very small and does not fill the rest of the screen. In my XML I do the following...
<ImageView
android:id="#+id/imgview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
If I change to "fill_parent" I get a larger image, but my question now is how would I go about getting the actual size image and putting it on a scrollable (in both the x and y directions) view container.
Thanks!
BTW, here is the code for capturing the Bitmap from the camera...
public class MainActivity extends Activity implements OnClickListener
{
private ImageView imageView;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("matt","1");
Button b = (Button)this.findViewById(R.id.btn);
imageView = (ImageView)this.findViewById(R.id.imgview);
b.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onClick(View v)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,1888);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == 1888)
{
Log.d("matt", "3.a");
Bitmap photo = (Bitmap) data.getExtras().get("data");
Log.d("matt", "3.b");
imageView.setImageBitmap(photo);
Log.d("matt", "3.c");
}
}
If you need the dimensions of the image being captured then you can use:
int height = photo.getHeight();
int width = photo.getWidth();
Otherwise, if you would like to find out the size of the image captured (in megabytes) then please refer to my answer to this question: https://stackoverflow.com/a/11846455/1512836
[EDIT] - Please check this for scrolling an image view: Images in ScrollView in android
The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.
Related
I am new Android programming.
I am trying to open an image from the Gallery of a phone and draw a Rectangle on top of the image I have opened. But I am able to open the image but I am not able to see the rectangle. I am using ImageView and Canvas to open image and draw. I have created a Button and used to image from Gallery. I have written the code to open and draw in onActiviesult() method.Could somebody help me?!. Thanks in advance
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button gal=(Button)findViewById(R.id.button1);
gal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent gal_open=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(gal_open,1);
}
});
}
public void onActivityResult(int requestCode,int resultCode,Intent intentData){
super.onActivityResult(requestCode, resultCode, intentData);
if(requestCode==1 && resultCode==RESULT_OK && intentData!=null){
ImageView img=(ImageView)findViewById(R.id.imageView1 );
Bitmap bmp=Bitmap.createBitmap(img.getHeight(),img.getWidth(),Bitmap.Config.RGB_565);
Canvas cnvs=new Canvas(bmp);
//img.setImageBitmap(bmp);
Paint paint=new Paint();
paint.setColor(Color.RED);
Uri data=intentData.getData();
String[] filePath={MediaStore.Images.Media.DATA};
Cursor cur=getContentResolver().query(data,filePath,null,null,null );
cur.moveToFirst();
int colIndex=cur.getColumnIndex(filePath[0]);
String picPath=cur.getString(colIndex);
cur.close();
cnvs.drawRect(20, 20,50,50 , paint);
img.setImageBitmap(bmp);
img.setImageBitmap(BitmapFactory.decodeFile(picPath));
}
}
return super.onOptionsItemSelected(item);
}
}
try drawing the bitmap on the canvas and then call setImageBitmap just once. In this moment you are overriding the content of the ImageView. Remove img.setImageBitmap(BitmapFactory.decodeFile(picPath));
and
cnvs.drawBitmap(BitmapFactory.decodeFile(picPath), 0, 0, null);
cnvs.drawRect(20, 20,50,50 , paint);
img.setImageBitmap(bmp);
I am not sure abaut the drawing order. If you don't see the rectangle try changing the order of drawRect and drawBitmap
You may try like this way which i use:
create customborder.xml file.
Just take imageview in layout and make border xml file
and now use this customborder in layout backgourd like:
android:layout_height="fill_parent"
android:background="#drawable/customborder">
i have Gridview that display Image from internet using universal-image-loader
i want when i click on image in gridview
show up in full screen
here is my simple code :
MainActivity
Photoist.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int pos, long id) {
// String url = mAdapter.getItem(pos).getImgUrl();
GridView lv = (GridView) parent;
ImageView SelectedImage = (ImageView) parent.getChildAt(pos-lv.getFirstVisiblePosition()).findViewById(R.id.iconImg);
SelectedImage.buildDrawingCache();
Bitmap myimage = SelectedImage.getDrawingCache();
Intent intent = new Intent(getActivity(),FullScreenImageView.class);
intent.putExtra("MyImageIntent", myimage);
startActivity(intent);
}
});
FullScreenImageView.Class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ImageView Fullscreen = (ImageView) findViewById(R.id.imgDisplay);
Bitmap Image = (Bitmap) getIntent().getExtras().get("MyImageIntent");
Fullscreen.setImageBitmap(Image);
}
The Error in this Code :
Fullscreen.setImageBitmap(Image);
i hope someone can help
and i have 1 more question
how i can make the FullScreenImage Swipe/Slide . i mean if i slide image left it show next image, and i slide it right show previews image .
-sorry for my bad english, and thanks
Edit:
Try this when getting the Bitmap from your ImageView:
Bitmap image = ((BitmapDrawable)SelectedImage.getDrawable()).getBitmap();
Bitmap class implements parcelable, which means that it's instances can be stored in a parcel.
Parcel by the way is just androids way of serialization.
Change
Bitmap image = (Bitmap) getIntent().getExtras().get("MyImageIntent");
to
Bitmap image = (Bitmap) getIntent().getParcelableExtra("MyImageIntent");
For the slide part you want to create a ViewPager which was designed for exactly that.
I'm trying to load in part of a tall image into a scrollable imageview (imageview inside a scrollview).
It works with smaller images but if the images is bigger than 2048x2048 the app will crash due to open gl out of memory.
I can load part of the image with this:
private Bitmap decodeBitmapRegion(InputStream in, Rect region) {
BitmapRegionDecoder decoder = null;
try {
decoder = BitmapRegionDecoder.newInstance(in, false);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
region.right = decoder.getWidth();
Bitmap reg = decoder.decodeRegion(region, null);
return reg;
}
Where the rectangle can be something like (0,0,DisplayWidth,800).
That works in most cases since i'm using the imageview to let the user crop a part of the image and save it as a coveriamge.The imageview has the same width as the display and the height of 400 dp.
So the image the user saves will be the same width as the orginal but only 400 dp's tall.
Is there anyway to make it possible for the user to scroll all the way to the bottom of the orginal image?
How does webview load images that are bigger than 2048x2048?
Here's one thought.
Imagine your image broken into rows of bands. Use a ListView to display the bands. A ListView uses an adapter. In adapter.getView(), you use the decoder to load the appropriate band.
The ListView already does "recycling". So, it more-or-less only holds enough rows to fill the screen.
This technique should work well if your images aren't too wide. If they are, and it's not tall enough to occupy more than one screen height, you'll still have the same memory problems. A more complicated solution would be to use some sort of grid view, but then you'll have to do your own view recycling. Not too difficult, but harder than using ListView.
I managed to get the functionality I wanted. But i don't think this is the best solution?
I loaded the image into a webview and then i took a screenshot of the visible part of the webview. After that i can save that bitmap and do whatever i want to it.
But there must be a better way to do this than to use a webview to show the original image?
Anyway here is what I did:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
imgView = (ImageView) findViewById(R.id.imageview);
webView = (WebView) findViewById(R.id.webview);
webView.loadUrl("file:///android_asset/test.jpg");
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
imgView.setImageBitmap(getBitmapForVisibleRegion(webView));
}
});
}
public static Bitmap getBitmapForVisibleRegion(WebView webview) {
Bitmap returnedBitmap = null;
webview.setDrawingCacheEnabled(true);
returnedBitmap = Bitmap.createBitmap(webview.getDrawingCache());
webview.setDrawingCacheEnabled(false);
return returnedBitmap;
}
I'm adding images to a gridview, everything works perfectly until I turn the phone sideways, all the images in the GridView disapper and this error appears on the log
SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
The images I'm using for my GridView are taken with the phone camera like this
Intent camera_intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera_intent, CAMERA_PIC_REQUEST);
After that I call the image Adapter
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case 0:
if(resultCode==RESULT_OK){
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
Bitmap.createScaledBitmap(thumbnail, 100, 100, true);
Drawable drawable = new BitmapDrawable(getResources(), thumbnail);
list.add(drawable);
GridView gv = (GridView)findViewById(R.id.GridV1);
gv.setAdapter(new ImageAdapter(this, list));
}
}
}
And finally this is where I add my images in the ImageAdapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(130, 130));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(10, 10, 10, 10);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageDrawable(mThumbIds.get(position));
return imageView;
}
I think it has something to do with the scale, but I'm not sure, can't find a solution to this problem.
Like Sochimickox said, your layout gets redrawn when you turn it. So there is two way that i see, first is disabling the redrawn. Since we know your error's cause is "spans", one of these should help you:
First one
Or this one
Probably the first would do it, but i recommend designing the app for both landscape and portait modes. To do that, you should crete one more folder in your resource named layout-port. Than you can simply copy the xml file here, select the portait look from top bar at "Graphical Layout" bar. I say read here first. It's from Android Developer site, just incase, I'm putting another tutorial link too.
Just to be clear, layout-port is for portait mode. If you want to design for lanscape, use layout-land. You can design for different screen size with this way too. Like layout-sw600dp
The images dissapear because when the configuration changes, the layout gets redrawn.
You can set landscape mode by default on your app by adding this to OnCreate:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Or set it to portrait mode:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
EDIT
Try this:
#Override
public void onConfigurationChanged(Configuration newConfig) {
newConfig.orientation = getResources().getConfiguration().orientation;
super.onConfigurationChanged(newConfig);
}
I have the requirment in one my activity imageview and button if user click the take picture button.I have to show preview another activity from preview activity user click the button and take the picture and display in the imageview in previous activity I have to open front face camera for capturing user image eg if i am using tablet I have to take my picture can anybody tell how to do? can anybody provide sample code
Thanks
You can start camera and take picture using this code
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 999);
//this method will be called once user takes the pictur
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 999)
{
if(data.getExtras() != null)
{
//here is the image from camera
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
}
}
}
That is how you can create an image view and display Bitmap in that ImageView
ImageView imageView = new ImageView(context);
imageView.setImageBitmap(bitmap);