Android 安卓;拍照单击的图像将保存为损坏的图像
我有一个按钮,它打开一个对话框,要求用户“拍照”或“从图库中选择” 我面临问题当用户“拍照”时,单击图像,出于验证目的,我在circularImage视图中设置位图图像,但当我转到图像的指定位置路径时,要么图像不存在,要么图像已损坏 此外,我正在尝试使用android中的AsyncHttpClient将图像上载到服务器,但未能成功完成 每次我收到Java套接字超时异常 下面是我的相机意图活动的代码Android 安卓;拍照单击的图像将保存为损坏的图像,android,image,android-camera,image-uploading,androidhttpclient,Android,Image,Android Camera,Image Uploading,Androidhttpclient,我有一个按钮,它打开一个对话框,要求用户“拍照”或“从图库中选择” 我面临问题当用户“拍照”时,单击图像,出于验证目的,我在circularImage视图中设置位图图像,但当我转到图像的指定位置路径时,要么图像不存在,要么图像已损坏 此外,我正在尝试使用android中的AsyncHttpClient将图像上载到服务器,但未能成功完成 每次我收到Java套接字超时异常 下面是我的相机意图活动的代码 public class AddAnUpdateActivity extends ActionBa
public class AddAnUpdateActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.composeEditText = (EditText) findViewById(R.id.composeEditText);
setContentView(R.layout.add_update);
ProfilePictureImage = (CircularImageView) findViewById(R.id.ProfilePic);
insertVideo = (ImageButton) findViewById(R.id.insertVideoButton);
setBtnListenerOrDisable(insertVideo,mTakeVidOnClickListener, MediaStore.ACTION_VIDEO_CAPTURE);
insertImage = (ImageButton) findViewById(R.id.insertImageButton);
insertImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
}
});
}
private void setBtnListenerOrDisable(ImageButton btn,
Button.OnClickListener onClickListener,
String intentName) {
if (isIntentAvailable(this, intentName)) {
btn.setOnClickListener(onClickListener);
} else {
btn.setClickable(false);
}
}
private boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(action);
List<ResolveInfo> list =
packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
private void selectImage() {
final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(AddAnUpdateActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(options,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if(options[item].equals("Take Photo"))
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "Image.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);
}
else if (options[item].equals("Choose from Gallery"))
{
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
}
else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@SuppressLint("Assert")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
File f = new File(Environment.getExternalStorageDirectory().toString());
Log.d("PhotoImage","file path:"+f);
Log.d("PhotoImage","list of file path:"+ Arrays.toString(f.listFiles()));
for (File temp : f.listFiles()) {
if (temp.getName().equals("Image.jpg")) {
Log.w("PhotoImage","enter in if block");
f = temp;
break;
}
}
try {
Log.w("PhotoImage","enter in else block");
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),bitmapOptions);
ProfilePictureImage.setImageBitmap(bitmap);
if(bitmap!=null)
{
bitmap.recycle();
bitmap=null;
}
String path = android.os.Environment.getExternalStorageDirectory()+ File.separator+ "Pictures" + File.separator + "Screenshots";
Log.w("PhotoImage","path where the image is stored :"+path);
setFilePath(path);
f.delete();
OutputStream outFile;
File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
Log.w("PhotoImage","file value:"+String.valueOf(System.currentTimeMillis()) + ".jpg");
try {
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
outFile.flush();
outFile.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == 2) {
Uri selectedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
setFilePath(picturePath);
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
Log.d("PhotoImage path of image from gallery......******************.........", picturePath + "");
ProfilePictureImage.setImageBitmap(thumbnail);
}
else if(requestCode == 3){
handleCameraVideo(data) ;
}
}
}
private void handleCameraVideo(Intent data) {
VideoUri = data.getData();
VideoView.setVideoURI(VideoUri);
//mImageBitmap = null;
} }
private void startActivityFeedActivity() {
Intent i = new Intent(getApplicationContext(), ActivityFeedActivity.class);
startActivity(i);
}
}
公共类AddAnUpdateActivity扩展了ActionBarActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.composeditext=(EditText)findViewById(R.id.composeditext);
setContentView(R.layout.add\u update);
ProfilePictureImage=(CircularImageView)findViewById(R.id.ProfilePic);
insertVideo=(ImageButton)findViewById(R.id.insertVideoButton);
SetbtListenerOrdisable(插入视频、mTakeVidOnClickListener、MediaStore.ACTION\u视频捕获);
insertImageButton=(ImageButton)findViewById(R.id.insertImageButton);
insertImage.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
选择image();
}
});
}
私有无效设置ListenerOrdinable(ImageButton btn,
Button.OnClickListener OnClickListener,
字符串名称){
如果(isIntentAvailable(本,意向名称)){
btn.setOnClickListener(onClickListener);
}否则{
btn.可设置可点击(错误);
}
}
私有布尔值isIntentAvailable(上下文、字符串操作){
final-PackageManager-PackageManager=context.getPackageManager();
最终意图=新意图(行动);
列表=
packageManager.QueryInputActivities(意图,
PackageManager.MATCH_(仅限默认值);
返回列表.size()>0;
}
私有void selectImage(){
final CharSequence[]选项={“拍照”、“从图库中选择”、“取消”};
AlertDialog.Builder=新建AlertDialog.Builder(AddAnUpdateActivity.this);
builder.setTitle(“添加照片!”);
setItems(选项,新的DialogInterface.OnClickListener(){
@凌驾
公共void onClick(对话框接口对话框,int项){
如果(选项[item].equals(“拍照”))
{
意向意向=新意向(MediaStore.ACTION\u IMAGE\u CAPTURE);
文件f=新文件(android.os.Environment.getExternalStorageDirectory(),“Image.jpg”);
intent.putExtra(MediaStore.EXTRA_输出,Uri.fromFile(f));
startActivityForResult(意向,1);
}
else if(选项[item].equals(“从库中选择”))
{
Intent Intent=新Intent(Intent.ACTION\u PICK,android.provider.MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI);
startActivityForResult(意向书,2);
}
else if(选项[item].equals(“取消”)){
dialog.dismise();
}
}
});
builder.show();
}
@SuppressLint(“断言”)
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
if(resultCode==RESULT\u OK){
if(requestCode==1){
文件f=新文件(Environment.getExternalStorageDirectory().toString());
Log.d(“PhotoImage”,“文件路径:”+f);
Log.d(“PhotoImage”,“文件路径列表:”+Arrays.toString(f.listFiles()));
对于(文件温度:f.listFiles()){
if(temp.getName().equals(“Image.jpg”)){
Log.w(“PhotoImage”,“输入if块”);
f=温度;
打破
}
}
试一试{
Log.w(“PhotoImage”,“在else块中输入”);
位图;
选项bitmapOptions=新的BitmapFactory.Options();
位图=BitmapFactory.decodeFile(f.getAbsolutePath(),bitmapOptions);
ProfilePictureImage.setImageBitmap(位图);
if(位图!=null)
{
bitmap.recycle();
位图=空;
}
String path=android.os.Environment.getExternalStorageDirectory()+File.separator+“Pictures”+File.separator+“Screenshots”;
Log.w(“PhotoImage”,“存储图像的路径:“+path”);
setFilePath(路径);
f、 删除();
输出流输出文件;
File File=new File(路径,String.valueOf(System.currentTimeMillis())+“.jpg”);
Log.w(“PhotoImage”,“文件值:”+String.valueOf(System.currentTimeMillis())+“.jpg”);
试一试{
outFile=新文件OutputStream(文件);
bitmap.compress(bitmap.CompressFormat.JPEG,85,outFile);
outFile.flush();
outFile.close();
}捕获(例外e){
e、 printStackTrace();
}
}捕获(例外e){
e、 printStackTrace();
}
}否则,如果(请求)
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "Image.jpg");
globalpath =f.getAbsolutePath(); //String make it global
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
File myfile = new File(globalpath);
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(myfile.getAbsolutePath(),
bitmapOptions);
ProfilePictureImage.setImageBitmap(bitmap);
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Pictures" + File.separator + "Screenshots";
OutputStream outFile;
File file = new File(path, String.valueOf(System
.currentTimeMillis()) + ".jpg");
try {
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
outFile.flush();
outFile.close();
myfile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(myNewFile)));
insertImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(myNewFile)));
}
});