我到处搜索但似乎找不到解决方案。
在我的应用中,我有以下布局:
<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中,我看到:
答案 0 :(得分: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);
}
}
您可能在获取路径之前设置了图像的路径。