小编MSe*_*iz5的帖子

NullPointerException - 尝试获取null数组的长度(readDirectory())

我有这里显示的代码.我的问题是files.length上的NullPointerException

for(int i=0; i < files.length; i++){
Run Code Online (Sandbox Code Playgroud)

这是因为我有一个"失败的readDirectory()errno = 13"at

File[] files = f.listFiles();
Run Code Online (Sandbox Code Playgroud)

但是,当路径良好时,为什么我的readDirectory会失败?

package com.example.androidexplorer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Environment;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity {

    private List<String> item = null;
    private List<String> path = null;
    private String root;
    private TextView myPath;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myPath = (TextView)findViewById(R.id.path);

        root = Environment.getExternalStorageDirectory().getPath();
        getDir(root);
    }

    private …
Run Code Online (Sandbox Code Playgroud)

directory android nullpointerexception

11
推荐指数
2
解决办法
4万
查看次数

如何通过php/android优化从mysql获取数据?

介绍

我有一个应用程序,并希望强制用户更新它,如果新版本在谷歌游戏市场.使用以下代码可以正常工作,但不是很稳定.我是通过AsyncTask进行的,并从php文件中获取数据.但它让我觉得必须有一种更好的方式或方式使它更稳定.

如何为程序设置超时,例如由于用户的互联网连接非常低?

get_newest_apk.php

<?PHP

    /*  GET APK VERSION FROM ANDROID DEVICE
        Example: [PATH]/get_newest_apk.php?apkversion=6
    */
    if($_GET["apkversion"]){
        $apkversion= $_GET["apkversion"];

        //MYSQL LOGIN PARAMETERS
        $host = '*****';
        $user = '*****';
        $pass = '*****';
        $db =   '*****';

        $mysqli = new mysqli($host, $user, $pass, $db);

            $result = $mysqli->query("SELECT MAX(VERSION) FROM TBL_APK");
            $row = $result->fetch_row();
            $count = $row[0];

            if($count > $apkversion){
                //Newer APK is avaiable
                echo "1";
            }else{
                //There is no never APK avaiable
                echo "2";
            }

    }else{
        //Error by GETTING apkversion from Android …
Run Code Online (Sandbox Code Playgroud)

php mysql stringbuilder android httpurlconnection

7
推荐指数
1
解决办法
333
查看次数

如何从可移动SD卡(第二外部存储)中删除文件?

我试图从我的SD卡(可移动)中删除android 9上的文件(PATH_TO_SD_CARD / DCIM / 1.PDF)。

1.我得到了第二个是我的SD卡的volumePaths。

private static String[] getVolumePaths(Context context) {
    String[] volumes = null;
    StorageManager managerStorage = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
    if (managerStorage == null) {
        return volumes;
    }
    try {
        return (String[]) managerStorage.getClass().getMethod("getVolumePaths", new Class[0]).invoke(managerStorage, new Object[0]);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
        return volumes;
    } catch (IllegalArgumentException e2) {
        e2.printStackTrace();
        return volumes;
    } catch (IllegalAccessException e3) {
        e3.printStackTrace();
        return volumes;
    } catch (InvocationTargetException e4) {
        e4.printStackTrace();
        return volumes;
    }
}

    String[] volumePaths = getVolumePaths(getContext());
    Log.v("VOLUME1: ", "-> …
Run Code Online (Sandbox Code Playgroud)

android delete-file

7
推荐指数
1
解决办法
152
查看次数

无障碍服务无法通过通知工作

我正在玩辅助功能服务。我知道如何从通知中读取数据,也知道如何将输入的文本打印到 logcat 中。

问题 当我收到朋友发来的 WhatsApp 消息时,我会通过通知打开该消息。(见下图)之后我可以在聊天中输入任何文本,但在 logcat 中我没有得到输出。当我正常打开 whatsapp 应用程序时,我在聊天中输入的所有内容都可以在 logcat 中看到。

当我通过通知打开消息时,为什么没有记录数据?

在此处输入图片说明

服务

public class MyAccessibilityService2 extends AccessibilityService {

    protected void onServiceConnected() {
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED | AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
        info.notificationTimeout = 100;
        setServiceInfo(info);
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
            Log.d("Notification","Recieved event");
            Parcelable data = event.getParcelableData();
            if (data instanceof Notification) {
                Log.d("Notification","Recieved notification");
                Notification notification = (Notification) data;
                Log.d("Notification","ticker: " + notification.tickerText);
                Log.d("Notification","icon: " …
Run Code Online (Sandbox Code Playgroud)

notifications android logcat accessibilityservice

6
推荐指数
1
解决办法
408
查看次数

Android,SQLite:在while子句中插入数据非常慢

在问题的底部解决问题

我有一个FTP服务器,上面有超过3,000张图片,我将图片名称保存到ArrayList.在此之后,我想将它们从arraylist保存到数据库,这需要非常非常非常的时间!看到:

        FTPFile[] files = ftpClient.listFiles();

        for (FTPFile file : files) {
            imageNames.add(file.getName());
        }
Run Code Online (Sandbox Code Playgroud)

我得到所有这些文件的列表并将它们保存到ArrayList imageNames中.这非常快,不需要超过1 - 最多2秒.

现在我想把这个数组向后写到我的sqlite数据库.当我这样做时,需要花费很长时间才能完成!我等了2分多钟......

        int i = imageNames.size() - 1;
        int q = 0;

        while(i >= 0){
                String insert_arraylist = "INSERT INTO ARRAYLIST (STRING) VALUES ('"+imageNames.get(q)+"')";
                db.execSQL(insert_arraylist);
            i--;
            q++;
        }
Run Code Online (Sandbox Code Playgroud)

那么我怎样才能加快插入速度?

提前致谢!

解决方案:(从2分钟到不到3秒;))

        FTPFile[] files = ftpClient.listFiles();
        insert_arraylist = "";
        ContentValues con = new ContentValues();

        for (FTPFile file : files) {
            imageUrls.add("http://192.168.99.104/GetThePicture/thumbs/"+file.getName());
        }

        int i = imageUrls.size() - 1;
        int q = 0; …
Run Code Online (Sandbox Code Playgroud)

sqlite ftp android insert arraylist

3
推荐指数
2
解决办法
2998
查看次数

如何从存储中获取准确的容量

我想以编程方式从实习存储中读取确切的容量。

我使用的是 Samsung Galaxy S7 Edge 32GB 设备。

在预装的三星文件管理器(德语:Eigene Dateien)中,它显示了 32GB 的总容量。即使在菜单 => 设置 => 存储中,它也显示 32GB。

(如截图所示)

当我使用下面的代码时,它显示总容量为 24,4GB。

(在帖子末尾的日志中显示)

问:如何获得我设备的准确 32GB 总容量?

截图

文件管理器(三星) 存储(设置)

完整代码:

package spicysoftware.com.space;

import android.os.Environment;
import android.os.StatFs;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {

    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String InternalFreeSpace = convertBytes(getInternalFreeSpace());
        Log.v("InternalFreeSpace : ", InternalFreeSpace);

        String InternalTotalSpace = convertBytes(getInternalTotalSpace());
        Log.v("InternalTotalSpace : ", InternalTotalSpace);

        String InternalUsedSpace = convertBytes(getInternalUsedSpace());
        Log.v("InternalUsedSpace …
Run Code Online (Sandbox Code Playgroud)

size storage android capacity android-internal-storage

3
推荐指数
1
解决办法
3630
查看次数

如何将 Recyclerview 中的项目居中?

我正在规划一个带有 recyclerviews 的小应用程序。\n有时我只需加载 4 个项目,有时甚至超过 200 个。\n现在我不明白,如何将自定义 recyclerviews 项目居中!

\n\n

我有一个自定义的 recyclerview (网格),我希望其中有 4 (2x2) 个项目居中。

\n\n

我拥有的:

\n\n

在此输入图像描述

\n\n

我想要的是:

\n\n

在此输入图像描述

\n\n

那么我下面的代码错误在哪里呢?

\n\n

fragment_single_pokemon.xml

\n\n
<?xml version="1.0" encoding="utf-8"?>\n<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"\n    xmlns:app="http://schemas.android.com/apk/res-auto"\n    xmlns:tools="http://schemas.android.com/tools"\n    android:layout_width="match_parent"\n    android:layout_height="match_parent"\n    app:layout_behavior="@string/appbar_scrolling_view_behavior"\n    tools:context=".MainActivity">\n\n\n    <android.support.constraint.ConstraintLayout\n        android:id="@+id/cLayTypOne"\n        android:layout_width="50dp"\n        android:layout_height="25dp"\n        android:layout_marginBottom="8dp"\n        android:layout_marginEnd="8dp"\n        android:layout_marginStart="8dp"\n        android:background="@drawable/customborder_typ"\n        app:layout_constraintBottom_toTopOf="@+id/cLayTypTwo"\n        app:layout_constraintEnd_toStartOf="@+id/civSprite"\n        app:layout_constraintStart_toStartOf="parent">\n\n        <TextView\n            android:id="@+id/txtTypOne"\n            android:layout_width="wrap_content"\n            android:layout_height="wrap_content"\n            android:text="TextView"\n            android:textColor="#FFFFFF"\n            android:textSize="8sp"\n            android:textStyle="bold"\n            app:layout_constraintBottom_toBottomOf="parent"\n            app:layout_constraintEnd_toEndOf="parent"\n            app:layout_constraintStart_toStartOf="parent"\n            app:layout_constraintTop_toTopOf="parent" />\n    </android.support.constraint.ConstraintLayout>\n\n    <View\n        android:id="@+id/vLine2"\n        android:layout_width="50dp"\n        android:layout_height="1dp"\n        android:layout_marginBottom="8dp"\n        android:layout_marginEnd="8dp"\n        android:layout_marginStart="8dp"\n        android:layout_marginTop="8dp"\n        android:background="#000000"\n        app:layout_constraintBottom_toBottomOf="@+id/civSprite"\n        app:layout_constraintEnd_toEndOf="parent"\n        app:layout_constraintStart_toEndOf="@+id/civSprite"\n        app:layout_constraintTop_toTopOf="@+id/civSprite">\n\n    </View>\n\n    <TextView\n        android:id="@+id/txtPokemonName"\n        android:layout_width="wrap_content"\n        android:layout_height="wrap_content"\n …
Run Code Online (Sandbox Code Playgroud)

android android-recyclerview

2
推荐指数
1
解决办法
9398
查看次数

FileProvider:安装APK。解析软件包时出错。

我在/storage/emulated/0/Download/app-debug.apk中有一个APK文件。我要通过FileProvider安装此文件,因为我正在使用AndroidN。

当我尝试启动Intent Logcat时,没有显示任何错误,但是我在手机上收到以下消息:

解析软件包时出现问题。

我究竟做错了什么?

主要活动

public class MainActivity extends AppCompatActivity {

Button btnStartIntent;
String strRootPathInternalStorage = Environment.getExternalStorageDirectory().toString();       //-> /storage/emulated/0     //-> /storage/emulated/0/Download/
String strApkToInstall = "app-debug.apk";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

/*
        --> Booooh bad way! <--
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
*/

    btnStartIntent = (Button)findViewById(R.id.btnStartIntent);
    btnStartIntent.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            installApk(MainActivity.this, strRootPathInternalStorage+"/Download/"+strApkToInstall);
        }
    });
}

public static void installApk(Context context, String apkPath) {
    if (context == null || TextUtils.isEmpty(apkPath)) {
        return;
    } …
Run Code Online (Sandbox Code Playgroud)

installation android apk android-fileprovider

1
推荐指数
1
解决办法
1951
查看次数

Android:如何在文件名中获得多个点的文件扩展名?

如何获得名称中包含多个点的文件扩展名?

例如:

image.fromyesterday.hello.jpg

提前致谢!

file-extension

0
推荐指数
1
解决办法
7528
查看次数