Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I guess I finally got it to work. First I used Emil's advice and saved the image. DCIM_PATH is the path to the DCIM folder. </p> <pre><code>public void takePhoto() { String directoryPath = DCIM_PATH; d("takePhoto directoryPath: " + directoryPath); this.pictureFileName = Long.toHexString(System.currentTimeMillis())+".jpg"; String filePath = directoryPath + pictureFileName ; File directory = new File(directoryPath); if (!directory.exists()) { // in case there's no DCIM folder directory.mkdirs(); // just create it } d("takePhoto filePath: " + filePath); this.imageUri = Uri.parse(filePath); d("takePhoto imageUri: " + filePath); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // here's where I tell the intent where to save the file intent.putExtra( MediaStore.EXTRA_OUTPUT,Uri.fromFile( new File(filePath) ) ); startActivityForResult(intent, TAKE_PICTURE); } </code></pre> <p>I had to use two different methods for loading the picture to the imageview. If it's a picture just taken, I use this one:</p> <pre><code>public void loadImageJustTaken(Uri selectedImage) { mActivity.getContentResolver().notifyChange(selectedImage, null); Bitmap bitmap = BitmapFactory.decodeFile(imageUri.getPath()); ivPicture.setImageBitmap(bitmap); ivPicture.setVisibility(View.VISIBLE); } </code></pre> <p>But to use one from the gallery I have to use the contentResolver</p> <pre><code>public void loadImage(Uri selectedImage){ imageUri = selectedImage; mActivity.getContentResolver().notifyChange(selectedImage, null); ContentResolver cr = mActivity.getContentResolver(); Bitmap bitmap; try { bitmap = android.provider.MediaStore.Images.Media .getBitmap(cr, imageUri); ivPicture.setImageBitmap(bitmap); ivPicture.setVisibility(View.VISIBLE); mActivity.croutonInfo(imageUri.getPath()); } catch (Exception e) { e("Camera " + e.toString()); } } </code></pre> <p>When I want to upload the image, I have to encode it. This method works as long as you provide it with the right file path</p> <pre><code> private String encodeImgForHTTP (Uri imageUri) throws IOException{ String realPicPath = getPath(imageUri); d("encodeImgForHTTP before opening stream " + realPicPath); FileInputStream fis = new FileInputStream(realPicPath); d("encodeImgForHTTP after opening stream "); // Get binary bytes for encode byte[] imageBytes = IOUtils.toByteArray(fis); d("encodeImgForHTTP after getting byte array "); // base 64 encode for text transmission (HTTP) //String data_string = Base64.encodeToString(data, Base64.URL_SAFE); d("encodeImgForHTTP pre 64: " + imageBytes); String data_string = Base64.encodeToString(imageBytes, Base64.URL_SAFE); d("encodeImgForHTTP before returning the encoded data string " + data_string); return data_string; } </code></pre> <p>And here's how I get the "real path" for the picture:</p> <pre><code>public String getPath(Uri uri) throws IOException { String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = mActivity. managedQuery(uri, projection, null, null, null); if (cursor == null){ // with pictures just taken, the uri returned by the onActivityResult makes cursor to be null. Following method takes care of that uri = saveMediaEntry(imageUri.getPath(), pictureFileName, ""); d("cursor nulo, segundo cursor con uri " + uri.getPath()); cursor = mActivity. managedQuery(uri, projection, null, null, null); } int column_index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } </code></pre> <p>The method saveMediaEntry creates an entry to the device's media database returning its Uri. Using that Uri, the cursor now will point to the picture file we want</p> <pre><code> private Uri saveMediaEntry( String imagePath,String title,String description) throws IOException { ExifInterface exif = new ExifInterface(imagePath); ContentValues v = new ContentValues(); v.put(Images.Media.TITLE, title); v.put(Images.Media.DISPLAY_NAME, title); v.put(Images.Media.DESCRIPTION, description); v.put(Images.Media.DATE_ADDED, System.currentTimeMillis()); v.put(Images.Media.DATE_TAKEN, exif.getAttribute(ExifInterface.TAG_DATETIME)); //v.put(Images.Media.DATE_MODIFIED, dateTaken) ; v.put(Images.Media.MIME_TYPE, "image/jpeg"); v.put(Images.Media.ORIENTATION, exif.getAttribute(ExifInterface.TAG_ORIENTATION)); File f = new File(imagePath) ; File parent = f.getParentFile() ; String path = parent.toString().toLowerCase() ; String name = parent.getName().toLowerCase() ; v.put(Images.ImageColumns.BUCKET_ID, path.hashCode()); v.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, name); v.put(Images.Media.SIZE,f.length()) ; f = null ; v.put(Images.Media.LATITUDE, exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE)); v.put(Images.Media.LONGITUDE, exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE)); v.put("_data",imagePath) ; ContentResolver c = mActivity.getContentResolver() ; return c.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, v); } </code></pre> <p>After all this, pictures get loaded OK, and the Base64.encodeToString returns are different for different pictures :)</p> <p>Hope it helps someone :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload