我有一个活动,其布局只有一个图像视图,我想在用户点击图像时创建它,一个对话框显示按钮以保存图像,但对话框没有出现,是什么错?
public class ShowImage extends Activity {
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_image);
iv = (ImageView) findViewById(R.id.ivShowImage);
String imgUrlPortrait = "some URL";
ImageLoader.getInstance().displayImage(imgUrlPortrait, iv);
}
public void saveImg(View v) {
final AlertDialog.Builder db = new AlertDialog.Builder(this);
db.setNeutralButton("Save Image", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File sdCardDirectory = Environment.getExternalStorageDirectory();
File image = new File(sdCardDirectory, "img" + System.currentTimeMillis() + ".png");
boolean success = false;
try {
FileOutputStream outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Image saved to SD Card", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Error during image saving", Toast.LENGTH_LONG).show();
}
AlertDialog dlg = db.create();
dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
dlg.show();
}
});
}
}
这是布局
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ivShowImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/app_name"
android:onClick="saveImg"
tools:context=".ShowImage" >
</ImageView>
这是清单中的活动
<activity
android:name="com.package.ShowImage"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="sensorLandscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
</activity>
答案 0 :(得分:0)
您似乎忘了在ImageView上设置监听器。尝试在onCreate的末尾添加:
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
saveImg(iv);
}
});
答案 1 :(得分:0)
尝试使用android:clickable="true"
这样的ImageView
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ivShowImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/app_name"
android:onClick="saveImg"
android:clickable="true"
tools:context=".ShowImage" >
</ImageView>
答案 2 :(得分:0)
结果显示警告对话框未正确放置的代码,现在它的工作原理如下
public class ShowImage extends Activity {
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_image);
iv = (ImageView) findViewById(R.id.ivShowImage);
String imgUrlPortrait = "some URL";
ImageLoader.getInstance().displayImage(imgUrlPortrait, iv);
}
public void saveImg(View v) {
final AlertDialog.Builder db = new AlertDialog.Builder(this);
db.setNeutralButton("Save Image", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File sdCardDirectory = Environment.getExternalStorageDirectory();
File image = new File(sdCardDirectory, "img" + System.currentTimeMillis() + ".png");
boolean success = false;
try {
FileOutputStream outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Image saved to SD Card", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Error during image saving", Toast.LENGTH_LONG).show();
}
}
});
AlertDialog dlg = db.create();
dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
dlg.show();
}
}