2014-10-07 76 views
1

我正在使用圖片來顯示從web service.Now如何使用圖像設置爲配置文件圖片 whatsapp或任何其他配置文件圖片選項。我可以保存和分享圖像。但是,如何提供一個選項菜單或按鈕,設置圖片原樣>如何在android中設置配置文件照片選項?

這個類似這是在畫廊使用..

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

     } 

    } 
}); 
+0

[android設置圖像作爲聯繫人圖標/壁紙]的可能重複(http://stackoverflow.com/questions/7284142/android-set-image-as-contact-icon-wallpaper) – dilix 2014-10-07 08:56:16

回答

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); 
} 

實現一類新的圖像處理

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; 
} 

}

在menifest附加:

<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); 
相關問題