如何在android中设置个人资料照片选项?

时间:2014-10-07 07:51:57

标签: android

我正在使用图片从网络服务中显示。现在如何使用图片设置为whatsapp的个人资料图片或任何其他个人资料图片选项。我能够保存并共享图像。但是如何在菜单中提供选项或如何将图片设置为 - >

类似于在Gallery中使用的..

enter image description here

我用于保存和分享图片按钮,但不知道如何 实施设置个人资料照片

share.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        BitmapDrawable bitmapDrawable = (BitmapDrawable)image.getDrawable();
        Bitmap bitmap = bitmapDrawable.getBitmap();

        // Save this bitmap to a file.
        File cache = activity.getExternalCacheDir();
        File sharefile = new File(cache, "save.png"); //give your name and save it.
        try {
            FileOutputStream out = new FileOutputStream(sharefile);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        } catch (IOException e) {

        }

        // Now send it out to share
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("image/*");
        share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + sharefile));
        try {
           activity.startActivity(Intent.createChooser(share, "Share photo"));
        } catch (Exception e) {

        }
    }
});

save.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

       image.setDrawingCacheEnabled(true);
       Bitmap bitmap = image.getDrawingCache();

       String root = Environment.getExternalStorageDirectory().toString();
       File newDir = new File(root + "/Nokia");    
       newDir.mkdirs();
       Random gen = new Random();
       int n = 10000;
       n = gen.nextInt(n);
       String fotoname = "Photo-"+ n +".jpg";
       File file = new File (newDir, fotoname);
       if (file.exists ()) file.delete (); 
       try {
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
            Toast.makeText(activity, "Saved to your folder"+fotoname, Toast.LENGTH_SHORT ).show();

        } catch (Exception e) {

        }

    }
});

2 个答案:

答案 0 :(得分:2)

Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.setDataAndType(Uri.parse("file:///" + yourFile), "image/jpg");
intent.putExtra("mimeType", "image/jpg");
startActivityForResult(Intent.createChooser(intent, "Set As"), 200);

答案 1 :(得分:2)

开启按钮点击:

OnButtonClick(){
ImageProcessing imageProcessing = new ImageProcessing();
            Bitmap bitmap = imageProcessing.takeScreenshot(getWindow().getDecorView().findViewById(R.id.view_thought));
            imageProcessing.saveBitmap(bitmap);
            Intent intent = imageProcessing.setAsOption(this,imageProcessing.getSavedImagePath());
            startActivityForResult(Intent.createChooser(intent, "Set image as"), 200);
}

实现一个新类ImageProcessing

public class ImageProcessing {
private File imagesPath;
public void saveBitmap(Bitmap bitmap) {
    imagesPath = new File(Environment.getExternalStorageDirectory() + "/screenshot.jpg");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagesPath);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("POS", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("POS", e.getMessage(), e);
    }
}
public File getSavedImagePath(){
    imagesPath = new File(Environment.getExternalStorageDirectory() + "/screenshot.jpg");
    return imagesPath;
}

public Bitmap takeScreenshot(View rootView) {
    rootView.setDrawingCacheEnabled(true);
    return rootView.getDrawingCache();
}
public Intent setAsOption(Context cntxt,File imagesPath){
    /*File imagesPath = new File(Environment.getExternalStorageDirectory() + "/screenshot.jpg");*/
    Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
    if(imagesPath.exists()){
        Uri contentUri = FileProvider.getUriForFile(cntxt, BuildConfig.APPLICATION_ID+".Utility.GenericFileProvider",imagesPath);

        intent.setDataAndType(contentUri, "image/jpg");
        intent.putExtra("mimeType", "image/jpg");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }else {
        Toast.makeText(cntxt,"Not a wallpaper",Toast.LENGTH_SHORT).show();
    }
    return intent;
}

}

在清单中添加:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />