选择图片两次时 - 应用程序崩溃+旋转图片

时间:2015-12-18 10:54:21

标签: java android android-bitmap

在我的应用程序中,有一个“上传图片”的按钮:从相机拍摄照片或从图库中选择。

在这两个选项中,有两个问题:

  1. 再次点击上传其他图片时,应用程序崩溃。

  2. 在第一次“上传”时,图片在侧面旋转(逆时针旋转90度)。

  3. import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.database.Cursor;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.Bundle;
    import android.app.Fragment;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.Toast;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    import entities.Order;
    
    public class SellABookFragment extends Fragment implements View.OnClickListener {
    
        private ImageView ivBookPicture;
        private EditText etBookName, etBookAuthor, etBookGenre, etBookPublishing, etQuantity, etBookPrice, etBookDetails;
        private Button bUploadPicture, bAddBook;
    
        public SellABookFragment() {
            // Required empty public constructor
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_sell_a_book, container, false);
            ivBookPicture = (ImageView) view.findViewById(R.id.ivBook_Picture);
            etBookName = (EditText) view.findViewById(R.id.etBook_Name);
            etBookAuthor = (EditText) view.findViewById(R.id.etAuthor_Name);
            etBookGenre = (EditText) view.findViewById(R.id.etGenre);
            etBookPublishing = (EditText) view.findViewById(R.id.etPublishing_Year);
            etQuantity = (EditText) view.findViewById(R.id.etBook_Quantity);
            etBookPrice = (EditText) view.findViewById(R.id.etBook_Price);
            etBookDetails = (EditText) view.findViewById(R.id.etBook_Details);
            bUploadPicture = (Button) view.findViewById(R.id.bUpload_Picture);
            bAddBook = (Button) view.findViewById(R.id.bAdd_Book);
    
            bUploadPicture.setOnClickListener(this);
            bAddBook.setOnClickListener(this);
            return view;
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.bUpload_Picture:
                    selectPicture();
                    break;
                case R.id.bAdd_Book:
                    addBook();
                    break;
            }
        }
    
        private void addBook() {
            try {
                HomeActivity.backEnd.addOrder(new Order(HomeActivity.LoggedUser.getID(),
                        etBookGenre.getText().toString(),
                        etBookName.getText().toString(),
                        Integer.parseInt(etBookPublishing.getText().toString()),
                        etBookAuthor.getText().toString(),
                        Double.parseDouble(etBookPrice.getText().toString()),
                        (Integer) ivBookPicture.getTag()));
            } catch (Exception e) {
                Toast.makeText(getActivity().getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    
        private void selectPicture() {
            final CharSequence[] options = {
                    getResources().getString(R.string.take_photo),
                    getResources().getString(R.string.gallery_choose),
                    getResources().getString(R.string.cancel)};
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle(getResources().getString(R.string.upload_picture));
            builder.setItems(options, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int item) {
                    if (options[item].equals(getResources().getString(R.string.take_photo))) {
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        File f = new File(android.os.Environment.getExternalStorageDirectory(), "tmp.jpg");
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                        startActivityForResult(intent, 1);
                    } else if (options[item].equals(getResources().getString(R.string.gallery_choose))) {
                        Intent intent = new Intent(Intent.ACTION_PICK,
                                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(intent, 2);
                    } else
                        dialog.dismiss();
                }
            });
            builder.show();
        }
    
        Bitmap bitmap;
    
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            OutputStream outFile;
            String path = android.os.Environment.getExternalStorageDirectory()
                    + File.separator
                    + "MyApp";
            File file;
            if (resultCode == Activity.RESULT_OK) {
                if (bitmap != null) {
                    ivBookPicture.setImageBitmap(null);
                    bitmap.recycle();
                    bitmap = null;
                }
                try {
                    if (requestCode == 1) {
                        File f = new File(Environment.getExternalStorageDirectory().toString());
                        for (File tmp : f.listFiles()) {
                            if (tmp.getName().equals("tmp.jpg")) {
                                f = tmp;
                                break;
                            }
                        }
                        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                        bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
                        ivBookPicture.setImageBitmap(bitmap);
                        if (f.delete()) {
                            file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                            try {
                                outFile = new FileOutputStream(file);
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                        }
                    } else if (requestCode == 2) {
                        Uri selectedImage = data.getData();
                        String[] filePath = {MediaStore.Images.Media.DATA};
                        Cursor c = getActivity().getContentResolver().query(selectedImage, filePath, null, null, null);
                        c.moveToFirst();
                        int columnIndex = c.getColumnIndex(filePath[0]);
                        String picturePath = c.getString(columnIndex);
                        c.close();
                        bitmap = (BitmapFactory.decodeFile(picturePath));
                        Log.w("image path", picturePath);
                        ivBookPicture.setImageBitmap(bitmap);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    包含上传图片的ImageView:

    <ImageView
        android:id="@+id/ivBook_Picture"
        android:layout_width="@dimen/sell_a_book_picture_size"
        android:layout_height="@dimen/sell_a_book_picture_size"
        android:src="@mipmap/ic_launcher" />

    谢谢!

    我在拍两次照片时附上错误日志。关于轮换问题,除了上面的代码,我没有任何补充。

    E/AndroidRuntime: FATAL EXCEPTION: main
    E/AndroidRuntime: java.lang.OutOfMemoryError
    E/AndroidRuntime:     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
    E/AndroidRuntime:     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
    E/AndroidRuntime:     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:378)
    E/AndroidRuntime:     at com.example.amit.java5776_7546_8412.SellABookFragment.onActivityResult(SellABook
    E/AndroidRuntime:     at android.app.Activity.dispatchActivityResult(Activity.java:5567)
    E/AndroidRuntime:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3496)
    E/AndroidRuntime:     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2906)
    E/AndroidRuntime:     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2948)
    E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354)
    E/AndroidRuntime:     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3877)
    E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:159)
    E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1322)
    E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
    E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:176)
    E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5419)
    E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
    E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:525)
    E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
    E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
    E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

应用程序崩溃,因为图片使用了大量内存。

您需要先将位图的大小调整为较小的大小(大约200dp X 200dp),然后将其设置为ImageView

使用此回答here调整位图大小:

更新:此外,您必须在AsyncTask内调用调整大小功能,并使用ImageView将位图设置为activity.runOnUiThread