1

所以我試圖將我從圖庫中挑選的圖像上傳到我的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> 
+0

您是否嘗試過在'getData'直接加載的URI?我想我使用了它,它工作正常。你還應該看看'getClipData',如果你做了多重選擇,那麼你可能會填充它。 – DariusL

+0

可能的重複[如何從圖庫加載圖像視圖中的圖像?](http://stackoverflow.com/questions/13023788/how-to-load-an-image-in-image-view-from-gallery) –

+0

看到我的回答波紋管 –

回答

2

在加載本地圖像您有路徑應該是這樣的「文件:///image_path.jpg 」。

更換

Picasso.with(this).load(user_image_path).fit().centerCrop().into(myImageView); 

Picasso.with(this).load("file:///"+user_image_path).fit().centerCrop().into(myImageView); 
+0

所以我沒有用你的線代替我的線,它沒有工作。但是,如果我使用Picasso.with(TakePhotoActivity.this).load(selectedImage).fit()。centerCrop()。into(myImageView); 然後它確實有效。那麼,畢加索能夠使用字符串imagePath或文件來處理嗎? – TheQ

+0

它會接受這兩個看到更多細節http://square.github.io/picasso/實際上畢加索做Picasso.with(上下文).load(R.drawable.landing_screen).into(imageView1); (上下文).load(「file:///android_asset/DvpvklR.png」).into(imageView2); Picasso.with(context).load(new File(...))。into(imageView3); –

1

試試這個方式來加載在imageview的

public class LoadImage extends Activity 
{ 
    Activity activity=null; 
    Context context=null; 

    Button header_left_btn=null; 
    Button header_right_btn=null; 
    TextView header_text=null; 
    TableLayout image_table=null; 

    ArrayList<String> image_list=new ArrayList<String>(); 
    ArrayList<Drawable> image_drawable=new ArrayList<Drawable>(); 
    String path=""; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
     setContentView(R.layout.main); 
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.header); 

     activity=LoadImage.this; 
     context=LoadImage.this; 

     header_left_btn=(Button)findViewById(R.id.header_left_btn); 
     header_right_btn=(Button)findViewById(R.id.header_right_btn); 
     header_text=(TextView)findViewById(R.id.header_text); 
     image_table=(TableLayout)findViewById(R.id.image_table); 

     header_text.setText("Image Table"); 
     header_left_btn.setText("Select"); 
     header_right_btn.setText("Clear"); 
     registerForContextMenu(header_left_btn); 

     header_left_btn.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) 
      { 
       // TODO Auto-generated method stub 
       openContextMenu(header_left_btn); 
      } 
     }); 

     header_right_btn.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) 
      { 
       // TODO Auto-generated method stub 
       image_list.clear(); 
       image_drawable.clear(); 
       deletePhotos(); 
       updateImageTable(); 
      } 
     }); 
    } 

    public void deletePhotos() 
    { 
     String folder=Environment.getExternalStorageDirectory() +"/LoadImg"; 
     File f=new File(folder); 
     if(f.isDirectory()) 
     { 
      File[] files=f.listFiles(); 
      Log.v("Load Image", "Total Files To Delete=====>>>>>"+files.length); 
      for(int i=0;i<files.length;i++) 
      { 
       String fpath=folder+File.separator+files[i].getName().toString().trim(); 
       System.out.println("File Full Path======>>>"+fpath); 
       File nf=new File(fpath); 
       if(nf.exists()) 
       { 
        nf.delete(); 
       } 
      } 
     } 
    } 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
    { 

     super.onCreateContextMenu(menu, v, menuInfo); 
     menu.setHeaderTitle("Post Image"); 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.camer_menu, menu); 
    } 

    @Override 
    public boolean onContextItemSelected(MenuItem item) 
    { 
     switch (item.getItemId()) 
     { 
      case R.id.take_photo: 
       //Toast.makeText(context, "Selected Take Photo", Toast.LENGTH_SHORT).show(); 
       takePhoto(); 
       break; 

      case R.id.choose_gallery: 
       //Toast.makeText(context, "Selected Gallery", Toast.LENGTH_SHORT).show(); 
       Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
       photoPickerIntent.setType("image/*"); 
       startActivityForResult(photoPickerIntent, 1); 

       break; 

      case R.id.share_cancel: 
       closeContextMenu(); 
       break; 
      default: 
      return super.onContextItemSelected(item); 
     } 
     return true; 
    } 

    public void takePhoto() 
    { 
     Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
     File folder = new File(Environment.getExternalStorageDirectory() + "/LoadImg"); 

     if(!folder.exists()) 
     { 
      folder.mkdir(); 
     }   
     final Calendar c = Calendar.getInstance(); 
     String new_Date= c.get(Calendar.DAY_OF_MONTH)+"-"+((c.get(Calendar.MONTH))+1) +"-"+c.get(Calendar.YEAR) +" " + c.get(Calendar.HOUR) + "-" + c.get(Calendar.MINUTE)+ "-"+ c.get(Calendar.SECOND); 
     path=String.format(Environment.getExternalStorageDirectory() +"/LoadImg/%s.png","LoadImg("+new_Date+")"); 
     File photo = new File(path); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photo)); 
     startActivityForResult(intent, 2); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     super.onActivityResult(requestCode, resultCode, data); 

     if(requestCode==1) 
     { 
      Uri photoUri = data.getData(); 
      if (photoUri != null) 
      { 
       String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
       Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null); 
       cursor.moveToFirst(); 
       int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       String filePath = cursor.getString(columnIndex); 
       cursor.close(); 
       Log.v("Load Image", "Gallery File Path=====>>>"+filePath); 
       image_list.add(filePath); 
       Log.v("Load Image", "Image List Size=====>>>"+image_list.size()); 

       //updateImageTable(); 
       new GetImages().execute(); 
      } 
     } 

     if(requestCode==2) 
     { 
      Log.v("Load Image", "Camera File Path=====>>>"+path); 
      image_list.add(path); 
      Log.v("Load Image", "Image List Size=====>>>"+image_list.size()); 
      //updateImageTable(); 
      new GetImages().execute(); 
     } 
    } 

    public void updateImageTable() 
    { 
     image_table.removeAllViews(); 

     if(image_drawable.size() > 0) 
     { 
      for(int i=0; i<image_drawable.size(); i++) 
      { 
       TableRow tableRow=new TableRow(this); 
       tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
       tableRow.setGravity(Gravity.CENTER_HORIZONTAL); 
       tableRow.setPadding(5, 5, 5, 5); 
       for(int j=0; j<1; j++) 
       { 
        ImageView image=new ImageView(this); 
        image.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

        /*Bitmap bitmap = BitmapFactory.decodeFile(image_list.get(i).toString().trim()); 
        bitmap = Bitmap.createScaledBitmap(bitmap,500, 500, true); 
        Drawable d=loadImagefromurl(bitmap);*/ 
        image.setBackgroundDrawable(image_drawable.get(i)); 

        tableRow.addView(image, 200, 200); 
       } 
       image_table.addView(tableRow); 
      } 
     } 
    } 

    public Drawable loadImagefromurl(Bitmap icon) 
    { 
     Drawable d=new BitmapDrawable(icon);  
     return d; 
    } 

    public class GetImages extends AsyncTask<Void, Void, Void> 
    { 
     public ProgressDialog progDialog=null; 

     protected void onPreExecute() 
     { 
      progDialog=ProgressDialog.show(context, "", "Loading...",true); 
     } 
     @Override 
     protected Void doInBackground(Void... params) 
     { 
      image_drawable.clear(); 
      for(int i=0; i<image_list.size(); i++) 
      { 
       Bitmap bitmap = BitmapFactory.decodeFile(image_list.get(i).toString().trim()); 
       bitmap = Bitmap.createScaledBitmap(bitmap,500, 500, true); 
       Drawable d=loadImagefromurl(bitmap); 

       image_drawable.add(d); 
      } 
      return null; 
     } 

     protected void onPostExecute(Void result) 
     { 
      if(progDialog.isShowing()) 
      { 
       progDialog.dismiss(); 
      } 
      updateImageTable(); 
     } 
    } 
} 

形象和表演見this

+0

謝謝你們的支持!我現在會嘗試這個 – TheQ

+0

,所以我沒有用你的線代替我的線,它沒有工作。但是,如果我使用Picasso.with(TakePhotoActivity.this).load(selectedImage).fit()。centerCrop()。into(myImageView); 然後它確實有效。那麼,畢加索能夠使用字符串imagePath或文件來處理嗎? 我希望它首先與畢加索一起工作,因爲我會在我的應用程序周圍更頻繁地使用它。你知道我該如何修復它嗎? – TheQ

+0

嘗試使用滑翔然後畢加索 –

相關問題