@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {
      String sdStatus = Environment.getExternalStorageState();
      if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
        Log.i("TestFile", "SD card is not avaiable/writeable right now.");
        return;
      }
      String name =
          new DateFormat().format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
      Toast.makeText(this, name, Toast.LENGTH_LONG).show();
      Bundle bundle = data.getExtras();
      Bitmap bitmap = (Bitmap) bundle.get("data"); // 获取相机返回的数据,并转换为Bitmap图片格式

      FileOutputStream b = null;
      File file = new File("/sdcard/myImage/");
      if (!file.exists()) file.mkdirs(); // 创建文件夹

      String fileName = "/sdcard/myImage/" + name;
      File photofile = new File(fileName);
      if (!photofile.exists()) {
        try {
          photofile.createNewFile();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      try {
        b = new FileOutputStream(photofile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b); // 把数据写入文件
      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        try {
          b.flush();
          b.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      // 动态的改变gridview中的一个Item的内容
      bitmapList.set(currentIndex, bitmap);
      myGalleryAdapter.notifyDataSetChanged();
      updatePhoto(
          Long.valueOf(phoneNumberToId.get(phoneNumber.get(currentIndex))), bitmapToBytes(bitmap));
      //            ((ImageView) findViewById(R.id.imageView)).setImageBitmap(bitmap);//
      // 将图片显示在ImageView里
      Intent intent = new Intent(ContactActivity.this, ContactActivity.class);
      this.startActivity(intent);
      this.finish();
    }
  }
Example #2
0
  private void checkAndLaunchUpdate() {
    Log.i(LOG_FILE_NAME, "Checking for an update");

    int statusCode = 8; // UNEXPECTED_ERROR
    File baseUpdateDir = null;
    if (Build.VERSION.SDK_INT >= 8)
      baseUpdateDir = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
    else baseUpdateDir = new File(Environment.getExternalStorageDirectory().getPath(), "download");

    File updateDir = new File(new File(baseUpdateDir, "updates"), "0");

    File updateFile = new File(updateDir, "update.apk");
    File statusFile = new File(updateDir, "update.status");

    if (!statusFile.exists() || !readUpdateStatus(statusFile).equals("pending")) return;

    if (!updateFile.exists()) return;

    Log.i(LOG_FILE_NAME, "Update is available!");

    // Launch APK
    File updateFileToRun = new File(updateDir, getPackageName() + "-update.apk");
    try {
      if (updateFile.renameTo(updateFileToRun)) {
        String amCmd =
            "/system/bin/am start -a android.intent.action.VIEW "
                + "-n com.android.packageinstaller/.PackageInstallerActivity -d file://"
                + updateFileToRun.getPath();
        Log.i(LOG_FILE_NAME, amCmd);
        Runtime.getRuntime().exec(amCmd);
        statusCode = 0; // OK
      } else {
        Log.i(LOG_FILE_NAME, "Cannot rename the update file!");
        statusCode = 7; // WRITE_ERROR
      }
    } catch (Exception e) {
      Log.i(LOG_FILE_NAME, "error launching installer to update", e);
    }

    // Update the status file
    String status = statusCode == 0 ? "succeeded\n" : "failed: " + statusCode + "\n";

    OutputStream outStream;
    try {
      byte[] buf = status.getBytes("UTF-8");
      outStream = new FileOutputStream(statusFile);
      outStream.write(buf, 0, buf.length);
      outStream.close();
    } catch (Exception e) {
      Log.i(LOG_FILE_NAME, "error writing status file", e);
    }

    if (statusCode == 0) System.exit(0);
  }
Example #3
0
  private boolean unpackFile(ZipFile zip, byte[] buf, ZipEntry fileEntry, String name)
      throws IOException, FileNotFoundException {
    if (fileEntry == null) fileEntry = zip.getEntry(name);
    if (fileEntry == null)
      throw new FileNotFoundException("Can't find " + name + " in " + zip.getName());

    File outFile = new File(sGREDir, name);
    if (outFile.lastModified() == fileEntry.getTime() && outFile.length() == fileEntry.getSize())
      return false;

    File dir = outFile.getParentFile();
    if (!dir.exists()) dir.mkdirs();

    InputStream fileStream;
    fileStream = zip.getInputStream(fileEntry);

    OutputStream outStream = new FileOutputStream(outFile);

    while (fileStream.available() > 0) {
      int read = fileStream.read(buf, 0, buf.length);
      outStream.write(buf, 0, read);
    }

    fileStream.close();
    outStream.close();
    outFile.setLastModified(fileEntry.getTime());
    return true;
  }
Example #4
0
 void removeFiles() throws IOException {
   BufferedReader reader = new BufferedReader(new FileReader(new File(sGREDir, "removed-files")));
   try {
     for (String removedFileName = reader.readLine();
         removedFileName != null;
         removedFileName = reader.readLine()) {
       File removedFile = new File(sGREDir, removedFileName);
       if (removedFile.exists()) removedFile.delete();
     }
   } finally {
     reader.close();
   }
 }
Example #5
0
  public void mOnClickSave(View v) {
    String inputPath = nowPath;
    UseDb db = new UseDb();

    File files = new File(inputPath);
    if (files.exists() == true) {
      db.setValue(this, dbPath, nowPath);

      Intent intent = new Intent(this, Loading.class);
      intent.putExtra("path", nowPath);
      startActivity(intent);

      finish();
    } else {
      Toast.makeText(
              FolderEx.this,
              getResources().getString(R.string.sp_msg_notexistfolder),
              Toast.LENGTH_SHORT)
          .show();
    }
  }