0

我到处搜索,但似乎没有找到解决方案。

在我的应用程序中,我有以下布局:

<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"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_marginTop="68dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="57dp"
        android:layout_marginTop="176dp"
        android:text="Load from Gallery" />

</RelativeLayout>

单击按钮时,我将使用来自 github 的 achooser 库项目显示文件选择。MainActivity 的代码是:

public class MainActivity extends SherlockActivity {

    private static final int REQUEST_CODE = 6384;
    Uri uri = null;
    String path = "";

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

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

            @Override
            public void onClick(View v) {
                showChooser();
                ImageView MyImageView = (ImageView) findViewById(R.id.imageView1);
                Drawable d = Drawable.createFromPath(path);
                MyImageView.setImageDrawable(d);
                MyImageView.refreshDrawableState();

            }
        });

    }

    private void showChooser() {
        // Use the GET_CONTENT intent from the utility class
        Intent target = FileUtils.createGetContentIntent();
        // Create the chooser Intent
        Intent intent = Intent.createChooser(target, "Choose Image");
        try {
            startActivityForResult(intent, REQUEST_CODE);
        } catch (ActivityNotFoundException e) {
            // The reason for the existence of aFileChooser
        }
    }

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

        switch (requestCode) {
        case REQUEST_CODE:
            // If the file selection was successful
            if (resultCode == RESULT_OK) {
                if (data != null) {
                    // Get the URI of the selected file
                    uri = data.getData();

                    try {
                        // Create a file instance from the URI
                        final File file = FileUtils.getFile(uri);
                        path = file.getAbsolutePath();
                    } catch (Exception e) {
                        Log.e("FileSelectorTestActivity", "File select error",
                                e);
                    }
                }
            }
            break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

选择图像时,没有任何反应。我的意思既不是任何异常,也不是图像加载。在 logcat 中,我看到:

在此处输入图像描述

4

1 回答 1

1

尝试这个:

public class MainActivity extends SherlockActivity {

    private static final int REQUEST_CODE = 6384;
    Uri uri = null;
    String path = "";

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

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

            @Override
            public void onClick(View v) {
                showChooser();
            }
        });

    }

    private void showChooser() {
        // Use the GET_CONTENT intent from the utility class
        Intent target = FileUtils.createGetContentIntent();
        // Create the chooser Intent
        Intent intent = Intent.createChooser(target, "Choose Image");
        try {
            startActivityForResult(intent, REQUEST_CODE);
        } catch (ActivityNotFoundException e) {
            // The reason for the existence of aFileChooser
        }
    }

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

        switch (requestCode) {
        case REQUEST_CODE:
            // If the file selection was successful
            if (resultCode == RESULT_OK) {
                if (data != null) {
                    // Get the URI of the selected file
                    uri = data.getData();

                    try {
                        // Create a file instance from the URI
                        final File file = FileUtils.getFile(uri);
                        path = file.getAbsolutePath();
                    } catch (Exception e) {
                        Log.e("FileSelectorTestActivity", "File select error",
                                e);
                    }

                ImageView MyImageView = (ImageView) findViewById(R.id.imageView1);
                Drawable d = Drawable.createFromPath(path);
                MyImageView.setImageDrawable(d);
                MyImageView.refreshDrawableState();

                }
            }
            break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

您可能在获取路径之前设置了图像的路径。

于 2013-08-20T16:15:00.700 回答