External Sd-Card Stroage - android

I am new to android, I tried a lot codes, but can't get the path.
but using below code, I got the path of the External Sdcard, but could'nt access the External Sdcard directory.
public static List<StorageInfo> getStorageList() {
List<StorageInfo> list = new ArrayList<StorageInfo>();
String def_path = Environment.getExternalStorageDirectory().getPath();
boolean def_path_internal = !Environment.isExternalStorageRemovable();
String def_path_state = Environment.getExternalStorageState();
boolean def_path_available = def_path_state.equals(Environment.MEDIA_MOUNTED)
|| def_path_state.equals(Environment.MEDIA_MOUNTED_READ_ONLY);
boolean def_path_readonly = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY);
BufferedReader buf_reader = null;
try {
HashSet<String> paths = new HashSet<String>();
buf_reader = new BufferedReader(new FileReader("/proc/mounts"));
String line;
int cur_display_number = 1;
while ((line = buf_reader.readLine()) != null) { if (line.contains("vfat") || line.contains("/mnt")) {
StringTokenizer tokens = new StringTokenizer(line, " ");
String unused = tokens.nextToken(); //device
String mount_point = tokens.nextToken(); //mount point
if (paths.contains(mount_point)) {
continue;
}
unused = tokens.nextToken(); //file system
List<String> flags = Arrays.asList(tokens.nextToken().split(",")); //flags
boolean readonly = flags.contains("ro");
if (mount_point.equals(def_path)) {
paths.add(def_path);
list.add(0, new StorageInfo(def_path, def_path_internal, readonly, -1));
} else if (line.contains("/dev/block/vold")) {
if (!line.contains("/mnt/secure")
&& !line.contains("/mnt/asec")
&& !line.contains("/mnt/obb")
&& !line.contains("/dev/mapper")
&& !line.contains("tmpfs")) {
paths.add(mount_point);
list.add(new StorageInfo(mount_point, false, readonly, cur_display_number++));
}
}
}
}
if (!paths.contains(def_path) && def_path_available) {
list.add(0, new StorageInfo(def_path, def_path_internal, def_path_readonly, -1));
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (buf_reader != null) {
try {
buf_reader.close();
} catch (IOException ex) {} }
}
return list;
}
How can i access the Sd card directory.

From android 23+ you need to gain user permission to write to the phones external storage. Do this by adding the following to your manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
If this does not give you access to the SD card then you may need to ask for permission at runtime before trying to access the card. Here is an example:
public void checkPermissions() {
String[] tmp = {"WRITE_EXTERNAL_STORAGE"};
for (String s : tmp) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!hasPermissionInManifest(getBaseContext(), s)) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
}
}
}
You need permissions to access other sensors in the phone and camera which can be done in a similar way. You can get the path to the external storage like this:
Environment.getExternalStorageDirectory().toString();

Related

Android Toast did not show after open Gallery and pick image

I want to show Toast (or progress bar) when clicking on the button "btnSearchImg". If before upload image, button clicked say "first pick image from gallary" and if after upload image clicked say "waiting". the toast before uploading image work fine but after uploading didn't work fine! my entire Activity code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_by_image);
Toasty.Config.getInstance().setTextSize(15).apply();
mSharedPreferences = getSharedPreferences("PREFERENCE", Context.MODE_PRIVATE);
mEditor = mSharedPreferences.edit();
mEditor.putString(PREF_SKIP,null);
if(mSharedPreferences.contains(PREF_SKIP)){
Log.i("payment", "true");
} else {
try {
}catch (Exception e){
Toast.makeText(getApplicationContext(), "ERORR", Toast.LENGTH_LONG).show();
}
}
context = getApplicationContext();
pbImgRetrvialProccess = (ProgressBar)findViewById(R.id.pbImgRetrvialProccess);
tvPermissionLoadImg = (TextView)findViewById(R.id.tvPermissionLoadImg);
tvPermissionLoadImg.setTypeface(Base.getIranSansFont());
TextView tvSearchImageToolBarText = (TextView) findViewById(R.id.tvSearchImageToolBarText);
tvSearchImageToolBarText.setTypeface(Base.getIranSansFont());
ivGalleryImgLoad = (ImageView) findViewById(R.id.ivGalleryImgLoad);
btnSearchImgLoad = (Button) findViewById(R.id.btnSearchImgLoad);
btnSearchImg = (Button) findViewById(R.id.btnSearchImg);
btnSearchImgLoad.setOnClickListener(this);
btnSearchImg.setOnClickListener(this);
}
public StringBuilder uniformQuantization( File filePath ){...}
private StringBuilder chHistogram( Mat newImage ){...}
private void CopyAssets(String filename){...}
private void copyFile(InputStream in, OutputStream out) throws IOException {...}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSearchImgLoad:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PICK_IMAGE);}
OpenGallery();
break;
case R.id.btnSearchImg:
Toasty.success(getBaseContext(), "Waiting...", Toast.LENGTH_LONG).show();
FindSimilarRequestedImage();
break;
}
}
private void OpenGallery() {
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
private void FindSimilarRequestedImage() {
if (ivGalleryImgLoad.getDrawable() != null) {
File loadedImageFilePath = new File(getRealPathFromURI(selectedImageUri));
queryFeatureX = uniformQuantization(loadedImageFilePath);
dateiLesenStringBuilder();
} else {
Toasty.error(getBaseContext(), "first pick image from gallary", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE && data != null) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PICK_IMAGE);
} else {
selectedImageUri = data.getData();
ivGalleryImgLoad.setImageURI(selectedImageUri);
tvPermissionLoadImg.setVisibility(View.INVISIBLE);
}
}
}
private String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
result = contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
cursor.close();
}
return result;
}
private void dateiLesenStringBuilder() {
FEATURE_PATH = context.getCacheDir().getPath() + "/" + FEATURE_NAME;
try {
FileOutputStream out = new FileOutputStream(FEATURE_PATH);
InputStream in = context.getAssets().open("coins/"+FEATURE_NAME);
byte[] buffer = new byte[1024];
int ch;
while ((ch = in.read(buffer)) > 0){
out.write(buffer, 0, ch);
}
out.flush();
out.close();
in.close();
}catch (Exception e){
System.out.println(e);
}
final File featureList = new File(FEATURE_PATH);
Runnable runner = new Runnable() {
BufferedReader in = null;
#Override
public void run() {
try {
String sCurrentLine;
int lines = 0;
BufferedReader newbw = new BufferedReader(new FileReader(featureList.getAbsolutePath()));
while (newbw.readLine() != null)
lines++;
in = new BufferedReader(new FileReader(featureList.getAbsolutePath()));
ArrayList<Double> nDistVal = new ArrayList<Double>();
ArrayList<String> nImagePath = new ArrayList<String>();
int count = 0;
while ((sCurrentLine = in.readLine()) != null) {
String[] featX = sCurrentLine.split(";")[1].split(" ");
nImagePath.add(sCurrentLine.split(";")[0]);
nDistVal.add(Distance.distL2(featX, queryFeatureX.toString().split(" ")));
count++;
}
ArrayList<Double> nstore = new ArrayList<Double>(nDistVal); // may need to be new ArrayList(nfit)
double maxDistVal = Collections.max(nDistVal);
Collections.sort(nDistVal);
sortedNImagePath = new ArrayList<String>();
for (int n = 0; n < nDistVal.size(); n++) {
int index = nstore.indexOf(nDistVal.get(n));
sortedNImagePath.add(nImagePath.get(index));
sortedNImageDistanceValues.add(String.valueOf(nDistVal.get(n) / maxDistVal));
String filePath = sortedNImagePath.get(0);
String minDistanceImg = sortedNImageDistanceValues.get(n);
FileNameFromPath = filePath.substring(filePath.lastIndexOf("/") + 1);
System.out.println("Distance values -> " + FileNameFromPath.toString());
System.out.println("Distance values -> " + minDistanceImg.toString());
}
} catch (Exception ex) {
String x = ex.getMessage();
System.out.println("ERORR" + x);
}
if (in != null) try {
in.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
Intent imgSearchedCoinNameIntent = new Intent(SearchByImageActivity.this, ImageSearchedCoinActivity.class);
imgSearchedCoinNameIntent.putExtra("imgSearchedCoinName", FileNameFromPath);
startActivity(imgSearchedCoinNameIntent);
}
}
};
Thread t = new Thread(runner, "Code Executer");
t.start();
}
}
If I put the "Waiting..." Toast after the FindSimilarRequestedImage() the toast will show up, but I need the toast show immediately after clicking on the btnSearchImg.
NOTE: Also in the dateiLesenStringBuilder() I removed the thread t that this part of code runs in normal flow and serial, but nothing changes!
Instead of toast use logcat to see if they run sequentially or not, maybe toast takes time to run
I solved this problem by putting the part of code in delay like below:
private void FindSimilarRequestedImage() {
if (ivGalleryImgLoad.getDrawable() != null) {
pbImgRetrvialProccess.setVisibility(View.VISIBLE);
Toasty.success(getBaseContext(), "Waiting ...", Toast.LENGTH_LONG).show();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
File loadedImageFilePath = new File(getRealPathFromURI(selectedImageUri));
queryFeatureX = uniformQuantization(loadedImageFilePath);
dateiLesenStringBuilder();
}
}, 5000);
} else {
Toasty.error(getBaseContext(), "first pick image from gallary", Toast.LENGTH_LONG).show();
}
}
Now before destroying activity throw functions, I first show the toast, then run other functions!

Save android logcat in a file on Android device

I want to write Android logcat in a file on my device.
To do that, I used the following code
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if(isExternalStorageWritable()){
File appDirectory = new File(Environment.getExternalStorageDirectory()+ "/MyAppfolder");
File logDirectory = new File(appDirectory + "/log");
File logFile = new File(logDirectory, "logcat"+System.currentTimeMillis()+".txt");
if(!appDirectory.exists()){
appDirectory.mkdir();
}
if(!logDirectory.exists()){
logDirectory.mkdir();
}
if(!logFile.exists()){
try {
logFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
Process process = Runtime.getRuntime().exec("logcat -f "+logFile);
}
}
}
catch (IOException e){
e.printStackTrace();
}
}
else if (isExternalStorageReadable()){
Log.i(TAG, "ONLY READABLE");
}
else{
Log.i(TAG, "NOT ACCESSIBLE");
}}
public boolean isExternalStorageReadable(){
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
return true;
}
return false;
}
public boolean isExternalStorageWritable(){
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)){
return true;
}
return false;
}
And I added the permissions in AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_LOGS"/>
The folders and the file were created, but the file is always empty.
How can I improve the code so the logcat will be written in the file.
if(checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
else{
Process process = Runtime.getRuntime().exec("logcat -f "+logFile);
}
Your code is working perfectly may be, by mistake, You had written ask for permission & make log file both together
Maybe you are not writing the log to the file. You are missing this method.
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if ( Environment.MEDIA_MOUNTED.equals( state ) ) {
return true;
}
return false;
}
Your took code from the solution of this question Stack overflow question. Check it out.
It's my pleasure to answer your question.
In my project,I use this to solve the problem.
1.add this class
public class LogcatHelper {
private static LogcatHelper INSTANCE = null;
private static String PATH_LOGCAT;
private LogDumper mLogDumper = null;
private int mPId;
/**
* init data
*/
public void init(Context context) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {// sd first
PATH_LOGCAT = Environment.getExternalStorageDirectory()
.getAbsolutePath() + File.separator + "logcat";
} else {
PATH_LOGCAT = context.getFilesDir().getAbsolutePath()
+ File.separator + "logcat";
}
File file = new File(PATH_LOGCAT);
if (!file.exists()) {
file.mkdirs();
}
}
public static LogcatHelper getInstance(Context context) {
if (INSTANCE == null) {
INSTANCE = new LogcatHelper(context);
}
return INSTANCE;
}
private LogcatHelper(Context context) {
init(context);
mPId = android.os.Process.myPid();
}
public void start() {
if (mLogDumper == null)
mLogDumper = new LogDumper(String.valueOf(mPId), PATH_LOGCAT);
mLogDumper.start();
}
public void stop() {
if (mLogDumper != null) {
mLogDumper.stopLogs();
mLogDumper = null;
}
}
private class LogDumper extends Thread {
private Process logcatProc;
private BufferedReader mReader = null;
private boolean mRunning = true;
String cmds = null;
private String mPID;
private FileOutputStream out = null;
public LogDumper(String pid, String dir) {
mPID = pid;
try {
out = new FileOutputStream(new File(dir, "logcat"
+ getFileName() + ".log"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
/**
*
* Level:*:v , *:d , *:w , *:e , *:f , *:s
*
*
* */
// cmds = "logcat *:e *:w | grep \"(" + mPID + ")\""; // print e level and ilevel info
// cmds = "logcat | grep \"(" + mPID + ")\"";// print all
// cmds = "logcat -s way";// print filter info
cmds = "logcat *:e *:i | grep \"(" + mPID + ")\"";
}
public void stopLogs() {
mRunning = false;
}
#Override
public void run() {
try {
logcatProc = Runtime.getRuntime().exec(cmds);
mReader = new BufferedReader(new InputStreamReader(
logcatProc.getInputStream()), 1024);
String line = null;
while (mRunning && (line = mReader.readLine()) != null) {
if (!mRunning) {
break;
}
if (line.length() == 0) {
continue;
}
if (out != null && line.contains(mPID)) {
out.write((getDateEN() + " " + line + "\n")
.getBytes());
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (logcatProc != null) {
logcatProc.destroy();
logcatProc = null;
}
if (mReader != null) {
try {
mReader.close();
mReader = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
out = null;
}
}
}
}
public static String getFileName() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String date = format.format(new Date(System.currentTimeMillis()));
return date;
}
public static String getDateEN() {
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date1 = format1.format(new Date(System.currentTimeMillis()));
return date1;
}
}
add code in the Application class
LogcatHelper.getInstance((getApplicationContext())).start();
3.add permissions in the Application class
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
Process process = Runtime.getRuntime().exec("logcat -f "+logFile);
}
}
I hope I can help you.

How to display duration using Google Maps?

I am trying to display to the user just the duration between two locations using latitude and longitude. I am having trouble with permissions which I am not sure how to handle it.
Here is my doInBackground code:
protected String[] doInBackground(String... params) {
try {
LocationManager locationManager = (LocationManager) MyCustomHomeActivity.this
.getSystemService(LOCATION_SERVICE);
// getting GPS status
boolean isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
boolean isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
String shuLat = "41.2207188";
String shuLong = "-73.24168179999999";
String forecastJsonStr = null;
String myUrlSetup = "https://maps.googleapis.com/maps/api/directions/json?origin="+latitude + "," + longitude +"&destination="+shuLat +"," + shuLong + "&departure_time=now&traffic_model=best_guess&key=AIzaSyB6l8vrnspw2-1Q_cnzO03JlAsIOMl-7bs";
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
try {
URL url;
url = new URL(myUrlSetup);
// Create the request to GoogleMapAPI, and open the connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
System.out.println("I am in doInBackground step3");
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
forecastJsonStr = buffer.toString();
Log.v(LOG_TAG, "Forecast string: " + forecastJsonStr);
} catch (IOException e) {
Log.e(LOG_TAG, "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attemping
// to parse it.
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e(LOG_TAG, "Error closing stream", e);
}
}
}
try {
System.out.println("I am just in front of calling getDurationDataFromJson");
return getDurationDataFromJson(forecastJsonStr);
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
return new String[0];
}//end doInBackground
Here is my getDurationDataFromJSON code:
private String[] getDurationDataFromJson(String forecastJsonStr)
throws JSONException {
// These are the names of the JSON objects that need to be extracted.
final String OWM_ROUTES = "routes";
final String OWM_LEGS = "legs";
final String OWM_DURATION = "duration";
final String OWM_TEXT = "text";
String[] resultStrs = new String[0];
String duration;
JSONObject durationJson = new JSONObject(forecastJsonStr);
JSONArray routeArray = durationJson.getJSONArray(OWM_ROUTES);
JSONArray legArray = routeArray.getJSONObject(0).getJSONArray(OWM_LEGS);
//Duration
JSONObject durationObj = legArray.getJSONObject(0).getJSONObject(OWM_DURATION);
duration = durationObj.getString(OWM_TEXT);
resultStrs[0] = duration;
System.out.println("Duration is: " + duration);
for (String s : resultStrs) {
System.out.println("Duration entry: " + s);
Log.v(LOG_TAG, "Duration entry: " + s);
}
return resultStrs;
}
Trouble I am facing is the following part in doInBackGround code:
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Error is:
Call requires permission which may be rejected by user: code should explicitly check to see if permission is available(with `check permission`) or explicitly handle a potential `SecurityException`.
I am not sure if I am going in the right direction. Please guide me through this.
With Android API level (23), permissions are requested at running time, here you have de official documentation:
https://developer.android.com/training/permissions/requesting.html
Basically, you need to check if the device Android API level is >= 23, and if so, request the required permissions. Try something like this:
if ( Build.VERSION.SDK_INT >= 23)
//Ask for needed permissions following the docs mentioned above
}
I hope this helps!
I will suggest you to use FusedLocationProviderApi
to get last known location.
But before doing anything with location you need make sure that you have provided
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
in your manifest.xml.
Now I came to your point. From API Level-23 android indrouces explicit permission seeking feature. To provide this you need to follow some steps.
I hereby attach some code blocks for your help-
Checker of required permission states and device location service
public boolean isLocationEnabled() {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
} else {
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}
public boolean hasCoarseLocationPermission() {
return ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
public boolean hasFineLocationPermission() {
return ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
Now launch your activity/fragment after checking and providing expicit permission
private void initiateYourActivityCallAfterCheckingPermission() {
if (hasCoarseLocationPermission() && hasFineLocationPermission()) {
if (locationUtil.isLocationEnabled()) {
startYourLocationActivity();
}
} else if (!hasFineLocationPermission() && hasCoarseLocationPermission()) {
requestBothLocationPermission();
} else if (hasFineLocationPermission() && hasCoarseLocationPermission()) {
requestFineLocationPermission();
} else if (hasFineLocationPermission() && !hasCoarseLocationPermission()) {
requestCoarseLocationPermission();
}
}
private void requestBothLocationPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(SplashActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)
&& ActivityCompat.shouldShowRequestPermissionRationale(SplashActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)) {
} else {
ActivityCompat.requestPermissions(SplashActivity.this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_BOTH_LOCATION);
}
}
private void requestCoarseLocationPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(SplashActivity.this,
Manifest.permission.ACCESS_COARSE_LOCATION)) {
} else {
ActivityCompat.requestPermissions(SplashActivity.this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_COARSE_LOCATION);
}
}
private void requestFineLocationPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(SplashActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
} else {
ActivityCompat.requestPermissions(SplashActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_FINE_LOCATION);
}
}
/**
* to process permission result
*/
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case General.REQUEST_BOTH_LOCATION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
startYourLocationActivity();
}
break;
case General.REQUEST_COARSE_LOCATION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startYourLocationActivity();
}
break;
case General.REQUEST_FINE_LOCATION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startYourLocationActivity();
}
break;
}
}
Hope this will help.

Download to external storage using downloadManager doesn't work properly on Galaxy S7

Downloading to internal storage works.
However, when I try to download to the external sdcard, then status doesn't update until after 2~3 minutes. (Meaning I get 0 bytes downloaded from cursor.getLong(cursor
.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));).
It eventually downloads after 2~3 minutes.
The notification status also says 0%
private void addToDownloadManager(String sourcePath, String destFolder, String deskFileName, DownloadManager downloadManager) {
try {
if(sourcePath == null || sourcePath.equals(""))
return;
try {
File folder = new File(destFolder);
if (!folder.exists()) {
folder.mkdirs();
}
}catch (Exception e) {
}
Uri Download_Uri = Uri.parse(sourcePath);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.setAllowedOverRoaming(false);
request.setTitle("title");
request.setDescription("description");
File destination = new File(destFolder, deskFileName);
request.setDestinationUri(Uri.fromFile(destination));
downloadReference = downloadManager.enqueue(request);
} catch (Exception e) {
}
}
private Timer mDownloadStatusTimer;
public void downloadStatusTimerSchedule() {
if (mDownloadStatusTimer != null)
downloadStatusTimerCancel();
try {
mDownloadStatusTimer = new Timer("DownloadStatusTimer");
DownloadStatusTimer timer = new DownloadStatusTimer();
mDownloadStatusTimer.schedule(timer, 500, 500); // 0.5 second
} catch (Exception e) {
}
}
public void downloadStatusTimerCancel() {
if (mDownloadStatusTimer != null) {
mDownloadStatusTimer.cancel();
mDownloadStatusTimer.purge();
mDownloadStatusTimer = null;
}
}
private long bytes_downloaded = 0;
private long bytes_total = 0;
public class DownloadStatusTimer extends TimerTask {
#Override
public void run() {
if (mDownloadManager != null) {
DownloadManager.Query myDownloadQuery = new DownloadManager.Query();
Cursor cursor = mDownloadManager.query(myDownloadQuery);
bytes_downloaded = 0;
bytes_total = 0;
try {
if (cursor != null && cursor.moveToFirst()) {
try {
// Get downloaded size/total size
if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL ||
cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_FAILED) {
// do nothing
} else {
bytes_downloaded += cursor.getLong(cursor
.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
bytes_total += cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
}
} catch (Exception e) {
}
while (cursor != null && cursor.moveToNext()) {
try {
// Get downloaded size/total size
if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL ||
cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_FAILED) {
// do nothing
} else {
bytes_downloaded += cursor.getLong(cursor
.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
bytes_total += cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
}
} catch (Exception e) {
}
}
} else {
}
} catch (Exception e) {
} finally {
if (cursor != null) {
cursor.close();
}
}
Log.e("test", "Download size: " + bytes_downloaded + " / " + bytes_total);
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
try {
tv_download_status.setText("Download size: " + bytes_downloaded + " / " + bytes_total);
} catch (Exception e) {
}
}
});
}
}
}
I tested Note 4 and Galaxy S5 and they seems to be fine.
Could it be Android 6.0 thing? or S7?
Is it a bug? Or is there anything I am doing wrong here?
If you want to test it yourself, here's the project with source code:
https://drive.google.com/open?id=0BygTefPD845LTkp5QU1mOHRkMDQ
Here's APK to test:
https://drive.google.com/open?id=0By...Ec1ZHk5ZWRkWWc
[Edit]
My destination location is: /storage/806E-1A11/Android/data/com.joshua.externalsddownloadtest/files/download/video.mp4
Source is: http://downloads.4ksamples.com/downloads/[2160p]%204K-HD.Club-2013-Taipei%20101%20Fireworks%20Trailer%20(4ksamples.com).mp4
(I got the URL from the Mp4 Sample website)
File[] files = ContextCompat.getExternalFilesDirs(MainActivity.this, null);
destFolder = null;
for(int i=0; i< files.length; i++) {
if (!files[i].getAbsolutePath().equals(MainActivity.this.getExternalFilesDir(null).getPath())) {
destFolder = files[i].getAbsolutePath();
break;
}
}
boolean bDownloadToExternal = false;
if(destFolder == null) {
tv_download_destination.setText("No external storage found");
} else {
destFolder += "/download/";
tv_download_destination.setText("Destination location: " + destFolder + fileName);
bDownloadToExternal = true;
}
I think its about new permission system at android 6.
You should ask permissions in runtime like:
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != getPackageManager().PERMISSION_GRANTED){
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, YOUR_CONST_REQUEST_CODE);
}
}
And implement listener
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case YOUR_CONST_REQUEST_CODE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// invoke your download method
} else {
// if permissions was not granted
}
}
}
}
Other way you can set targetSdkVersion less then 23 in gradle file.
More about new permission system you can read here Android.com

How to set on button backup/restore functionality in sqlite database ?

I want to set on button click events backup my applications data on SD-Card.
This is my code :
shv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(SecondActivity.this, "backup Button press",Toast.LENGTH_LONG).show();
boolean rc = MyDatabaseTools.backup();
System.out.println(rc);
if(rc==true)
{
Toast.makeText(SecondActivity.this, "backup Successfully",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(SecondActivity.this, "backup error",Toast.LENGTH_LONG).show();
}
}
});
I proceed like this :
public class MyDatabaseTools {
private String appName = "";
private String packageName = "";
public static boolean backup() {
boolean rc = false;
boolean writeable = isSDCardWriteable();
if (writeable) {
File file = new File(Environment.getDataDirectory() + "/data/<com.example.judgedetail>/databases/ado.db");
File fileBackupDir = new File(Environment.getExternalStorageDirectory(), "ADOBOOK" + "/backup");
if (!fileBackupDir.exists()) {
fileBackupDir.mkdirs();
}
if (file.exists()) {
File fileBackup = new File(fileBackupDir, "ado.db");
try {
fileBackup.createNewFile();
/*Files.copyFile(file, fileBackup);*/ //got error here dis line not work
rc = true;
} catch (IOException ioException) {
//
} catch (Exception exception) {
//
}
}
}
return rc;
}
private static boolean isSDCardWriteable() {
boolean rc = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
rc = true;
}
return rc;
}
public MyDatabaseTools(final Context context, final String appName) {
this.appName = appName;
packageName = context.getPackageName();
}
}
Files.copyFile(file, fileBackup);
This line got error.
How shall I solve this ?
Please help.
Try this:
public void writeToSD() throws IOException {
File f=new File("/data/data/com.YourPackageName/databases/DBName");
FileInputStream fis=null;
FileOutputStream fos=null;
try{
fis=new FileInputStream(f);
fos=new FileOutputStream("/mnt/sdcard/dump.db");
while(true){
int i=fis.read();
if(i!=-1){
fos.write(i);
}
else{
break;
}
}
fos.flush();
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
fos.close();
fis.close();
}
catch(IOException ioe){
System.out.println(ioe);
}
}
}
I don't know where you copied that code from, but either you or the poster missed something. Files.copyFile() is no existing Android API function, and I guess that's what you describe as the line being not working.
You'll have to copy your file using another method, I'd suggest one like this:
https://stackoverflow.com/a/9293885/1691231

Categories

Resources