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

时间:2015-11-03 21:00:08

标签: android intentservice

Android新手并且我的应用程序在模拟器上没有崩溃,但它在手机上崩溃了。因此,我没有机会看到导致应用崩溃的原因。

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

我使用最小的UI设置了我的应用。只有2个活动:1。主要活动,以及2.设置文件夹首选项的活动。主要活动有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>

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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