【问题标题】:How to set profile photo option in android?如何在android中设置个人资料照片选项?
【发布时间】:2017-12-20 07:21:59
【问题描述】:

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

与画廊中使用的类似..

我使用了图片的保存和分享按钮,但不知道如何实现设置头像

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) {

        }

    }
});

【问题讨论】:

标签: android


【解决方案1】:

点击按钮:

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" />

【讨论】:

    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多