显示照片尺寸困难

时间:2016-01-09 14:20:32

标签: android android-image

我想要显示照片,但我总是遇到这个问题。

  

E / JavaBinder:FAILED BINDER TRANSACTION

我在互联网上阅读了有关它的所有答案,但它仍然无效。代码只处理一些照片(大小<60kB),并且大多数照片(拍摄或来自画廊)失败。
有人能帮帮我吗?

public class FotosActivity extends ActionBarActivity {
    String name = "";
    String description = "";
    String picturePath;
    byte[] imageBytes;
    private ArrayList<Highlight_Information> Hilis;
    private ImagesAdapter imageAdapter;
    private ListView listView;
    private Uri mCapturedImageURI;
    private static final int RESULT_LOAD_IMAGE = 1;
    private static final int REQUEST_IMAGE_CAPTURE = 2;
    private HighlightInformationDataSource hiliDS;
    private Highlight_Stamm hs;
    private Integer KeyOnlyFotos = 0;
    private Button btnAdd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fotos);
        Bundle params = getIntent().getExtras();
        KeyOnlyFotos = params.getInt("CALL_ONLY_PHOTOS");
        btnAdd = (Button) findViewById(R.id.btnAdd);
        if (KeyOnlyFotos == 20) {
            btnAdd.setEnabled(false);
            btnAdd.setText("Liste Aller Bilder dieses Highlights");
        }


        // Construct the data source
        Hilis = new ArrayList();
        // Create the adapter to convert the array to views
        imageAdapter = new ImagesAdapter(this, Hilis);
        // Attach the adapter to a ListView
        listView = (ListView) findViewById(R.id.main_list_view);
        listView.setAdapter(imageAdapter);
        addItemClickListener(listView);
        initDB();
        evaluateAufruf();
    }

    /**
     * initialize database
     */
    private void initDB() {
        hiliDS = new HighlightInformationDataSource(this);
        //        add images from database to images ArrayList
        hiliDS.open();
        for (Highlight_Information mi : hiliDS.getAllHighlightInformation()) {
            Hilis.add(mi);
        }
        hiliDS.close();
    }

    public void btnAddOnClick(View view) {

        final Dialog dialogFotoAufnahmeArt = new Dialog(this);
        dialogFotoAufnahmeArt.setContentView(R.layout.custom_dialog_box);
        dialogFotoAufnahmeArt.setTitle("Fotoinfos");
        final EditText editTextHiliName = (EditText) dialogFotoAufnahmeArt.findViewById(R.id.editTextHiliName);
        final EditText editTextkomentar = (EditText) dialogFotoAufnahmeArt.findViewById(R.id.editTextkomentar);
        Button btnExit = (Button) dialogFotoAufnahmeArt.findViewById(R.id.btnExit);
        btnExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogFotoAufnahmeArt.dismiss();
            }
        });
        dialogFotoAufnahmeArt.findViewById(R.id.btnChoosePath).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (editTextHiliName.getText() != null) {
                    name = editTextHiliName.getText().toString().trim();
                }
                if (name.length() == 0) {
                    Toast toast = Toast.makeText(FotosActivity.this, getResources().getString(R.string.meldung_inft_name_anlegen_leer),
                            Toast.LENGTH_SHORT);
                    toast.show();
                    return;
                }


                if (editTextkomentar.getText() != null) {
                    description = editTextkomentar.getText().toString().trim();
                }
                if (description.length() == 0) {
                    Toast toast = Toast.makeText(FotosActivity.this, getResources().getString(R.string.meldung_inft_comment_anlegen_leer),
                            Toast.LENGTH_SHORT);
                    toast.show();
                    return;
                }
                activeGallery();
                dialogFotoAufnahmeArt.dismiss();

            }
        });
        dialogFotoAufnahmeArt.findViewById(R.id.btnTakePhoto).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (editTextHiliName.getText() != null) {
                    name = editTextHiliName.getText().toString().trim();
                }
                if (name.length() == 0) {
                    Toast toast = Toast.makeText(FotosActivity.this, getResources().getString(R.string.meldung_inft_name_anlegen_leer),
                            Toast.LENGTH_SHORT);
                    toast.show();
                    return;
                }


                if (editTextkomentar.getText() != null) {
                    description = editTextkomentar.getText().toString().trim();
                }
                if (description.length() == 0) {
                    Toast toast = Toast.makeText(FotosActivity.this, getResources().getString(R.string.meldung_inft_comment_anlegen_leer),
                            Toast.LENGTH_SHORT);
                    toast.show();
                    return;
                }
                activeTakePhoto();
                dialogFotoAufnahmeArt.dismiss();
            }
        });

        // show dialog on screen
        dialogFotoAufnahmeArt.show();
    }

    /**
     * take a photo
     */
    private void activeTakePhoto() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            String fileName = "temp.jpg";
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, fileName);
            mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

    /**
     * to gallery
     */
    private void activeGallery() {
        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, RESULT_LOAD_IMAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case RESULT_LOAD_IMAGE:
                if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
                    Uri selectedImage = data.getData();
                   // File file= new File(selectedImage.getPath());
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    picturePath = cursor.getString(columnIndex);

                    InputStream iStream = null;
                    try {
                        iStream = getContentResolver().openInputStream(selectedImage);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    try {
                        imageBytes = getBytes(iStream);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    makeHighlightInfo(hs);

                }
            case REQUEST_IMAGE_CAPTURE:
                if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                    String[] projection = {MediaStore.Images.Media.DATA};
                    Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null);
                    int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    cursor.moveToFirst();
                    picturePath = cursor.getString(column_index_data);
                    InputStream iStream = null;
                    try {
                        iStream = getContentResolver().openInputStream(mCapturedImageURI);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    try {
                        imageBytes = getBytes(iStream);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    makeHighlightInfo(hs);
                }
        }
    }
    public byte[] convertBitmapToByteArray(Context context, Bitmap bitmap) {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream(bitmap.getWidth() * bitmap.getHeight());
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, buffer);
        return buffer.toByteArray();
    }
    private Bitmap decodeFile(File f) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            // The new size we want to scale to
            final int REQUIRED_SIZE=70;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while(o.outWidth / scale / 2 >= REQUIRED_SIZE &&
                    o.outHeight / scale / 2 >= REQUIRED_SIZE) {
                scale *= 2;
            }

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}
        return null;
    }

    public byte[] getBytes(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];

        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }
        return byteBuffer.toByteArray();
    }

    /**
     * item clicked listener used to implement the react action when an item is clicked.
     *
     * @param listView
     */
    private void addItemClickListener(final ListView listView) {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Highlight_Information image = (Highlight_Information) listView.getItemAtPosition(position);
                Intent intent = new Intent(getApplicationContext(), DisplayImages.class);
                intent.putExtra("HILI", (new Gson()).toJson(image));
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // Save the user's current game state
        if (mCapturedImageURI != null) {
            outState.putString("mCapturedImageURI", mCapturedImageURI.toString());
        }
        // Always call the superclass so it can save the view hierarchy state
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // Always call the superclass so it can restore the view hierarchy
        super.onRestoreInstanceState(savedInstanceState);

        // Restore state members from saved instance
        if (savedInstanceState.containsKey("mCapturedImageURI")) {
            mCapturedImageURI = Uri.parse(savedInstanceState.getString("mCapturedImageURI"));
        }
    }

    /**
     * wertet die beim Aufruf uebergebenen Parameter aus
     */
    private void evaluateAufruf() {
        // Aufruf bestimmen
        Intent i = getIntent();

        if (i.getExtras() != null) {

            hs = (Highlight_Stamm) i.getExtras().getSerializable(Constants.PARA_HIGHLIGHT_STAMM);

            try {
                i.removeExtra(Constants.PARA_HIGHLIGHT_STAMM);
            } catch (RuntimeException e) {

            }

        }

    }

    @Override
    public void onRestart() {
        super.onRestart();
        // Construct the data source
        Hilis = new ArrayList();
        // Create the adapter to convert the array to views
        imageAdapter = new ImagesAdapter(this, Hilis);
        // Attach the adapter to a ListView
        listView = (ListView) findViewById(R.id.main_list_view);
        listView.setAdapter(imageAdapter);
        addItemClickListener(listView);
        initDB();
        evaluateAufruf();
    }



    private Highlight_Information makeHighlightInfo(Highlight_Stamm hs){
        Highlight_Information hi = new Highlight_Information();

        boolean retWert = true;
        hi.setHiliHilsUuid(hs.getHilsUuid());
        hi.setHiliHilsId(hs.getHilsId());
        hi.setHiliRebeId(Constants.TOURMELDER_REBE_ID);
        hi.setHiliInftId(Constants.TOURMELDER_INFT_FOTO);
        hi.setHiliName(name);
        hi.setHiliKommentar(description);
        hi.setPath(picturePath);
        hi.setHiliObjekt(imageBytes);
        hi.setHiliUuid(UUID.randomUUID().toString());
        hi.setHiliIsInsert(Boolean.TRUE);
        hi.setHka(TourmelderHelper.getCurrentUsername());
        hi.setHkn(TourmelderHelper.getCurrentUsername());
        hi.setDatetime(System.currentTimeMillis());
        imageAdapter.add(hi);
         hiliDS = HighlightInformationDataSource.getInstance(FotosActivity.this);
        hiliDS.open();
        try {
            hiliDS.createHighlightInformationExport(hi);
            hiliDS.close();
        } catch (NotFoundException e) {
            e.printStackTrace();
            retWert = false;
        } catch (InsertException e) {
            e.printStackTrace();
            retWert = false;
        }

        return hi;
    }
}

0 个答案:

没有答案