2015-04-08 54 views
0

即時通訊尋找在同一個佈局上有兩個按鈕,您單擊第一個按鈕,選擇一個圖像,該按鈕將變爲所選圖像。您單擊第二個按鈕並選擇該圖像將替換該按鈕。最容易使用imageButton而不是ImageView。 如果可能,我想要代碼,謝謝。從圖庫加載圖像到ImageButton(查看)

(靜止不明白的端應爲2倍的圖像彼此相鄰由用戶選擇?)

MainActivity:

package com.example.triptych; 

import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.graphics.BitmapFactory; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.view.View; 
import android.widget.ImageButton; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
    private static int RESULT_LOAD_IMG = 1; 
    String imgDecodableString; 

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

    public void loadImagefromGallery(View view) { 
     // Create intent to Open Image applications like Gallery, Google Photos 
     Intent galleryIntent = new Intent(Intent.ACTION_PICK, 
       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     // Start the Intent 
     startActivityForResult(galleryIntent, RESULT_LOAD_IMG); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     try { 
      // When an Image is picked 
      if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK 
        && null != data) { 
       // Get the Image from data 

       Uri selectedImage = data.getData(); 
       String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

       // Get the cursor 
       Cursor cursor = getContentResolver().query(selectedImage, 
         filePathColumn, null, null, null); 
       // Move to first row 
       cursor.moveToFirst(); 

       int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       imgDecodableString = cursor.getString(columnIndex); 
       cursor.close(); 
       ImageButton imageButton = (ImageButton) findViewById(R.id.buttonLoadPicture); 
       // Set the Image in ImageView after decoding the String 
       imageButton.setImageBitmap(BitmapFactory 
         .decodeFile(imgDecodableString)); 

      } else { 
       Toast.makeText(this, "You haven't picked Image", 
         Toast.LENGTH_LONG).show(); 
      } 
     } catch (Exception e) { 
      Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG) 
        .show(); 
     } 

    } 

} 

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ImageButton 
     android:id="@+id/buttonLoadPicture" 
     android:layout_width="160dp" 
     android:layout_height="300dp" 
     android:layout_marginLeft="10dp" 
     android:layout_weight="0.51" 
     android:contentDescription="TODO" 
     android:onClick="loadImagefromGallery" 
     android:src="@drawable/ic_launcher" 
     android:text="@string/load_picture" /> 

    <ImageButton 
     android:id="@+id/button2" 
     android:layout_width="160dp" 
     android:layout_height="300dp" 
     android:layout_marginLeft="180dp" 
     android:layout_weight="0.51" 
     android:contentDescription="TODO" 
     android:onClick="loadImagefromGallery" 
     android:src="@drawable/ic_launcher" 
     android:text="@string/load_picture" /> 

</RelativeLayout> 

另一個問題,我是否爲第二個按鈕創建另一個活動並粘貼相同的代碼,或者我是否對同一活動執行此操作?

+1

這裏的每個人都不會爲你寫代碼。如果你有書面的代碼,然後告訴我們。我們可以幫助解決問題。 @Harrison – Pooja

+0

我已經編寫了代碼,我將編輯帖子,但是我注意到每次寫出代碼時,我都會在帖子中收到負面反饋。 – Harrison

+0

負面反饋背後可能有某種原因。 @Harrison – Pooja

回答

1
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 
    <ImageButton 
    android:id="@+id/buttonLoadPicture" 
    android:layout_width="160dp" 
    android:layout_height="300dp" 
    android:layout_marginLeft="10dp" 
    android:layout_weight="0.51" 
    android:contentDescription="TODO" 
    android:onClick="loadImagefromGallery" 
    android:src="@drawable/ic_launcher" 
    android:text="@string/load_picture" /> 

<ImageButton 
    android:id="@+id/button2" 
    android:layout_width="160dp" 
    android:layout_height="300dp" 
    android:layout_marginLeft="180dp" 
    android:layout_weight="0.51" 
    android:contentDescription="TODO" 
    android:onClick="loadImagefromGallery" 
    android:src="@drawable/ic_launcher" 
    android:text="@string/load_picture" /> 

0

你可以嘗試這樣的事情:

在你MainActivity類,你需要創建2種不同的方法來處理圖庫中選擇圖片,並把它在兩個不同的點擊圖片 按鈕,請參閱以下代碼: 您的MainActivity.jav a將如下所示:

public class MainActivity extends ActionBarActivity { 
    private static int RESULT_LOAD_IMG = 1, RESULT_LOAD_IMG_TWO = 2; 
    String imgDecodableString, imgDecodableStringTwo; 
    ImageButton btn_load, btn_loadTwo; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     btn_load = (ImageButton) findViewById(R.id.buttonLoadPicture); 
     btn_loadTwo = (ImageButton) findViewById(R.id.buttonLoad); 
     btn_loadTwo.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       loadImagefromGalleryTwo(btn_loadTwo); 
      } 
     }); 
     btn_load.setOnClickListener(new OnClickListener() { 

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

    public void loadImagefromGallery(View view) { 
     Intent galleryIntent = new Intent(Intent.ACTION_PICK, 
       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(galleryIntent, RESULT_LOAD_IMG); 
    } 

    public void loadImagefromGalleryTwo(View view) { 
     Intent galleryIntent = new Intent(Intent.ACTION_PICK, 
       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(galleryIntent, RESULT_LOAD_IMG_TWO); 
    } 

    @SuppressLint("NewApi") 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     try { 
      if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) { 

       Uri selectedImage = data.getData(); 
       String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

       Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
       cursor.moveToFirst(); 

       int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       imgDecodableString = cursor.getString(columnIndex); 
       cursor.close(); 

       Drawable d = new BitmapDrawable(getResources(), BitmapFactory.decodeFile(imgDecodableString)); 
       btn_load.setBackground(d); 

      } else if (requestCode == RESULT_LOAD_IMG_TWO && resultCode == RESULT_OK && null != data) { 
       Uri selectedImage = data.getData(); 
       String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

       Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
       cursor.moveToFirst(); 

       int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       imgDecodableStringTwo = cursor.getString(columnIndex); 
       cursor.close(); 

       Drawable d = new BitmapDrawable(getResources(), BitmapFactory.decodeFile(imgDecodableStringTwo)); 
       btn_loadTwo.setBackground(d); 
      } 
     } catch (Exception e) { 
      Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show(); 
     } 

    } 
} 

輸出: enter image description here

希望它能幫助!

+0

嘿,男士,謝謝你的回覆!我使用了代碼,但.setOnClickListener無法正常工作,即時通訊出錯,是否適用於您? – Harrison

0

getContentResolver()。查詢不應該從UI線程調用

相關問題