Rash
Rash

Reputation: 896

Open FileDialog on click event

I am a beginner in Android I am trying to develop Set Wallpaper App.After clicking on image second activity will open.Here is my code:

manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapplication"
    android:versionCode="1"
    android:versionName="1.0" >
     <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="15" />

   <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".secondActivity"
                  android:label="@string/app_name">
        </activity>
    </application>

</manifest>

activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="wrap_content"
     android:layout_height="fill_parent"
     android:orientation="vertical"
     android:paddingLeft="8dp"
     android:paddingRight="8dp" >

     <ImageView
         android:id="@+id/imageView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@android:drawable/btn_star_big_on"
         android:clickable="true"  
         android:onClick="imageClick"  />

     <TextView
         android:id="@android:id/empty"
         android:layout_width="150dp"
         android:layout_height="wrap_content"
         android:text="Wallpaper" />
 </LinearLayout>

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="wrap_content"
     android:layout_height="fill_parent"
     android:orientation="vertical"
     android:paddingLeft="8dp"
     android:paddingRight="8dp" >

    <Button
        android:id="@+id/set"
        android:layout_width="298dp"
        android:layout_height="70dp"
        android:text="Set Wallpaper" />


    <Button
        android:id="@+id/hoti"
        android:layout_width="fill_parent"
        android:layout_height="193dp"
        android:text="HOTI" />

    <Button
        android:id="@+id/smb"
        android:layout_width="298dp"
        android:layout_height="70dp"
        android:text="See More" />
    <Button
        android:id="@+id/quit"
        android:layout_width="298dp"
        android:layout_height="70dp"
        android:text="Quit" />
 </LinearLayout>

MainActivity.java

package com.example.myfirstapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

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


public void imageClick(View v){

    //Create an intent to start the new activity.
       // Our intention is to start secondActivity
    Intent intent = new Intent();
    intent.setClass(this,secondActivity.class);
    startActivity(intent);
   }
}

secondActivity.java

package com.example.myfirstapplication;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class secondActivity extends Activity {
    Bitmap bitmap;
   /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

     // Set the layout for this activity
        setContentView(R.layout.main);


        Button btn1 = (Button) findViewById(R.id.quit);
        btn1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
                System.exit(0);
            }
        });
        }

}

After clicking on set wallpaper button the file chooser should open from which I can choose image and also I can see preview of that image and can set it as a wallpaper.Please help me out. I also want to open a popup window after clicking on "see more" button.

Upvotes: 0

Views: 1311

Answers (1)

NaserShaikh
NaserShaikh

Reputation: 1576

showFileChooser() method will do this for you. Hope u get ur requirment.

 private void showFileChooser() {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
            intent.setType("*/*"); 
            intent.addCategory(Intent.CATEGORY_OPENABLE);

            try {
                startActivityForResult(
                        Intent.createChooser(intent, "Select a Image"), FILE_SELECT_CODE);
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(this, "Please install a File Manager.",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            switch (requestCode) {
                case FILE_SELECT_CODE:      
                if (resultCode == RESULT_OK) {  

                    Uri uri = data.getData();
                    String path = getRealPathFromURI(uri);
                    File imgFile = new File(path);
                    if(imgFile.exists())
                    {
                       myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                       img.setImageBitmap(myBitmap);
                    }

                }           
                break;
            }
        }

        /**
         * Returns path of the file specified by content Uri
         */
        public String getRealPathFromURI(Uri contentUri) {
            String[] proj = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            String path = cursor.getString(column_index);
            cursor.close();
            return path;
        }

Upvotes: 3

Related Questions