单击事件打开FileDialog

时间:2012-08-31 13:09:38

标签: android

我是Android的初学者我正在尝试开发Set Wallpaper App。点击图片后,第二个活动将会打开。这是我的代码:

的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);
            }
        });
        }

}

点击设置壁纸按钮后,文件选择器应该打开,我可以从中选择图像,我也可以看到该图像的预览,并可以将其设置为壁纸。请帮助我。 我还想在点击“查看更多”按钮后打开一个弹出窗口。

1 个答案:

答案 0 :(得分:3)

showFileChooser()方法将为您完成此操作。 希望你得到你的要求。

 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;
        }