Question

I need to get information from a bitmap that the users clicks on in the Gallery, and returns to my page. I can return the image fine. I need to get: Size in bytes, width, height, and file path and return those to a text View.

Here is my code to invoke the gallery view and pick an image:

package com.example.name;

import statements here   
public class ImageCaptureActivity extends Activity  {

    //1st version
    private static final int CAMERA_RESULT = 5;
    private static final int GALLERY_RESULT = 6;
    private Bitmap imageResult;
    private ImageView idImageView1;
    private Bitmap selectedImage;
    private ImageView imgShowResult;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_capture);

        //get a reference to the image view that will display plant photo
        //idImageView1 = (ImageView) findViewById(R.id.idImageView1);

        //get access to gallery image picked- on this layout: idImageView1
        imgShowResult = (ImageView) findViewById(R.id.idImageView1);



    }

    private void getSystemService() {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.image_capture, menu);
        return true;
    }


    //Gallery view method

    public void onViewGalleryClicked(View v){
        //testbuttons-Toast.makeText(this,"you clicked the View Gallery button",Toast.LENGTH_LONG).show(); 

        //Implicit the line below says what we want to do, not where we want to go.
        //pick an image from a gallery, so specify an image pick        
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);

        //give me the path (file system directory) where we store images:
        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath();

        //convert to URI, as our gallery expects this, and convert to new local variable
        Uri picturesDirectory = Uri.parse(path);

        // set the data and the type on this intent,
        //so we tell it where to look for files and what files types we want
        photoPickerIntent.setDataAndType(picturesDirectory, "image/*");

        // start activity and tell it we want a result    
        //line below need to create a constant on the 6 ctrl-space I think, create constant
        startActivityForResult(photoPickerIntent, GALLERY_RESULT);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode,resultCode,data);


        if (resultCode == RESULT_OK ){

            //ONLY DO IF RESULT OK
            if (requestCode == CAMERA_RESULT){

                imageResult = (Bitmap) data.getExtras().get("data");

                imgShowResult.setImageBitmap(imageResult);

            }
            else if (requestCode == GALLERY_RESULT){
                //anything returned is returned through the get data object

                //find the path of the selected image
                Uri photoLocation = data.getData();
                Intent resultIntent = new Intent (this, ImageInformationActivity.class);
                resultIntent.putExtra("imagePath", photoLocation);
                startActivityForResult(resultIntent,10);

            }
        }

    }

}

Here is my code for the receiving page:

package com.example.namehere;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ImageInformationActivity extends Activity {
    //public static final String PLANT_RESULT = "PLANT_RESULT";



    private static final String IMAGE_FILENAME = null;
    private Bitmap selectedImage;
    private ImageView imgShowResult;
    private Uri photoLocation;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_information);
        //String searchTerm = getIntent().getStringExtra(ImageCaptureActivity.SEARCH_PLANT_NAME);
        imgShowResult = (ImageView) findViewById(R.id.imageView2);    


        try {
            photoLocation = (Uri)getIntent().getParcelableExtra("imagePath");

            //a stream of data from a tile
            InputStream openInputStream = getContentResolver().openInputStream(photoLocation);

            //take a stream of data and convert to bitmap
            selectedImage = BitmapFactory.decodeStream(openInputStream);

            //assign this image to our image view on this page
            imgShowResult.setImageBitmap(selectedImage);


        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

            //alert the user that something went wrong.
     Toast.makeText(this, getString(R.string.unable_to_open_image),  Toast.LENGTH_LONG).show();
        }


       // Bundle fileInfo = new Bundle();

      //  photoLocation = findViewById(R.id.idLocation);
        //plantPlacesColor.putExtras(fileInfo);
        //=(TextView) findViewById(R.id.idHeight);
        //=(TextView) findViewById(R.id.idWidth);
        //=(TextView) findViewbyId(R.id.idBytes);


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.image_information, menu);
        return true;
    }

}

and Here is my xml page

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ImageInformationActivity" >

    <TextView
        android:id="@+id/idBytes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/idHeight"
        android:layout_below="@+id/idHeight"
        android:layout_marginTop="31dp"
        android:text="@string/lblBytes"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/idHeight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/idWidth"
        android:layout_below="@+id/idWidth"
        android:layout_marginTop="22dp"
        android:text="@string/lblHeight"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/idLocation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="16dp"
        android:text="@string/lblLocation"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <RatingBar
        android:id="@+id/ratingBarImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView2"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="22dp" />

    <TextView
        android:id="@+id/idLike"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/ratingBarImage"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="22dp"
        android:text="@string/doLike"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/idBytes"
        android:layout_below="@+id/idBytes"
        android:layout_marginTop="27dp"
        android:src="@android:drawable/gallery_thumb" />

    <TextView
        android:id="@+id/idWidth"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/idLocation"
        android:layout_below="@+id/idLocation"
        android:layout_marginTop="15dp"
        android:text="@string/lblWidth"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

I know I should use Bitmap Factory, and I know I should use findViewById... etc. For example:

        //=(TextView) findViewById(R.id.idHeight);
        //=(TextView) findViewById(R.id.idWidth);
        //=(TextView) findViewbyId(R.id.idBytes);

But I get caught up in the details.

Was it helpful?

Solution

from a Bitmap you can get size in bytes, width, height but not the path of the file because the bitmap its not a file but an image loaded in the memory.

to get this infos do that:

int width = bitmap.getWidth();
int height = bitmap.getHeight();
int sizeBytes = getSizeFromBitmap(bitmap);

TextView textWidth = (TextView) findViewById(R.id.idWidth);
textWidth.setText(String.valueOf(width));
// ..continue

use this method to calculate the size:

public static int getSizeFromBitmap(Bitmap bitmap) {
    int pixels = bitmap.getHeight() * bitmap.getWidth();
    int bytesPerPixel = 0;
    switch (bitmap.getConfig()) {
        case ARGB_8888:
            bytesPerPixel = 4;
            break;
        case RGB_565:
            bytesPerPixel = 2;
            break;
        case ARGB_4444:
            bytesPerPixel = 2;
            break;
        case ALPHA_8:
            bytesPerPixel = 1;
            break;
    }
    return pixels * bytesPerPixel;
}

after that you can set the values to the right TextView. be careful: you can set an int inside the method setText of the TextView because it is accepting only ids of a string resource. you have to convert the number to a String before to set it. hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top