Thursday, 21 May 2015

How to create folder in android gallery

Here I am going to explain how to create a new folder in android gallery 
                                     

Step 1).Open res->layout->activity_main.xml



<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="com.example.create_folder.MainActivity" >

   <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android Dhina" />

   <ImageButton
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_centerInParent="true" />

</RelativeLayout>


Step 2).Activity Class

  
package com.example.create_folder;

import  java.io.File;

import  java.util.Date;

import android.provider.MediaStore;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import  android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity 

{
Button button;

ImageView image;

String imagepth;
Uri uri;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)

@SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


button= (Button) findViewById(R.id.button);
image = (ImageView) findViewById(R.id.image);
button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new 

                           Intent(  android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                                    

uri = setImageUri();

intent .putExtra(MediaStore.EXTRA_OUTPUTuri);


startActivityForResult(intent , 1);


}

});
}

public Uri setImageUri()  {


   File imageRoot = new File(Environment.getExternalStoragePublicDirectory
              (Environment.DIRECTORY_PICTURES),"Android Dhina");
                                                                              //folder name
 imageRoot .mkdirs();

 final File file = new File(imageRoot , "image" + new Date().getTime()+ ".png");


  Uri imgUri = Uri.fromFile(file );


   this.imagepth = file.getAbsolutePath();


return imgUri ;

   }

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


if (requestCode == 1 && resultCode == RESULT_OK) {


super.onActivityResult(requestCode resultCode data);


BitmapFactory.Options options = new BitmapFactory.Options ();


options .inSampleSize = 8;


final Bitmap bitmap = BitmapFactory.decodeFile(uri.getPath(), options);


button.setVisibility(View.INVISIBLE);
image.setImageBitmap(bitmap);

Intent ScanIntent = new Intent(

                                       Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

ScanIntent.setData(uri);
sendBroadcast(ScanIntent);
}
}
}


Step 3).Add uses-permission  to your Manifest file


<uses-permission android:name="android.permission.CAMERA" />

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


Happy Coding :)