我是android编程的新手。我希望在我的布局中拍摄相机后显示我拍摄的照片,并且还要显示将从照片库中选择的照片。我为此选项创建了一个警告对话框,并设置了上传功能的隐含意图。但是当我要去运行程序时会发生ClassCastException并且它会显示
Caused by:java.lang.classcastException android.widget.ImageView cannot be cast to android.widget.Button
I have a selectbutton and a ImageView in my activity_layout. here is my code
主要活动
package com.example.imagelist;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.CursorLoader;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainImageListActivity extends Activity implements OnClickListener
{
int REQUEST_CAMERA=0;
int SELECT_FILE=1;
Button selectButton;
ImageView imgview;
public String[]dialougeitems={"Capture Photo","Choose From Gallery","Cancel"};
FileOutputStream fos;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_image_list);
initialComponent();
selectButton.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_image_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void initialComponent()
{
selectButton=(Button)findViewById(R.id.button1);
imgview=(ImageView)findViewById(R.id.imageView1);
}
public void imageSelection()
{
AlertDialog.Builder alertbuilder=new AlertDialog.Builder(MainImageListActivity.this);
alertbuilder.setTitle("Add Photo");
alertbuilder.setItems(dialougeitems, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int items)
{
// TODO Auto-generated method stub
if(dialougeitems[items].equals("Capture Photo"))
{
Intent captureintent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureintent,REQUEST_CAMERA);
}
else if(dialougeitems[items].equals("Choose From Gallery"))
{
Intent gallerychose=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
gallerychose.setType("image/*");
startActivityForResult(Intent.createChooser(gallerychose, "SELECT FILE"),SELECT_FILE);
}
else if(dialougeitems[items].equals("Cancel"))
{
dialog.dismiss();
}
}
});
alertbuilder.show();
}
@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK)
{
if(requestCode==REQUEST_CAMERA)
{
Bitmap thumbshot=(Bitmap)data.getExtras().get("data");
ByteArrayOutputStream bytes=new ByteArrayOutputStream();
thumbshot.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destfolder=new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
try
{
destfolder.createNewFile();
fos=new FileOutputStream(destfolder);
fos.write(bytes.toByteArray());
fos.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException ie)
{
ie.printStackTrace();
}
imgview.setImageBitmap(thumbshot);
}
else if(requestCode==SELECT_FILE)
{
Uri galleryImageUri=data.getData();
String[]projection={MediaColumns.DATA};
CursorLoader imagecursorloader=new CursorLoader(this,galleryImageUri,projection,null,null, null);
Cursor imagecursor=imagecursorloader.loadInBackground();
int imageColumnIndex=imagecursor.getColumnIndexOrThrow(MediaColumns.DATA);
imagecursor.moveToFirst();
String imagepath=imagecursor.getString(imageColumnIndex);
BitmapFactory.Options mapoption=new BitmapFactory.Options();
mapoption.inJustDecodeBounds=true;
BitmapFactory.decodeFile(imagepath,mapoption);
final int REQUIRED_SIZE=200;
int scale=1;
while(mapoption.outWidth/scale/2 >=REQUIRED_SIZE
&& mapoption.outHeight/scale/2>=REQUIRED_SIZE)
scale*=2;
mapoption.inSampleSize=scale;
mapoption.inJustDecodeBounds=false;
bitmap=BitmapFactory.decodeFile(imagepath,mapoption);
imgview.setImageBitmap(bitmap);
}
}
}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
imageSelection();
}
}
此外,我想在动态列表中显示我的imagefile路径,这意味着我将选择pic,链接将自动添加到列表中。这是我的最终目标,但我从一开始就坚持下去。我需要指导进行。我该如何解决这个当前的异常。任何帮助将不胜感激
答案 0 :(得分:0)
你有一个ImageView。你正试图把它当作Button。如果返回的视图是Button或Button的子类,那么这是唯一合法的。即使您将其作为一个整体使用,ImageView也不是。将其转换为正确的类类型。