我有两个imageView,当用户点击一个按钮时,它会将它们发送到图库以选择图像。然后他们选择的图像显示在imageView中,然后他们可以根据他们的设置做两件事之一: 1)背景 2)图标
我的背景工作正常,但现在我试图包含设置图标的选项。我在这里有图标的imageView(在widget.xml中):
<ImageView
android:id="@+id/bwidgetOpen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:contentDescription="@string/desc"/>
这是我的两个编码类: Personalize.java:
package com.example.awesomefilebuilderwidget;
IMPORTS
public class Personalize extends Activity implements View.OnClickListener {
Button button;
ImageView image;
ImageView image2;
Button btnChangeImage;
Button btnChangeImageForIcon;
Button btnSetBackground;
private static final int SELECT_PICTURE = 1;
private static final int SELECT_PICTURE_2 = 2;
private String selectedImagePath;
Bitmap background;
Bitmap b2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.personalize);
Log.d("Personalize", "OnCreate called");
image = (ImageView) findViewById(R.id.imageView1);
image2 = (ImageView) findViewById(R.id.imageView2Icon);
Button btnChangeImage = (Button) findViewById(R.id.btnChangeImage);
btnChangeImage.setOnClickListener(this);
Button btnChangeImageForIcon = (Button) findViewById(R.id.btnChangeImageForIcon);
btnChangeImageForIcon.setOnClickListener(this);
Button btnSetBackground = (Button) findViewById(R.id.btnSetBackground);
btnSetBackground.setOnClickListener(this);
Button btnLinkToFeedback = (Button) findViewById(R.id.btnLinkToFeedback);
Button btnSetIcon = (Button) findViewById(R.id.btnSetIcon);
btnSetIcon.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnChangeImage:
launchImageChooser();
break;
case R.id.btnChangeImageForIcon:
launchImageChooser();
break;
case R.id.btnSetBackground:
setBackgroundImageInDragAndDrop();
break;
case R.id.btnSetIcon:
setIconImageInWidget();
break;
}
}
private void setIconImageInWidget() {
// TODO Auto-generated method stub
Log.d("Personalize", "setIconImageInWidget() called");
Intent i = getIntent();
//Convert bitmap to byte array to send back to activity
// See: http://stackoverflow.com/questions/11010386/send-bitmap-using-intent-android
scaleDownBitmapForIcon(b2, 500, this.getBaseContext());
Log.d("Personalize", "Scale Bitmap Chosen For Icon");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b2.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[]byteArray = stream.toByteArray();
i.putExtra("myIconBitmap", byteArray);
setResult(RESULT_OK, i);
finish();
}
public static Bitmap scaleDownBitmap(Bitmap background, int newHeight, Context c) {
final float densityMultiplier = c.getResources().getDisplayMetrics().density;
int h= (int) (500*densityMultiplier);
int w= (int) (h * background.getWidth()/((double) background.getHeight()));
background=Bitmap.createScaledBitmap(background, w, h, true);
// TO SOLVE LOOK AT HERE:http://stackoverflow.com/questions/15517176/passing-bitmap-to-other-activity-getting-message-on-logcat-failed-binder-transac
return background;
}
public static Bitmap scaleDownBitmapForIcon(Bitmap b2, int newHeight, Context c) {
final float densityMultiplier = c.getResources().getDisplayMetrics().density;
int h= (int) (500*densityMultiplier);
int w= (int) (h * b2.getWidth()/((double) b2.getHeight()));
b2=Bitmap.createScaledBitmap(b2, w, h, true);
// TO SOLVE LOOK AT HERE:http://stackoverflow.com/questions/15517176/passing-bitmap-to-other-activity-getting-message-on-logcat-failed-binder-transac
return b2;
}
private void setBackgroundImageInDragAndDrop() {
Log.d("Personalize", "setBackgroundImageInDragAndDrop() called");
Intent i = getIntent();
//Convert bitmap to byte array to send back to activity
// See: http://stackoverflow.com/questions/11010386/send-bitmap-using-intent-android
scaleDownBitmap(background, 500, this.getBaseContext());
Log.d("Personalize", "Scale Bitmap Chosen");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
background.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[]byteArray = stream.toByteArray();
i.putExtra("myBackgroundBitmap", byteArray);
setResult(RESULT_OK, i);
finish();
}
private void launchImageChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, SELECT_PICTURE);
startActivityForResult(intent, SELECT_PICTURE_2);
Log.d("Personalize", "launchImageChooser called");
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String imagePath = cursor.getString(column_index);
if(cursor != null) {
cursor.close();
}
return imagePath;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE)
{
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
background = getAndDecodeImage(selectedImagePath);
if(background != null){
image.setImageBitmap(background);
}
} else if (requestCode == SELECT_PICTURE_2)
{
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Bitmap b2 = getAndDecodeImage(selectedImagePath);
if(b2 != null){
image2.setImageBitmap(b2);
}
}
}
}
private Bitmap getAndDecodeImage(String selectedImagePath){
try {
Log.d("Personalize", "selectedImagePath: " + selectedImagePath);
FileInputStream fileis=new FileInputStream(selectedImagePath);
BufferedInputStream bufferedstream=new BufferedInputStream(fileis);
byte[] bMapArray= new byte[bufferedstream.available()];
bufferedstream.read(bMapArray);
Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
if (fileis != null)
{
fileis.close();
}
if (bufferedstream != null)
{
bufferedstream.close();
}
return bMap;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public boolean saveImageToInternalStorage(Bitmap image) {
try {
FileOutputStream fos = this.openFileOutput("desiredFilename.png", Context.MODE_PRIVATE);
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
return true;
} catch (Exception e) {
return false;
}
}
}
然后Drag_and_Drop.java:
package com.example.awesomefilebuilderwidget;
IMPORTS
public class Drag_and_Drop_App extends Activity {
private static final int SET_BACKGROUND = 10;
private static final int SET_ICON = 20;
private ListView mListAppInfo;
// Search EditText
EditText inputSearch;
public AppInfoAdapter adapter;
final SwipeDetector swipeDetector = new SwipeDetector();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set layout for the main screen
setContentView(R.layout.drag_and_drop_app);
Log.d("D&D", "onCreate called");
}
public Bitmap getThumbnail(String filename) {
Bitmap thumbnail = null;
try {
File filePath = this.getFileStreamPath(filename);
FileInputStream fi = new FileInputStream(filePath);
thumbnail = BitmapFactory.decodeStream(fi);
} catch (Exception ex) {
Log.e("getThumbnail() on internal storage", ex.getMessage());
}
return thumbnail;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("Drag_and_Drop_App", "requestCode: " + requestCode + ", resultCode: " + resultCode);
if(requestCode == SET_BACKGROUND && resultCode == RESULT_OK){
byte[] byteArray = data.getByteArrayExtra("myBackgroundBitmap");
Bitmap myBackground = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
setBackgroundImage(myBackground);
}
else if(requestCode == SET_ICON && resultCode == RESULT_OK){
byte[] byteArray = data.getByteArrayExtra("myIconBitmap");
Bitmap myIcon = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
setBackgroundImageForIcon(myIcon);
}
}
@SuppressLint("NewApi")
private void setBackgroundImage(Bitmap bitmap) {
RelativeLayout yourBackgroundView = (RelativeLayout) findViewById(R.id.rl_drag_and_drop_app);
Drawable d = new BitmapDrawable(getResources(), bitmap);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
yourBackgroundView.setBackgroundDrawable(d);
} else {
yourBackgroundView.setBackground(d);
}
}
@SuppressLint("NewApi")
private void setBackgroundImageForIcon(Bitmap bitmap) {
LinearLayout yourIconView = (LinearLayout) findViewById(R.id.widgetXMl);
Drawable dq = new BitmapDrawable(getResources(), bitmap);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
yourIconView.setBackgroundDrawable(dq);
} else {
yourIconView.setBackground(dq);
}
}
}
我现在遇到的问题是,当我点击按钮选择图标图像时,我会去图库并选择一张图片,然后重新加载图库并让我选择另一张图片(最终会转到另一个图像视图)。我该如何解决这个问题?
这是我的Personalize.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/personalizetextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/customize"
android:textSize="30sp"
android:gravity="center"
android:layout_marginTop="20dip"/>
<TextView
android:id="@+id/personalizetextviewChangeBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/customizebackground"
android:gravity="center" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="100dp"
android:maxHeight="100dp"
android:scaleType="fitCenter"
android:contentDescription="@string/descForBackground"
/>
<Button
android:id="@+id/btnChangeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/change_background" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/set_background"
android:id="@+id/btnSetBackground" />
<TextView
android:id="@+id/personalizetextviewChangeIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/change_icon"
android:gravity="center" />
<ImageView
android:id="@+id/imageView2Icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="100dp"
android:maxHeight="100dp"
android:scaleType="fitCenter"
android:contentDescription="@string/descForIcon"
/>
<Button
android:id="@+id/btnChangeImageForIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/change_icon" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/set_icon"
android:id="@+id/btnSetIcon" />
<Button
android:id="@+id/btnLinkToFeedback"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dip"
android:background="@null"
android:text="@string/Send_Feedback"
android:textColor="#21dbd4"
android:textStyle="bold" />
</LinearLayout>
另外,有没有办法可以清理我的代码,这样我就不会重复那么多了?
我知道这是一个需要查看的代码,但如果有人可以的话,我会真的感到满意。
谢谢!
答案 0 :(得分:0)
it reloads the gallery and makes me choose another image
因为您两次致电startActivityForResult
startActivityForResult(intent, SELECT_PICTURE);
startActivityForResult(intent, SELECT_PICTURE_2);
在launchImageChooser()中。更改此代码。