Showing posts with label Images. Show all posts
Showing posts with label Images. Show all posts

ANDROID:BITMAP IMAGE STORE IN EXTERNAL OR INTERNAL STORAGE?

Write this line in onCreate() method, This line of code is used for checking Sd card  is available or not.


  1. Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals
                             (android.os.Environment.MEDIA_MOUNTED);

And this method is used for save your bitmap file in external and internal storage.

  1. private String SaveImage_Sta(Bitmap finalBitmap, String name) {
    if(isSDPresent) {
    Log.i("isSDPresent yes", " path is==> " +isSDPresent );
    String root = Environment.getExternalStorageDirectory().toString() + "/profile";
    File myDir = new File(root);
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = name + ".jpg";
    file = new File(myDir, fname);
    if (file.exists())
    file.delete();
    try {
    FileOutputStream out = new FileOutputStream(file);
    finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
    out.flush();
    out.close();

    } catch (Exception e) {
    e.printStackTrace();
    }
        } else {
        
         Log.i("isSDPresent no ", " path is==> false ");
         ContextWrapper cw = new ContextWrapper(getApplicationContext());
             // path to /data/data/yourapp/app_data/imageDir
            File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
            // Create imageDir
            file =new File(directory,"profile.jpg");       
          
            if (file.exists())
    file.delete();
            try {        
             FileOutputStream fos = new FileOutputStream(file);
             finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
             fos.flush();
                fos.close();
            
            } catch (Exception e) {
                e.printStackTrace();
            }       
        }
    return file.toString();


Android Select image from gallery and display in imageview?

GallerysampleActivity.java  
 

  public class GallerysampleActivity extends Activity {
private static Bitmap Image = null;
private static Bitmap rotateImage = null;
private ImageView imageView;
private static final int GALLERY = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

imageView = (ImageView) findViewById(R.id.imageView1);

Button gallery = (Button) findViewById(R.id.button1);
gallery.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

imageView.setImageBitmap(null);
if (Image != null)Image.recycle();
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), GALLERY);
}
});
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GALLERY && resultCode != 0) {
Uri mImageUri = data.getData();
try {

Image = Media.getBitmap(this.getContentResolver(), mImageUri);
if (getOrientation(getApplicationContext(), mImageUri) != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(getOrientation(getApplicationContext(), mImageUri));
if (rotateImage != null)
rotateImage.recycle();
rotateImage = Bitmap.createBitmap(Image, 0, 0, Image.getWidth(), Image.getHeight(), matrix,
true);
imageView.setImageBitmap(rotateImage);
} else
imageView.setImageBitmap(Image);
} catch (FileNotFoundException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
}
}

public static int getOrientation(Context context, Uri photoUri) {
/* it's on the external media. */
Cursor cursor = context.getContentResolver().query(photoUri,
new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

if (cursor.getCount() != 1) {
return -1;
}

cursor.moveToFirst();
return cursor.getInt(0);
}
}

main.xml 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="100dp"
        android:layout_marginTop="20dp"
        android:text="Go to gallery" />


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Output Images






Select DateRange UsingRangePicker.

  /* * This Method is for select range from picker. * */ private fun selectDateRangeUsingRangePicker () { pageNumber = 1 val displ...