Example usage for android.net Uri fromFile

List of usage examples for android.net Uri fromFile

Introduction

In this page you can find the example usage for android.net Uri fromFile.

Prototype

public static Uri fromFile(File file) 

Source Link

Document

Creates a Uri from a file.

Usage

From source file:net.evecom.android.web.Web0Activity.java

public Intent getFileIntent(File file) {
    // Uri uri = Uri.parse("http://m.ql18.com.cn/hpf10/1.pdf");
    Uri uri = Uri.fromFile(file);
    String type = getMIMEType(file);
    Log.i("tag", "type=" + type);
    Intent intent = new Intent("android.intent.action.VIEW");
    intent.addCategory("android.intent.category.DEFAULT");
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setDataAndType(uri, type);//w  w  w.  j a  va  2s  .c  o m
    return intent;
}

From source file:com.qiscus.sdk.ui.fragment.QiscusBaseChatFragment.java

protected void takeImage() {
    if (QiscusPermissionsUtil.hasPermissions(getActivity(), FILE_PERMISSION)) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = QiscusImageUtil.createImageFile();
            } catch (IOException ex) {
                showError(getString(R.string.chat_error_failed_write));
            }/*from   w ww.  j  av  a 2 s. com*/

            if (photoFile != null) {
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                } else {
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(getActivity(),
                            Qiscus.getProviderAuthorities(), photoFile));
                }
                startActivityForResult(intent, TAKE_PICTURE_REQUEST);
            }
            hideAttachmentPanel();
        }
    } else {
        requestPermissions();
    }
}

From source file:com.slim.turboeditor.activity.MainActivity.java

public Observable<Boolean> getSaveFileObservable(final Context context, final File file,
        final String newContent, final String encoding) {
    return Observable.create(new Observable.OnSubscribe<Boolean>() {
        @Override/*www . j ava  2s  . com*/
        public void call(Subscriber<? super Boolean> subscriber) {
            try {
                String filePath = mFile.getAbsolutePath();
                // if the uri has no path
                if (TextUtils.isEmpty(filePath)) {
                    FileUtil.writeUri(context, Uri.fromFile(file), newContent, encoding);
                } else {
                    FileUtil.writeFile(context, newContent, file, encoding);
                }
                subscriber.onNext(true);
                subscriber.onCompleted();
            } catch (Exception e) {
                e.printStackTrace();
                subscriber.onNext(false);
                subscriber.onError(e);
            }
        }
    });
}

From source file:com.ccxt.whl.activity.SettingsFragmentC_0815.java

/**
 * onActivityResult//from  w ww.  j  ava2s.  c om
 */
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == USERPIC_REQUEST_CODE_CAMERA) { // ??
        if (cameraFile != null && cameraFile.exists()) {
            //sendPicture(cameraFile.getAbsolutePath());
            Log.d("log", "cameraFile" + cameraFile.getAbsolutePath());
            //?uri imageUri = Uri.fromFile(cameraFile);
            imageUri = Uri.fromFile(cameraFile);
            cropImageUri(imageUri, 200, 200, USERPIC_REQUEST_CODE_CUT);

        }
    } else if (requestCode == USERPIC_REQUEST_CODE_LOCAL) { //??
        if (imageUritest != null) {
            // Bitmap bitmap = decodeUriAsBitmap(imageUritest);//decode bitmap 
            //iv_user_photo.setImageBitmap(bitmap);  
            Log.d("log", "imageUri==>" + imageUritest);
            iv_user_photo.setImageURI(imageUri);
            RequestParams params = new RequestParams();
            // final String contentType = RequestParams.APPLICATION_OCTET_STREAM;
            File file = Uritofile(imageUritest);
            //File file = new File(imageUri.getPath());
            if (file.exists()) {
                try {
                    params.put("headurl", file, "image/jpg");
                    params.put("user", DemoApplication.getInstance().getUser());
                    params.put("param", "headurl");
                    HttpRestClient.post(Constant.UPDATE_USER_URL, params, responseHandler);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                Toast toast = Toast.makeText(getActivity(), "?SD??",
                        Toast.LENGTH_SHORT);
            }
        } else {
            Toast toast = Toast.makeText(getActivity(), "?SD??",
                    Toast.LENGTH_SHORT);
        }
    } else if (requestCode == USERPIC_REQUEST_CODE_LOCAL_19) { // ?19 
        if (data != null) {
            Uri selectedImage = data.getData();
            if (selectedImage != null) {
                //?uri imageUri = selectedImage;
                //sendPicByUri(selectedImage);
                imageUri = selectedImage;
                sendPicByUri(selectedImage);
                //Log.d("log","selectedImage"+selectedImage);
            }
        }
    } else if (requestCode == USERPIC_REQUEST_CODE_CUT) {//?19
        // ?  
        if (data != null) {

            Log.d("log", "imageUribundle==>" + imageUri);
            iv_user_photo.setImageURI(imageUri);
            RequestParams params = new RequestParams();
            // final String contentType = RequestParams.APPLICATION_OCTET_STREAM;
            File file = Uritofile(imageUri);
            //File file = new File(imageUri.getPath());
            if (file.exists()) {
                try {
                    params.put("headurl", file, "image/jpeg");
                    params.put("user", DemoApplication.getInstance().getUser());
                    params.put("param", "headurl");
                    HttpRestClient.post(Constant.UPDATE_USER_URL, params, responseHandler);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                Toast toast = Toast.makeText(getActivity(), "?SD??",
                        Toast.LENGTH_SHORT);
            }

            //params.put(key, file, )

            //**??data
            //Bitmap bitmap = data.getParcelableExtra("data");
            // Bitmap bitmap = data.getExtras().getParcelable("data");

            /*
            // ByteArrayOutputStream out = new ByteArrayOutputStream(); 
            // bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);  
            */

            //this.iv_image.setImageBitmap(bitmap);  
        } else {
            Toast toast = Toast.makeText(getActivity(), "?SD??",
                    Toast.LENGTH_SHORT);

        }

        /*  try {  
           //   
          // tempFile.delete();  
        } catch (Exception e) {  
           e.printStackTrace();  
        }  */

        RequestParams params = new RequestParams();
        final String contentType = RequestParams.APPLICATION_OCTET_STREAM;
        // params.put(key, file, contentType)
        //HttpRestClient.post(url, params, responseHandler)
    }
}

From source file:com.zertinteractive.wallpaper.activities.DetailActivity.java

public void setAsWallpaperMore() {

    String path = Environment.getExternalStorageDirectory().toString();
    File file = new File(path, "/" + TEMP_WALLPAPER_DIR + "/" + TEMP_WALLPAPER_NAME + ".png");
    Uri uri = Uri.fromFile(file);
    Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    intent.setDataAndType(uri, "image/png");
    intent.putExtra("mimeType", "image/png");
    startActivity(Intent.createChooser(intent, "Set as : Mood Wallpaper"));

    //        WallpaperManager myWallpaperManager
    //                = WallpaperManager.getInstance(getApplicationContext());
    //        try {
    //            myWallpaperManager.setBitmap(((BitmapDrawable) imageView.getDrawable()).getBitmap());
    //        } catch (IOException e) {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        }/*from  w w  w. j a v  a 2s  .com*/
}

From source file:com.ccxt.whl.activity.ChatActivity.java

/**
 * ?//from w  w w. j  a v a 2  s . co m
 */
public void selectPicFromCamera() {
    if (!CommonUtils.isExitsSdcard()) {
        Toast.makeText(getApplicationContext(), "SD????", 0).show();
        return;
    }

    //      cameraFile = new File(PathUtil.getInstance().getImagePath(), DemoApplication.getInstance().getUserName()
    cameraFile = new File(PathUtil.getInstance().getImagePath(),
            DemoApplication.getInstance().getUser() + System.currentTimeMillis() + ".jpg");
    cameraFile.getParentFile().mkdirs();
    startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(cameraFile)), REQUEST_CODE_CAMERA);
}

From source file:com.ieeton.user.activity.ChatActivity.java

/**
 * ?/*from   w  ww .j  a  v a 2s .  c  om*/
 */
public void selectPicFromCamera() {
    if (!CommonUtils.isExitsSdcard()) {
        Toast.makeText(getApplicationContext(), R.string.no_sdcard_for_capture, Toast.LENGTH_SHORT).show();
        return;
    }

    cameraFile = new File(PathUtil.getInstance().getImagePath(),
            IeetonApplication.getInstance().getUserName() + System.currentTimeMillis() + ".jpg");
    cameraFile.getParentFile().mkdirs();
    startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(cameraFile)), REQUEST_CODE_CAMERA);
}

From source file:com.example.carsharing.LongWayActivity.java

@Override
public void onResume() {

    super.onResume(); // Always call the superclass method first

    // Get the Camera instance as the activity achieves full user focus
    Context phonenumber = LongWayActivity.this;
    SharedPreferences filename = phonenumber.getSharedPreferences(getString(R.string.PreferenceDefaultName),
            Context.MODE_PRIVATE);
    UserPhoneNumber = filename.getString("refreshfilename", "0");
    drawernum.setText(UserPhoneNumber);//from   w w  w.  jav a 2  s  . c om
    Context context = LongWayActivity.this;
    SharedPreferences sharedPref = context.getSharedPreferences(UserPhoneNumber, Context.MODE_PRIVATE);
    String fullname = sharedPref.getString("refreshname", "");
    drawername.setText(fullname);
    File photoFile = new File(this.getExternalFilesDir(Environment.DIRECTORY_PICTURES), UserPhoneNumber);
    if (photoFile.exists()) {
        photouri = Uri.fromFile(photoFile);
        drawericon.setImageURI(photouri);
    } else {
        drawericon.setImageResource(R.drawable.ic_launcher);
    }
}

From source file:babybear.akbquiz.ConfigActivity.java

/**
 * ?//  www.j a v a 2 s . co  m
 */
void update() {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), APP_SAVENAME)),
            "application/vnd.android.package-archive");
    startActivity(intent);
}

From source file:com.transistorsoft.cordova.bggeo.CDVBackgroundGeolocation.java

private void emailLog(final CallbackContext callback, final String email) {
    cordova.getThreadPool().execute(new Runnable() {
        public void run() {
            String log = readLog();
            if (log == null) {
                callback.error(500);/*from   w ww .j av a 2s  . c  om*/
                return;
            }

            Intent mailer = new Intent(Intent.ACTION_SEND);
            mailer.setType("message/rfc822");
            mailer.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
            mailer.putExtra(Intent.EXTRA_SUBJECT, "BackgroundGeolocation log");

            try {
                JSONObject state = getState();
                if (state.has("license")) {
                    state.put("license", "<SECRET>");
                }
                if (state.has("orderId")) {
                    state.put("orderId", "<SECRET>");
                }
                mailer.putExtra(Intent.EXTRA_TEXT, state.toString(4));
            } catch (JSONException e) {
                Log.w(TAG, "- Failed to write state to email body");
                e.printStackTrace();
            }
            File file = new File(Environment.getExternalStorageDirectory(), "background-geolocation.log");
            try {
                FileOutputStream stream = new FileOutputStream(file);
                try {
                    stream.write(log.getBytes());
                    stream.close();
                    mailer.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                    file.deleteOnExit();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                Log.i(TAG, "FileNotFound");
                e.printStackTrace();
            }

            try {
                cordova.getActivity()
                        .startActivityForResult(Intent.createChooser(mailer, "Send log: " + email + "..."), 1);
                callback.success();
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(cordova.getActivity(), "There are no email clients installed.",
                        Toast.LENGTH_SHORT).show();
                callback.error("There are no email clients installed");
            }
        }
    });
}