安卓系统:应用程序在IntentService期间在我的手机上崩溃,但在Emulator上没有



android新手,我的应用程序不会在Emulator上崩溃,但在手机上崩溃。所以我没有机会看到是什么导致应用程序崩溃。

该应用程序运行IntentService,该服务不断查找图像文件夹,以:-将新图像上传到我的服务器-将图像复制到另一个文件夹-从原始文件夹中删除图像-返回以查找下一个图像

我已经用最少的用户界面设置了我的应用程序。只有两项活动:1。主要活动和2。活动以设置文件夹首选项。主活动上有两个按钮:1。以启动服务,以及2。以停止服务。

当我启动服务并将图像复制到指定的文件夹时,图像会上传到我的服务器,然后移动到指定的移动到手机文件夹-太棒了。但我想,当进程返回文件夹查找下一个文件时,会发生一些事情,迫使应用程序崩溃。

IntentService:

import android.app.IntentService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
public class incomingPicListener extends IntentService {
    Bitmap photoCapturedBitmap;
    private static final String RESULT_SUCCESS = "success";
    private static final String URL_SAVE_IMAGE = "http://.....php";
    Boolean imgsaved;
    String fileFound;
    public incomingPicListener() {
        super("incomingPicListener");
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startid){
        Toast.makeText(incomingPicListener.this, "Service started...", Toast.LENGTH_LONG).show();
        System.out.println("Service Started");
        return super.onStartCommand(intent,flags,startid);
    }
    @Override
    public void onDestroy(){
        super.onDestroy();
        System.out.println("Service Stopped");
        Toast.makeText(incomingPicListener.this, "Service stopped...", Toast.LENGTH_LONG).show();
    }
    @Override
    protected void onHandleIntent(Intent intent) {
        SharedPreferences folderPrefs = getSharedPreferences("FolderPrefs", 0);
        final String savedFolder = folderPrefs.getString("FolderInput", "<Empty>");
        final String savedTargetPhoneFolder = folderPrefs.getString("TargetPhoneFolder", "<Empty>");
        synchronized (this) {
            int count = 0;
            while (count<10) {
                try {
                    wait(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                File dir = new File(savedFolder);
                File[] directoryListing = dir.listFiles(new FilenameFilter() {
                    public boolean accept(File dir, String name) {
                        fileFound=name;
                        return name.toLowerCase().endsWith(".JPG");
                    }
                });
                String filepath = savedFolder+"/"+fileFound;
                File check = new File(savedFolder+"/"+fileFound);
                if ((!check.isDirectory()) && fileFound != null) {
                    String imagepath= savedFolder + "/" + fileFound;
                    if(setupImage(imagepath)){
                        moveFile(imagepath, savedTargetPhoneFolder + "/" + fileFound);
                    }else{
                        Toast.makeText(incomingPicListener.this, "Upload failed: " +imagepath, Toast.LENGTH_LONG).show();
                    }
                }

            }
        }
    }
    public boolean setupImage(String imgpath){
        photoCapturedBitmap = BitmapFactory.decodeFile(imgpath);
        String str = getStringImage(photoCapturedBitmap);
        HashMap HashMap = new HashMap();
        HashMap.put("name",fileFound);
        HashMap.put("image", str);
        String result = sendPostRequest(URL_SAVE_IMAGE, HashMap);
        if (result.toLowerCase().contains(RESULT_SUCCESS)){
            return true;
        }else {
            return false;
        }
    }
    public String getStringImage(Bitmap paramBitmap)
    {
        ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
        paramBitmap.compress(Bitmap.CompressFormat.JPEG, 100, localByteArrayOutputStream);
        return Base64.encodeToString(localByteArrayOutputStream.toByteArray(), 0);
    }
    public void moveFile(String origFile, String trgFile){
        InputStream inStream = null;
        OutputStream outStream = null;
        try{
            File afile =new File(origFile);
            File bfile =new File(trgFile);
            inStream = new FileInputStream(afile);
            outStream = new FileOutputStream(bfile);
            byte[] buffer = new byte[1024];
            int length;
            //copy the file content in bytes
            while ((length = inStream.read(buffer)) > 0){
                outStream.write(buffer, 0, length);
            }
            inStream.close();
            outStream.close();
            //delete the original file
            afile.delete();
            System.out.println("File is copied successful!");
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    public String sendPostRequest(String requestURL, HashMap<String, String> postDataParams) {
        URL url;
        StringBuilder sb = new StringBuilder();
        try {
            url = new URL(requestURL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(15000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            DataOutputStream os = new DataOutputStream (conn.getOutputStream());
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(getPostDataString(postDataParams));
            writer.flush();
            writer.close();
            os.close();
            int responseCode = conn.getResponseCode();
            if (responseCode == HttpsURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                sb = new StringBuilder();
                String response;
                while ((response = br.readLine()) != null) {
                    sb.append(response);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
    public String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
        StringBuilder result = new StringBuilder();
        boolean first = true;
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (first)
                first = false;
            else
                result.append("&");
            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
        }
        return result.toString();
    }
}
enter code here

主要活动:

import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    TextView status;
    Intent loader;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        status = (TextView) findViewById(R.id.tvCurrentState);
        Button btnPref = (Button) findViewById(R.id.bFolderLocations);
        Button btnEnable = (Button) findViewById(R.id.bEnableProcess);
        Button btnDisable = (Button) findViewById(R.id.bDisableProcess);

        btnPref.setOnClickListener(this);
        btnEnable.setOnClickListener(this);
        btnDisable.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bFolderLocations:
                startActivity(new Intent(MainActivity.this, FolderPref.class));
                break;
            case R.id.bEnableProcess:
                startService(this);
                status.setText("Enabled");
                break;
            case R.id.bDisableProcess:
                stopService(this);
                status.setText("Disabled");
                break;
        }

    }
    public void startService(MainActivity view){
        Intent intent = new Intent(this,incomingPicListener.class);
        startService(intent);
    }
    public void stopService (MainActivity view){
        Intent intent = new Intent(this,incomingPicListener.class);
        stopService(intent);
    }
}

这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.imgloader..." >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".FolderPref"
            android:label="@string/title_activity_folder_pref" >
        </activity>
        <service
            android:name=".incomingPicListener"
            android:exported="false" >
        </service>
        <activity
            android:name=".DirectoryChooserActivity"
            android:label="@string/title_activity_directory_chooser" >
        </activity>
    </application>
</manifest>

您是否已将IntentService添加到清单中?

<service
    android:name="com.package.name.MyIntentService"
    android:exported="false" >
</service>

所以我想好了如何在AS上使用手机进行调试。问题是我为fileFound使用的全局变量-我没有重置它,所以当进程循环时,它试图再次加载同一个文件-只是因为我移动了它而不存在。所以我需要在从原始文件夹中删除它后将fileFound设置为null。感谢大家的投入!

相关内容

最新更新