Android"TAKE PHOTO" -单击的图像被保存为损坏的图像 [英] Android "TAKE PHOTO" - Image which is clicked is getting saved as a corrupt image

查看:108
本文介绍了Android"TAKE PHOTO" -单击的图像被保存为损坏的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,它打开一个对话框,要求用户拍摄照片"或从图库中选择".

I have a button, which opens up a dialog box asking user to either "Take Picture" or "Choose from gallery".

我遇到了问题,当用户单击拍摄照片"时,图像被单击,出于验证目的,我将Bitmap图像设置在circleImage视图中,但是当我转到图像的指定位置时图片,或者图片不存在或图片已损坏.

I am facing issues when user "Take photo" , image is getting clicked, and for verification purpose I am setting Bitmap image inside the circularImage view, but when I go to specified location path of the image, either Image is not there or Image is corrupted.

我也试图使用android中的AsyncHttpClient将图像上传到服务器,但无法成功完成.

Also I am trying to upload the image to the server using AsyncHttpClient in android but not being able to do it successfully.

每次我都收到Java套接字超时异常.

Everytime I am getting Java Socket TimeOut Exception.

以下是我的相机意图活动

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);
    } 
}

推荐答案

我简化了您的代码.保留对全局文件路径的引用

I simplified your code .keep reference of file path global

 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();
            }
        } 

    }

}

这篇关于Android"TAKE PHOTO" -单击的图像被保存为损坏的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆