所以我試圖將我從圖庫中挑選的圖像上傳到我的imageview中。但圖像從來沒有出現在imageview?該程序不會崩潰或任何事情。我使用了toast消息來查看onActivityResult是否抓取了圖像,並且它確實如此。但它無法將其加載到圖像視圖中。如何將圖庫中的圖像上傳到圖像視圖中?
希望你們能幫忙,下面是我的代碼。
public class TakePhotoActivity extends AppCompatActivity implements View.OnClickListener {
private View snapButton;
private ImageButton galleryButton;
private int RESULT_LOAD_IMAGE = 1;
private String user_image_path;
private boolean pickedCamera = false;
private ImageView myImageView;
private ProgressBar myprogressBar;
//GPS
private LocationManager locationManager = null;
private LocationListener locationListener = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
setupCamera();
}
private void setupCamera()
{
snapButton = (View) findViewById(R.id.cameraActivity_CameraButton);
snapButton.setOnClickListener((View.OnClickListener) this);
snapButton.setOnClickListener(this);
galleryButton = (ImageButton) findViewById(R.id.cameraActivity_GalleryButton);
galleryButton.setOnClickListener(this);
myprogressBar = (ProgressBar) findViewById(R.id.myPB);
myprogressBar.setVisibility(View.INVISIBLE);
myImageView = (ImageView) findViewById(R.id.cameraActivity_ImageView);
}
private void grabImageFromGallery()
{
Intent imageGetter = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(imageGetter, RESULT_LOAD_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
{
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};//Array size of 1, and we put in a string
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
user_image_path = cursor.getString(columnIndex);//here we have our image path.
cursor.close();
}
Picasso.with(this).load(user_image_path).fit().centerCrop().into(myImageView);
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.cameraActivity_GalleryButton:
grabImageFromGallery();
break;
}
}
}
這是我的xml。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/toolbar_layout">
</include>
<ImageView
android:layout_width="200dp"
android:layout_height="150dp"
android:id="@+id/cameraActivity_ImageView"
android:layout_marginLeft="100dp"
android:layout_marginTop="50dp"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/circle_fb_2"
android:src="@drawable/icon_camera_red"
android:layout_marginTop="70dp"
android:layout_marginLeft="70dp"
android:id="@+id/cameraActivity_CameraButton"/>
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/circle_fb_2"
android:src="@drawable/icon_gallery"
android:layout_marginTop="70dp"
android:layout_marginLeft="150dp"
android:layout_alignLeft="@id/cameraActivity_CameraButton"
android:id="@+id/cameraActivity_GalleryButton"/>
<ProgressBar
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/myPB"
android:layout_below="@id/cameraActivity_CameraButton"
android:layout_marginLeft="170dp"/>
</RelativeLayout>
</LinearLayout>
您是否嘗試過在'getData'直接加載的URI?我想我使用了它,它工作正常。你還應該看看'getClipData',如果你做了多重選擇,那麼你可能會填充它。 – DariusL
可能的重複[如何從圖庫加載圖像視圖中的圖像?](http://stackoverflow.com/questions/13023788/how-to-load-an-image-in-image-view-from-gallery) –
看到我的回答波紋管 –