Edit: I don't know how to convert an image file (example.jpg) to byteArray I am trying to understand how it is done but I got nothing so far, I have tried different methods found in this site but nothing seems to work for me. I have resulted in asking in here so I might be able to understand how it's done with the help of others
I have an image file in my sdcard and I know my path, now I want to take that image and convert it to byteArray, I am getting wrong results in my logcat or so I think I do. I am not sure how the output should be
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.awareness.snapshot.internal.Snapshot;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.GoogleMap.SnapshotReadyCallback;
import com.google.android.gms.maps.GoogleMap.OnMapLoadedCallback;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.Map;
import static android.R.attr.bitmap;
import static android.R.attr.duration;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
public byte[] B;
private GoogleMap mMap;
GPSTracker gps;
public File imageFile ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
Create the map when the map is ready OnMapReady, The object GPS is ccalled (GPSTraccker) to obtain
Values for lat and long.
*/
final Handler handler = new Handler();
@Override
public void onMapReady(final GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
gps = new GPSTracker(MapsActivity.this);
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
LatLng sydney = new LatLng(latitude, longitude);
//adding an automated marker to mark the users spot on the map.
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Cyprus"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mMap.animateCamera(CameraUpdateFactory.zoomTo(19.0f));
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
//When the map is clicked the following will occure
@Override
public void onMapClick(LatLng point) {
//create a new marker on the new lat/long of the spot clicked
MarkerOptions marker = new MarkerOptions().position(
new LatLng(point.latitude, point.longitude)).title("New Marker");
//places the marker
mMap.addMarker(marker);
//create an object of Bitmap
Bitmap bitmap;
//Creating and calling object Snapshot
SnapshotReadyCallback callback = new SnapshotReadyCallback() {
@Override
//when the snapshot is ready do the following
public void onSnapshotReady(Bitmap bitmap) {
//Creating object Date and storing it to now
Date now = new Date();
//Retribing exact date and storing it to now
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
//dissabling strick mode for testing , remember to remvoe this code
StrictMode.ThreadPolicy old = StrictMode.getThreadPolicy();
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder(old)
.permitDiskWrites()
.build());
//mpath = /sd/date.jpg creating a dir for the image to be snaped
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
//Calling object File.
File imageFile = new File(mPath);
FileOutputStream out = new FileOutputStream(imageFile);
//outputing streaming to /imagefile
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
Log.d("Var:OUT: ", " " + out);
Log.d("ImageFile" ,""+ imageFile) ;
Log.d("mPath" ,""+ mPath) ;
//decompose image
Bitmap bmp = BitmapFactory.decodeFile(mPath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
StrictMode.setThreadPolicy(old);
ByteArrayOutputStream streams = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, streams);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image=stream.toByteArray();
System.out.println("byte array:"+image);
Log.d("array", image.toString());
String img_str = Base64.encodeToString(image, 0);
Log.d("array_Str",img_str);
System.out.println("string:"+img_str);
} catch (Exception e) {
e.printStackTrace();
}
}
};
mMap.snapshot(callback);
System.out.println(point.latitude + "---" + point.longitude);
}
});
}
// public byte[] getBytesFromBitmap(Bitmap bitmap) {
// ByteArrayOutputStream stream = new ByteArrayOutputStream();
// bitmap.compress(Bitmap.CompressFormat.JPEG, 90, now);
//// Log.d("ARRAY", "" + now.toByteArray());
// byte[] B = stream.toByteArray();
// return stream.toByteArray();
//}
public void onSnapshotReady (Bitmap bitmap) {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
Log.d("DOulefko","test");
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
}
private void openScreenshot(File imageFile) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(imageFile);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
}
}
My logcat (not including the entire base64)
11-15 11:46:25.565 19702-19702/com.example.apostolis.gpsapi15 I/art: Late-enabling -Xcheck:jni 11-15 11:46:25.597 19702-19702/com.example.apostolis.gpsapi15 W/System: ClassLoader referenced unknown path: /data/app/com.example.apostolis.gpsapi15-1/lib/arm64 11-15 11:46:25.600 19702-19702/com.example.apostolis.gpsapi15 I/MultiDex: VM with version 2.1.0 has multidex support 11-15 11:46:25.600 19702-19702/com.example.apostolis.gpsapi15 I/MultiDex: install 11-15 11:46:25.600 19702-19702/com.example.apostolis.gpsapi15 I/MultiDex: VM has multidex support, MultiDex support library is disabled. 11-15 11:46:25.603 19702-19702/com.example.apostolis.gpsapi15 I/FirebaseInitProvider: FirebaseApp initialization unsuccessful 11-15 11:46:25.616 19702-19702/com.example.apostolis.gpsapi15 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 11-15 11:46:25.622 19702-19702/com.example.apostolis.gpsapi15 D/PhoneWindow: state.preset = false, color: 0, translucent = 0 11-15 11:46:25.622 19702-19702/com.example.apostolis.gpsapi15 D/PhoneWindow: state.preset = false, color: 0, translucent = 0 11-15 11:46:25.622 19702-19702/com.example.apostolis.gpsapi15 D/PhoneWindow: state.preset = false, color: 0, translucent = 0 11-15 11:46:25.624 19702-19702/com.example.apostolis.gpsapi15 D/PhoneWindow: state.preset = false, color: -16777216, translucent = 0 11-15 11:46:25.629 19702-19702/com.example.apostolis.gpsapi15 V/BoostFramework: mAcquireFunc method = public int com.qualcomm.qti.Performance.perfLockAcquire(int,int[]) 11-15 11:46:25.630 19702-19702/com.example.apostolis.gpsapi15 V/BoostFramework: mReleaseFunc method = public int com.qualcomm.qti.Performance.perfLockRelease() 11-15 11:46:25.630 19702-19702/com.example.apostolis.gpsapi15 V/BoostFramework: mAcquireTouchFunc method = public int com.qualcomm.qti.Performance.perfLockAcquireTouch(android.view.MotionEvent,android.util.DisplayMetrics,int,int[]) 11-15 11:46:25.630 19702-19702/com.example.apostolis.gpsapi15 V/BoostFramework: mIOPStart method = public int com.qualcomm.qti.Performance.perfIOPrefetchStart(int,java.lang.String) 11-15 11:46:25.630 19702-19702/com.example.apostolis.gpsapi15 V/BoostFramework: mIOPStop method = public int com.qualcomm.qti.Performance.perfIOPrefetchStop() 11-15 11:46:25.632 19702-19702/com.example.apostolis.gpsapi15 V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@5ec1d49 11-15 11:46:25.632 19702-19702/com.example.apostolis.gpsapi15 V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@5eae64e 11-15 11:46:25.655 19702-19702/com.example.apostolis.gpsapi15 D/AppTracker: App Event: start 11-15 11:46:25.662 19702-19717/com.example.apostolis.gpsapi15 D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 11-15 11:46:25.667 19702-19702/com.example.apostolis.gpsapi15 D/AppTracker: App Event: stop 11-15 11:46:25.684 19702-19702/com.example.apostolis.gpsapi15 D/PhoneWindow: state.preset = true, color: -16777216, translucent = 0 11-15 11:46:25.708 19702-19717/com.example.apostolis.gpsapi15 I/Adreno: QUALCOMM build
: 19f8f74, Iac38d51750 Build Date : 06/06/16 OpenGL ES Shader Compiler Version: XE031.07.00.01 Local Branch : case2489916_Unmap_crash Remote Branch : Remote Branch : Reconstruct Branch : 11-15 11:46:25.712 19702-19717/com.example.apostolis.gpsapi15 I/OpenGLRenderer: Initialized EGL, version 1.4 11-15 11:46:25.721 19702-19702/com.example.apostolis.gpsapi15 D/AbstractTracker: Event success 11-15 11:46:25.725 19702-19702/com.example.apostolis.gpsapi15 D/AbstractTracker: Event success 11-15 11:46:25.740 19702-19717/com.example.apostolis.gpsapi15 E/HAL: load: gralloc.msm8996.so 11-15 11:46:37.663 19702-19702/com.example.apostolis.gpsapi15 D/AppTracker: App Event: start 11-15 11:46:37.705 19702-19702/com.example.apostolis.gpsapi15 D/AbstractTracker: Event success 11-15 11:46:38.715 19702-19702/com.example.apostolis.gpsapi15 D/LocationManager: requestLocationUpdates listener=com.example.apostolis.gpsapi15.GPSTracker@f1972c8 packageName= com.example.apostolis.gpsapi15 11-15 11:46:38.718 19702-19702/com.example.apostolis.gpsapi15 D/Network: Network 11-15 11:46:38.735 19702-19702/com.example.apostolis.gpsapi15 D/AppTracker: App Event: stop 11-15 11:46:38.749 19702-19702/com.example.apostolis.gpsapi15 D/PhoneWindow: state.preset = false, color: 0, translucent = 0 11-15 11:46:38.749 19702-19702/com.example.apostolis.gpsapi15 D/PhoneWindow: state.preset = false, color: 0, translucent = 0 11-15 11:46:38.749 19702-19702/com.example.apostolis.gpsapi15 D/PhoneWindow: state.preset = false, color: 0, translucent = 0 11-15 11:46:38.750 19702-19702/com.example.apostolis.gpsapi15 D/PhoneWindow: state.preset = false, color: -16777216, translucent = 0 11-15 11:46:38.757 19702-19702/com.example.apostolis.gpsapi15 I/zzai: Making Creator dynamically 11-15 11:46:38.797 19702-19702/com.example.apostolis.gpsapi15 W/System: ClassLoader referenced unknown path: /data/user/0/com.google.android.gms/app_chimera/m/0000000e/n/arm64-v8a 11-15 11:46:38.818 19702-19702/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 11-15 11:46:38.822 19702-19702/com.example.apostolis.gpsapi15 I/Google Maps Android API: Google Play services client version: 9877000 11-15 11:46:38.826 19702-19702/com.example.apostolis.gpsapi15 I/Google Maps Android API: Google Play services package version: 10084448 11-15 11:46:38.832 19702-19702/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 11-15 11:46:38.846 19702-19702/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 11-15 11:46:38.857 19702-19702/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 11-15 11:46:38.880 19702-19702/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 11-15 11:46:38.891 19702-19702/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 11-15 11:46:38.911 19702-19702/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 11-15 11:46:38.925 19702-19702/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 11-15 11:46:38.939 19702-19702/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 11-15 11:46:38.939 19702-19702/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskWriteViolation 11-15 11:46:38.960 19702-19702/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 11-15 11:46:38.989 19702-19702/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 11-15 11:46:38.989 19702-19702/com.example.apostolis.gpsapi15 I/c: Token loaded from file. Expires in: 430122644 ms. 11-15 11:46:38.989 19702-19702/com.example.apostolis.gpsapi15 I/c: Scheduling next attempt in 429822 seconds. 11-15 11:46:39.018 19702-19702/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskWriteViolation 11-15 11:46:39.019 19702-19702/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskWriteViolation 11-15 11:46:39.032 19702-19924/com.example.apostolis.gpsapi15 I/DpmTcmClient: RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor 11-15 11:46:39.073 19702-19954/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 11-15 11:46:39.074 19702-19702/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 11-15 11:46:39.075 19702-19702/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskWriteViolation 11-15 11:46:39.083 19702-19702/com.example.apostolis.gpsapi15 D/AppTracker: App Event: start 11-15 11:46:39.090 19702-19702/com.example.apostolis.gpsapi15 D/PhoneWindow: state.preset = true, color: -16777216, translucent = 0 11-15 11:46:39.122 19702-19702/com.example.apostolis.gpsapi15 D/AbstractTracker: Event success 11-15 11:46:39.137 19702-19702/com.example.apostolis.gpsapi15 D/LocationManager: requestLocationUpdates listener=com.example.apostolis.gpsapi15.GPSTracker@5f141d packageName= com.example.apostolis.gpsapi15 11-15 11:46:39.140 19702-19702/com.example.apostolis.gpsapi15 D/Network: Network 11-15 11:46:39.182 19702-19702/com.example.apostolis.gpsapi15 D/AbstractTracker: Event success 11-15 11:46:39.195 19702-19717/com.example.apostolis.gpsapi15 D/OpenGLRenderer: endAllStagingAnimators on 0x7fa01fdc00 (RippleDrawable) with handle 0x7f9b7feb80 11-15 11:46:39.942 19702-19973/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 11-15 11:46:40.120 19702-19702/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 11-15 11:46:40.121 19702-19702/com.example.apostolis.gpsapi15 W/f: Suppressed StrictMode policy violation: StrictModeDiskWriteViolation 11-15 11:46:41.098 19702-19956/com.example.apostolis.gpsapi15 W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found. 11-15 11:46:41.115 19702-19956/com.example.apostolis.gpsapi15 I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:2 11-15 11:46:41.115 19702-19956/com.example.apostolis.gpsapi15 I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 2 11-15 11:46:41.117 19702-19956/com.example.apostolis.gpsapi15 E/DynamiteModule: Failed to load DynamiteLoader: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.dynamite.DynamiteModule$DynamiteLoaderClassLoader" on path: DexPathList[[zip file "/data/app/com.example.apostolis.gpsapi15-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.apostolis.gpsapi15-1/lib/arm64, /vendor/lib64, /system/lib64]] 11-15 11:46:41.117 19702-19956/com.example.apostolis.gpsapi15 W/DynamiteModule: Failed to load remote module: Failed to get module context 11-15 11:46:41.118 19702-19956/com.example.apostolis.gpsapi15 W/DynamiteModule: Failed to load module via fast routetn: Remote load failed. No local fallback found. 11-15 11:46:41.119 19702-19956/com.example.apostolis.gpsapi15 W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found. 11-15 11:46:41.124 19702-19956/com.example.apostolis.gpsapi15 I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:2 11-15 11:46:41.125 19702-19956/com.example.apostolis.gpsapi15 I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version = 2 11-15 11:46:49.625 19702-19702/com.example.apostolis.gpsapi15 I/System.out: 34.69429310156397---33.01508009433746 11-15 11:46:50.316 19702-19702/com.example.apostolis.gpsapi15 D/Var:OUT:: java.io.FileOutputStream@c4a6090 11-15 11:46:50.316 19702-19702/com.example.apostolis.gpsapi15 D/ImageFile: /storage/emulated/0/Tue Nov 15 11:46:50 GMT+02:00 2016.jpg 11-15 11:46:50.317 19702-19702/com.example.apostolis.gpsapi15 D/mPath: /storage/emulated/0/Tue Nov 15 11:46:50 GMT+02:00 2016.jpg 11-15 11:46:50.771 19702-19702/com.example.apostolis.gpsapi15 I/System.out: byte array:[B@4772d89 11-15 11:46:50.772 19702-19702/com.example.apostolis.gpsapi15 D/array: [B@4772d89 11-15 11:46:50.776 19702-19702/com.example.apostolis.gpsapi15 D/array_Str: iVBORw0KGgoAAAANSUhEUgAABDgAAAc4CAYAAADd+HYSAAAABHNCSVQICAgIfAhkiAAAIABJREFU eJzs3