Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 手机在android中从画廊上传图片时被吊死_Java_Android - Fatal编程技术网

Java 手机在android中从画廊上传图片时被吊死

Java 手机在android中从画廊上传图片时被吊死,java,android,Java,Android,我想从gallery上传图片作为我的个人资料图片。但是我的手机挂了几秒钟,之后,它进入了正常模式并延迟上传图片。有人能帮我避免挂起并快速上传图片吗 这是我的密码 public void res() { System.out.println("res method for jscript"); webView.loadUrl("javascript:callFromActivity(\"" + msgToSend + "\")"); } pub

我想从gallery上传图片作为我的个人资料图片。但是我的手机挂了几秒钟,之后,它进入了正常模式并延迟上传图片。有人能帮我避免挂起并快速上传图片吗

这是我的密码

public void res() {


        System.out.println("res method for jscript");
        webView.loadUrl("javascript:callFromActivity(\"" + msgToSend + "\")");
    }

    public void showCustomAlert() {
        // TODO Auto-generated method stub

        Context context = getApplicationContext();
        // Create layout inflator object to inflate toast.xml file
        LayoutInflater inflater = getLayoutInflater();

        // Call toast.xml file for toast layout
        View toastRoot = inflater.inflate(R.layout.toast, null);

        Toast toast = new Toast(context);

        // Set layout to toast
        toast.setView(toastRoot);
        toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,
                0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    if(data!=null)
    {
        if (requestCode == CAM_REQUREST) {
             Bundle extras = data.getExtras();

             if (extras != null) {
            bitmap_profile_image = extras.getParcelable("data");            
            bitmap_profile_image = (Bitmap) data.getExtras().get("data");
            imagepath = ImageWrite(bitmap_profile_image);
        }
        }

        else if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            imagepath = ImageWrite(BitmapFactory.decodeFile(picturePath));

        } else {

        }
    }

            Imageuploading();

    }

    public String ImageWrite(Bitmap bitmap1) {

        String extStorageDirectory = Environment.getExternalStorageDirectory()
                .toString();
        OutputStream outStream = null;
        File file = new File(extStorageDirectory, "selectimage.PNG");

        try {

            outStream = new FileOutputStream(file);
            bitmap1.compress(Bitmap.CompressFormat.PNG, 100, outStream);`enter code here`
            outStream.flush();
            outStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();

        }
        String imageInSD = "/sdcard/selectimage.PNG";

        return imageInSD;

    }

    protected void Imageuploading() {
        // TODO Auto-generated method stub

        try {

            Log.e("imageuploading", "dfdf");

            HttpURLConnection connection = null;
            DataOutputStream outputStream = null;
            DataInputStream inputStream = null;

            String pathToOurFile = (String) imagepath;

            // String urlServer =
            // "http://demo.cogzideltemplates.com/client/snapchat-clone/index.php/user/image_upload";
            String urlServer = Liveurl+"mobile/image_upload";
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";

            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1 * 1024 * 1024;

            FileInputStream fileInputStream = new FileInputStream(new File(
                    pathToOurFile));

            URL url = new URL(urlServer);
            connection = (HttpURLConnection) url.openConnection();

            // Allow Inputs & Outputs
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);

            // Enable POST method
            connection.setRequestMethod("POST");

            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type",
                    "multipart/form-data;boundary=" + boundary);

            outputStream = new DataOutputStream(connection.getOutputStream());
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream
                    .writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                            + pathToOurFile + "\"" + lineEnd);
            outputStream.writeBytes(lineEnd);

            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];

            // Read file
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0) {
                outputStream.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }

            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens
                    + lineEnd);

            // Responses from the server (code and message)
            int serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();

            // Toast.makeText(getApplicationContext(), serverResponseMessage,
            // Toast.LENGTH_LONG).show();
            System.out.println("image" + serverResponseMessage);

            fileInputStream.close();
            outputStream.flush();
            outputStream.close();

            DataInputStream inputStream1 = null;
            inputStream1 = new DataInputStream(connection.getInputStream());
            String str = "";
            String Str1_imageurl = "";

            while ((str = inputStream1.readLine()) != null) {
                Log.e("Debug", "Server Response " + str);

                Str1_imageurl = str;
                Log.e("Debug", "Server Response String imageurl" + str);
            }
            inputStream1.close();
            System.out.println("image url inputstream1 " + Str1_imageurl);
            // Toast.makeText(getApplicationContext(), Str1_imageurl,
            // Toast.LENGTH_LONG).show();

            msgToSend = Str1_imageurl.trim();

            System.out.println("message to send "+msgToSend);

        } catch (Exception e) {

            e.printStackTrace();

        }

        res();

    }

    public void clearApplicationData() {
        File cache = getCacheDir();
        File appDir = new File(cache.getParent());

        System.out.println("application dir"+appDir);
        if (appDir.exists()) {
            String[] children = appDir.list();
            for (String s : children) {
                if (!s.equals("lib")) {
                    deleteDir(new File(appDir, s));
                    Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
                    Intent i=new Intent(Profile_settings.this,splash.class);
                    //i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                   // clearApplicationData();
                    startActivity(i);
                }
            }
        }
    }

    public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }

        return dir.delete();
    }
    private void loadSavedPreferences() {    
        //User has successfully logged in, save this information
        // We need an Editor object to make preference changes.

        SharedPreferences settings = getSharedPreferences(index.PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.clear();`enter code here`
        editor.commit();

        }
public void res(){
System.out.println(“jscript的res方法”);
loadUrl(“javascript:callFromActivity(\“”+msgToSend+“\”));
}
public void showCustomAlert(){
//TODO自动生成的方法存根
Context=getApplicationContext();
//创建布局充气器对象以充气toast.xml文件
LayoutInflater充气机=getLayoutInflater();
//为toast布局调用toast.xml文件
视图toastRoot=充气机。充气(R.layout.toast,null);
吐司吐司=新吐司(上下文);
//将布局设置为toast
toast.setView(toastRoot);
toast.setGravity(重心水平|重心垂直,
0, 0);
toast.setDuration(toast.LENGTH\u LONG);
toast.show();
}
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
如果(数据!=null)
{
if(请求代码==CAM_请求列表){
Bundle extras=data.getExtras();
如果(附加值!=null){
位图_profile_image=extras.getParcelable(“数据”);
位图_profile_image=(位图)data.getExtras().get(“数据”);
imagepath=ImageWrite(位图\配置文件\图像);
}
}
else if(requestCode==RESULT\u LOAD\u IMAGE&&resultCode==RESULT\u OK
&&空!=数据){
Uri selectedImage=data.getData();
字符串[]filePathColumn={MediaStore.Images.Media.DATA};
Cursor Cursor=getContentResolver().query(selectedImage,
filePathColumn,null,null,null);
cursor.moveToFirst();
int columnIndex=cursor.getColumnIndex(filePathColumn[0]);
String picturePath=cursor.getString(columnIndex);
cursor.close();
imagepath=ImageWrite(BitmapFactory.decodeFile(picturePath));
}否则{
}
}
图像上传();
}
公共字符串ImageWrite(位图位图位图1){
字符串extStorageDirectory=Environment.getExternalStorageDirectory()
.toString();
OutputStream outStream=null;
File File=新文件(extStorageDirectory,“selectimage.PNG”);
试一试{
outStream=新文件OutputStream(文件);
bitmap1.compress(Bitmap.CompressFormat.PNG,100,扩展);`在这里输入代码`
冲水;
exptream.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
字符串imageInSD=“/sdcard/selectimage.PNG”;
返回图像insd;
}
受保护的无效映像上载(){
//TODO自动生成的方法存根
试一试{
Log.e(“图像上传”、“dfdf”);
HttpURLConnection=null;
DataOutputStream outputStream=null;
DataInputStream inputStream=null;
String pathToOurFile=(String)imagepath;
//字符串URL服务器=
// "http://demo.cogzideltemplates.com/client/snapchat-clone/index.php/user/image_upload";
字符串urlServer=Liveurl+“移动/图像上传”;
字符串lineEnd=“\r\n”;
字符串双连字符=“--”;
字符串边界=“*******”;
int字节读取,字节可用,缓冲区大小;
字节[]缓冲区;
int maxBufferSize=1*1024*1024;
FileInputStream FileInputStream=新文件inputstream(新文件(
路径文件);
URL=新URL(URL服务器);
connection=(HttpURLConnection)url.openConnection();
//允许输入和输出
connection.setDoInput(true);
connection.setDoOutput(真);
connection.setUseCaches(false);
//启用POST方法
connection.setRequestMethod(“POST”);
setRequestProperty(“连接”,“保持活动”);
connection.setRequestProperty(“内容类型”,
“多部分/表单数据;边界=”+边界);
outputStream=新的DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(两个连字符+边界+行结束);
输出流
.writeBytes(“内容处置:表单数据;名称=\“uploadedfile\”文件名=\”
+pathToOurFile+“\”+lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable=fileInputStream.available();
bufferSize=Math.min(字节可用,maxBufferSize);
buffer=新字节[bufferSize];
//读取文件
bytesRead=fileInputStream.read(缓冲区,0,缓冲区大小);
而(字节读取>0){
写入(缓冲区,0,缓冲区大小);
bytesAvailable=fileInputStream.available();
bufferSize=Math.min(字节可用,maxBufferSize);
bytesRead=fileInputStream.read(缓冲区,0,缓冲区大小);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(两个连字符+边界+两个连字符
+线路端);
//来自服务器的响应(代码和消息)
int serverResponseCode=connection.getResponseCode();
字符串serverResponseMessage=connection.getResponseMessage();
//Toast.makeText(getApplicationContext(),serverResponseMessage,
//Toast.LENGTH_LONG).show();