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 に答える